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



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

#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



144
145
146
# File 'lib/hyalite/component.rb', line 144

def child_context
  {}
end

#component_did_mountObject



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_unmountObject



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_mountObject



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_unmountObject



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_stateObject



132
133
134
# File 'lib/hyalite/component.rb', line 132

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



203
204
# File 'lib/hyalite/component.rb', line 203

def render
end

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

Returns:

  • (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

#stateObject



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