Module: Hyalite::Component
Defined Under Namespace
Modules: ClassMethods, ShortHand
Classes: ChildrenRenderer, State
Constant Summary
collapse
- TAGS =
%w(
a abbr address area article aside audio b base bdi bdo blockquote body br button button button button canvas caption
cite code col colgroup command datalist dd del details dfn div dl dt em embed fieldset figcaption figure footer form
h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd keygen label legend li link map mark menu meta
meter nav noscript object ol optgroup option output p param pre progress q rp rt ruby s samp script section select small
source span strong style sub summary sup table tbody td textarea tfoot th thead time title tr track u ul var video wbr
)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#child_context ⇒ Object
-
#component_did_mount ⇒ Object
-
#component_did_unmount ⇒ Object
-
#component_did_update(props, state, context) ⇒ Object
-
#component_will_mount ⇒ Object
-
#component_will_unmount ⇒ Object
-
#component_will_update(props, state, context) ⇒ Object
-
#force_update(&block) ⇒ Object
-
#init_component(props, context, updator) ⇒ Object
-
#initial_state ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
-
#pp(obj) ⇒ Object
-
#render ⇒ Object
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#set_state(states, &block) ⇒ Object
(also: #update_state)
-
#should_component_update(props, state, context) ⇒ Object
-
#state ⇒ Object
-
#state=(state) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
191
192
193
194
195
196
197
|
# File 'lib/hyalite/component.rb', line 191
def method_missing(method_name, *args, &block)
if @props.has_key?(method_name)
@props[method_name]
else
super
end
end
|
Instance Attribute Details
Returns the value of attribute context.
17
18
19
|
# File 'lib/hyalite/component.rb', line 17
def context
@context
end
|
Returns the value of attribute props.
17
18
19
|
# File 'lib/hyalite/component.rb', line 17
def props
@props
end
|
Returns the value of attribute refs.
17
18
19
|
# File 'lib/hyalite/component.rb', line 17
def refs
@refs
end
|
Class Method Details
.included(klass) ⇒ Object
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
|
# File 'lib/hyalite/component.rb', line 27
def self.included(klass)
klass.instance_eval do
define_singleton_method(:state) do |key, initial_value|
(@initial_state ||= {})[key] = initial_value
end
define_singleton_method(:initial_state) { @initial_state || {} }
define_singleton_method(:before_mount) do |&block|
if block
@before_mount = block
else
@before_mount
end
end
define_singleton_method(:after_mount) do |&block|
if block
@after_mount = block
else
@after_mount
end
end
define_singleton_method(:before_unmount) do |&block|
if block
@before_unmount = block
else
@before_unmount
end
end
define_singleton_method(:after_unmount) do |&block|
if block
@after_unmount = block
else
@after_unmount
end
end
define_singleton_method(:before_update) do |&block|
if block
@before_update = block
else
@before_update
end
end
define_singleton_method(:after_update) do |&block|
if block
@after_update = block
else
@after_update
end
end
end
TAGS.each do |tag|
define_method(tag) do |props, *children, &block|
if block
Hyalite.create_element_hook do |hook_setter|
renderer = ChildrenRenderer.new(self, hook_setter)
renderer.instance_eval(&block)
children += renderer.children.select{|el| el.is_a?(ElementObject) && el.parent.nil? }
end
end
Hyalite.create_element(tag, props, *children)
end
end
klass.extend ClassMethods
end
|
Instance Method Details
#child_context ⇒ Object
144
145
146
|
# File 'lib/hyalite/component.rb', line 144
def child_context
{}
end
|
#component_did_mount ⇒ Object
152
153
154
|
# File 'lib/hyalite/component.rb', line 152
def component_did_mount
self.instance_eval(&self.class.after_mount) if self.class.after_mount
end
|
#component_did_unmount ⇒ Object
160
161
162
|
# File 'lib/hyalite/component.rb', line 160
def component_did_unmount
self.instance_eval(&self.class.after_unmount) if self.class.after_unmount
end
|
#component_did_update(props, state, context) ⇒ Object
168
169
170
|
# File 'lib/hyalite/component.rb', line 168
def component_did_update(props, state, context)
self.instance_exec(props, state, context, &self.class.after_update) if self.class.after_update
end
|
#component_will_mount ⇒ Object
148
149
150
|
# File 'lib/hyalite/component.rb', line 148
def component_will_mount
self.instance_eval(&self.class.before_mount) if self.class.before_mount
end
|
#component_will_unmount ⇒ Object
156
157
158
|
# File 'lib/hyalite/component.rb', line 156
def component_will_unmount
self.instance_eval(&self.class.before_unmount) if self.class.before_unmount
end
|
#component_will_update(props, state, context) ⇒ Object
164
165
166
|
# File 'lib/hyalite/component.rb', line 164
def component_will_update(props, state, context)
self.instance_exec(props, state, context, &self.class.before_update) if self.class.before_update
end
|
#force_update(&block) ⇒ Object
176
177
178
179
180
181
|
# File 'lib/hyalite/component.rb', line 176
def force_update(&block)
@updator.enqueue_force_update(self);
if block_given?
@updator.enqueue_callback(self, &block)
end
end
|
#init_component(props, context, updator) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/hyalite/component.rb', line 19
def init_component(props, context, updator)
@props = props
@context = context
@updator = updator
@state = State.new(self, updator, initial_state)
@refs = nil
end
|
#initial_state ⇒ Object
132
133
134
|
# File 'lib/hyalite/component.rb', line 132
def initial_state
self.class.initial_state
end
|
13
14
15
|
# File 'lib/hyalite/component.rb', line 13
def pp(obj)
puts obj.inspect
end
|
203
204
|
# File 'lib/hyalite/component.rb', line 203
def render
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
199
200
201
|
# File 'lib/hyalite/component.rb', line 199
def respond_to_missing?(method_name, include_private = false)
@props.has_key?(method_name) || super
end
|
#set_state(states, &block) ⇒ Object
Also known as:
update_state
183
184
185
186
187
188
|
# File 'lib/hyalite/component.rb', line 183
def set_state(states, &block)
@updator.enqueue_set_state(self, states)
if block_given?
@updator.enqueue_callback(self, &block)
end
end
|
#should_component_update(props, state, context) ⇒ Object
172
173
174
|
# File 'lib/hyalite/component.rb', line 172
def should_component_update(props, state, context)
true
end
|
136
137
138
|
# File 'lib/hyalite/component.rb', line 136
def state
@state.to_h
end
|
#state=(state) ⇒ Object
140
141
142
|
# File 'lib/hyalite/component.rb', line 140
def state=(state)
@state.set(state)
end
|