-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathprops_wrapper.rb
More file actions
145 lines (129 loc) · 4.56 KB
/
props_wrapper.rb
File metadata and controls
145 lines (129 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require 'active_support/core_ext/string'
module Hyperstack
module Internal
module Component
class PropsWrapper
attr_reader :component
class << self
def instance_var_name_for(name)
case Hyperstack.naming_convention
when :camelize_params
fix_suffix(name.camelize)
when :prefix_params
"_#{name}"
else
name
end
end
def fix_suffix(name)
return unless name
if name =~ /\?$/
name[0..-2] + '_q'
elsif name =~ /\!$/
name[0..-2] + '_b'
else
name
end
end
def param_accessor_style(style = nil)
@param_accessor_style = style if style
@param_accessor_style ||=
if superclass.respond_to? :param_accessor_style
superclass.param_accessor_style
else
:hyperstack
end
end
def param_definitions
@param_definitions ||=
if superclass.respond_to? :param_definitions
superclass.param_definitions.dup
else
Hash.new
end
end
def define_param(name, param_type, aka = nil)
if param_accessor_style != :legacy || aka
meth_name = aka || name
var_name = fix_suffix(aka) || instance_var_name_for(name)
param_definitions[name] = lambda do |props|
@component.instance_variable_set :"@#{var_name}", val = fetch_from_cache(name, param_type, props)
next unless param_accessor_style == :accessors
`#{@component}[#{"$#{meth_name}"}] = function() { return #{val} }`
# @component.define_singleton_method(name) { val } if param_accessor_style == :accessors
end
return if %i[hyperstack accessors].include? param_accessor_style
end
if param_type == Proc
define_method(name.to_sym) do |*args, &block|
props[name].call(*args, &block) if props[name]
end
else
define_method(name.to_sym) do
fetch_from_cache(name, param_type, props)
end
end
end
def define_all_others(name)
var_name = instance_var_name_for(name)
param_definitions[name] = lambda do |props|
@component.instance_variable_set :"@#{var_name}", val = yield(props)
next unless param_accessor_style == :accessors
`#{@component}[#{"$#{name}"}] = function() { return #{val} }`
# @component.define_singleton_method(name) { val } if param_accessor_style == :accessors
end
define_method(name.to_sym) do
@_all_others_cache ||= yield(props)
end
end
end
def param_accessor_style
self.class.param_accessor_style
end
def initialize(component, incoming = nil)
@component = component
#return if param_accessor_style == :legacy
self.class.param_definitions.each_value do |initializer|
instance_exec(incoming || props, &initializer)
end
end
def reload(next_props)
@_all_others_cache = nil # needed for legacy params wrapper
initialize(@component, next_props)
end
def [](prop)
props[prop]
end
private
def fetch_from_cache(name, param_type, props)
last, cached_value = cache[name]
return cached_value if last.equal?(props[name])
value = convert_param(name, param_type, props)
cache[name] = [props[name], value]
return value
end
def convert_param(name, param_type, props)
if param_type.respond_to? :_react_param_conversion
param_type._react_param_conversion props[name], nil
elsif param_type.is_a?(Array) &&
param_type[0].respond_to?(:_react_param_conversion)
props[name].collect do |param|
param_type[0]._react_param_conversion param, nil
end
else
props[name]
end
end
def cache
@cache ||= Hash.new { |h, k| h[k] = [] }
end
def props
component.props
end
def value_for(name)
self[name].instance_variable_get('@value') if self[name]
end
end
end
end
end