Completed
Push — master ( d8f4da...221e1b )
by Yoh
01:09
created

ClassMethods.el()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
require 'hyalite/short_hand'
2
3
module Hyalite
4
  module Component
5
    TAGS = %w(
6
      a abbr address area article aside audio b base bdi bdo blockquote body br button button button button canvas caption
7
      cite code col colgroup command datalist dd del details dfn div dl dt em embed fieldset figcaption figure footer form
8
      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
9
      meter nav noscript object ol optgroup option output p param pre progress q rp rt ruby s samp script section select small
10
      source span strong style sub summary sup table tbody td textarea tfoot th thead time title tr track u ul var video wbr
11
    )
12
13
    def pp(obj)
14
      puts obj.inspect
15
    end
16
17
    attr_accessor :props, :context, :refs
18
19
    def init_component(props, context, updator)
20
      @props = props
21
      @context = context
22
      @updator = updator
23
      @state = State.new(self, updator, initial_state)
24
      @refs = nil
25
    end
26
27
    def self.included(klass)
28
      klass.instance_eval do
29
        define_singleton_method(:state) do |key, initial_value|
30
          (@initial_state ||= {})[key] = initial_value
31
        end
32
33
        define_singleton_method(:initial_state) { @initial_state || {} }
34
      end
35
36
      TAGS.each do |tag|
37
        define_method(tag) do |props, *children|
38
          Hyalite.create_element(tag, props, *children)
39
        end
40
      end
41
42
      klass.extend ClassMethods
43
    end
44
45
    module ClassMethods
46
      def el(props, *children)
47
        Hyalite.create_element(self, props, *children)
48
      end
49
    end
50
51
    def initial_state
52
      self.class.initial_state
53
    end
54
55
    def state
56
      @state.to_h
57
    end
58
59
    def state=(state)
60
      @state.set(state)
61
    end
62
63
    def context_types
64
      {}
65
    end
66
67
    def child_context
68
      {}
69
    end
70
71
    def component_did_mount
72
    end
73
74
    def component_will_mount
75
    end
76
77
    def component_did_mount
0 ignored issues
show
Unused Code Bug introduced by
The method named Hyalite::Component#component_did_mount has been defined twice. The first time in file /home/scrutinizer/build/package/client/hyalite/component.rb at line 71, the second time in file /home/scrutinizer/build/package/client/hyalite/component.rb at line 77. Please rename either of them, if you need them both.
Loading history...
78
    end
79
80
    def component_will_unmount
81
    end
82
83
    def component_will_update(props, state, context)
84
    end
85
86
    def component_did_update(props, state, context)
87
    end
88
89
    def should_component_update(props, state, context)
90
      true
91
    end
92
93
    def force_update(&block)
94
      @updator.enqueue_force_update(self);
95
      if block_given?
96
        @updator.enqueue_callback(self, &block)
97
      end
98
    end
99
100
    def set_state(states, &block)
101
      @updator.enqueue_set_state(self, states)
102
      if block_given?
103
        @updator.enqueue_callback(self, &block)
104
      end
105
    end
106
107
    def render
108
    end
109
110
    class State
111
      def initialize(component, updator, initial_state)
112
        @component = component
113
        @updator = updator
114
        @state = initial_state.clone
115
        initial_state.each do |key, value|
116
          define_singleton_method(key) do
117
            @state[key]
118
          end
119
          define_singleton_method(key + '=') do |value|
120
            @updator.enqueue_set_state(@component, key => value)
121
          end
122
        end
123
      end
124
125
      def [](key)
126
        @state[key]
127
      end
128
129
      def set(state)
130
        @state = state.clone
131
      end
132
133
      def to_h
134
        @state
135
      end
136
    end
137
  end
138
139
  class EmptyComponent
140
    include Component
141
142
    def self.empty_element
143
      @instance ||= ElementObject.new(EmptyComponent, nil, nil, nil, nil)
144
    end
145
146
    def render
147
      Hyalite.create_element("noscript", nil, nil)
148
    end
149
  end
150
151
  class TopLevelWrapper
152
    include Component
153
154
    def render
155
      @props
156
    end
157
  end
158
end
159