Module: Hyalite::Component

Included in:
EmptyComponent, TopLevelWrapper
Defined in:
lib/hyalite/component.rb,
lib/hyalite/short_hand.rb

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/hyalite/component.rb', line 197

def method_missing(method_name, *args, &block)
  if @props.has_key?(method_name)
    @props[method_name]
  else
    super
  end
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



17
18
19
# File 'lib/hyalite/component.rb', line 17

def context
  @context
end

#propsObject

Returns the value of attribute props.



17
18
19
# File 'lib/hyalite/component.rb', line 17

def props
  @props
end

#refsObject

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_contextObject



150
151
152
# File 'lib/hyalite/component.rb', line 150

def child_context
  {}
end

#component_did_mountObject



158
159
160
# File 'lib/hyalite/component.rb', line 158

def component_did_mount
  self.instance_eval(&self.class.after_mount) if self.class.after_mount
end

#component_did_unmountObject



166
167
168
# File 'lib/hyalite/component.rb', line 166

def component_did_unmount
  self.instance_eval(&self.class.after_unmount) if self.class.after_unmount
end

#component_did_update(props, state, context) ⇒ Object



174
175
176
# File 'lib/hyalite/component.rb', line 174

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_mountObject



154
155
156
# File 'lib/hyalite/component.rb', line 154

def component_will_mount
  self.instance_eval(&self.class.before_mount) if self.class.before_mount
end

#component_will_unmountObject



162
163
164
# File 'lib/hyalite/component.rb', line 162

def component_will_unmount
  self.instance_eval(&self.class.before_unmount) if self.class.before_unmount
end

#component_will_update(props, state, context) ⇒ Object



170
171
172
# File 'lib/hyalite/component.rb', line 170

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



182
183
184
185
186
187
# File 'lib/hyalite/component.rb', line 182

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_stateObject



138
139
140
# File 'lib/hyalite/component.rb', line 138

def initial_state
  self.class.initial_state
end

#pp(obj) ⇒ Object



13
14
15
# File 'lib/hyalite/component.rb', line 13

def pp(obj)
  puts obj.inspect
end

#renderObject



209
210
# File 'lib/hyalite/component.rb', line 209

def render
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/hyalite/component.rb', line 205

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



189
190
191
192
193
194
# File 'lib/hyalite/component.rb', line 189

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



178
179
180
# File 'lib/hyalite/component.rb', line 178

def should_component_update(props, state, context)
  true
end

#stateObject



142
143
144
# File 'lib/hyalite/component.rb', line 142

def state
  @state.to_h
end

#state=(state) ⇒ Object



146
147
148
# File 'lib/hyalite/component.rb', line 146

def state=(state)
  @state.set(state)
end