Module: React::Component::ClassMethods

Defined in:
lib/react/component/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_item_to_tree(current_tree, new_item) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/react/component/class_methods.rb', line 173

def add_item_to_tree(current_tree, new_item)
  if Native(current_tree).class != Native::Object || new_item.length == 1
    new_item.inject { |memo, sub_name| { sub_name => memo } }
  else
    Native(current_tree)[new_item.last] = add_item_to_tree(Native(current_tree)[new_item.last], new_item[0..-2])
    current_tree
  end
end

#backtrace(*args) ⇒ Object



4
5
6
# File 'lib/react/component/class_methods.rb', line 4

def backtrace(*args)
  @backtrace_off = (args[0] == :off)
end

#collect_other_params_as(name) ⇒ Object



96
97
98
99
100
101
# File 'lib/react/component/class_methods.rb', line 96

def collect_other_params_as(name)
  validator.allow_undefined_props = true
  define_method(name) do
    @_all_others ||= self.class.validator.undefined_props(props)
  end
end

#default_propsObject



41
42
43
# File 'lib/react/component/class_methods.rb', line 41

def default_props
  validator.default_props
end

#define_param_method(name, param_type) ⇒ Object



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
# File 'lib/react/component/class_methods.rb', line 49

def define_param_method(name, param_type)
  if param_type == React::Observable
    (@two_way_params ||= []) << name
    define_method("#{name}") do
      params[name].instance_variable_get("@value") if params[name]
    end
    define_method("#{name}!") do |*args|
      return unless params[name]
      if args.count > 0
        current_value = params[name].instance_variable_get("@value")
        params[name].call args[0]
        current_value
      else
        current_value = params[name].instance_variable_get("@value")
        params[name].call current_value unless @dont_update_state rescue nil # rescue in case we in middle of render
        params[name]
      end
    end
  elsif param_type == Proc
    define_method("#{name}") do |*args, &block|
      params[name].call(*args, &block) if params[name]
    end
  else
    define_method("#{name}") do
      @processed_params[name] ||= if param_type.respond_to? :_react_param_conversion
                                    param_type._react_param_conversion params[name]
                                  elsif param_type.is_a?(Array) && param_type[0].respond_to?(:_react_param_conversion)
                                    params[name].collect { |param| param_type[0]._react_param_conversion param }
                                  else
                                    params[name]
                                  end
    end
  end
end

#define_state(*states, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/react/component/class_methods.rb', line 103

def define_state(*states, &block)
  default_initial_value = (block && block.arity == 0) ? yield : nil
  states_hash = (states.last.is_a?(Hash)) ? states.pop : {}
  states.each { |name| states_hash[name] = default_initial_value }
  (self.initial_state ||= {}).merge! states_hash
  states_hash.each do |name, initial_value|
    define_state_methods(self, name, &block)
  end
end

#define_state_methods(this, name, from = nil, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/react/component/class_methods.rb', line 124

def define_state_methods(this, name, from = nil, &block)
  this.define_method("#{name}") do
    React::State.get_state(from || self, name)
  end
  this.define_method("#{name}=") do |new_state|
    yield name, React::State.get_state(from || self, name), new_state if block && block.arity > 0
    React::State.set_state(from || self, name, new_state)
  end
  this.define_method("#{name}!") do |*args|
    #return unless @native
    if args.count > 0
      yield name, React::State.get_state(from || self, name), args[0] if block && block.arity > 0
      current_value = React::State.get_state(from || self, name)
      React::State.set_state(from || self, name, args[0])
      current_value
    else
      current_state = React::State.get_state(from || self, name)
      yield name, React::State.get_state(from || self, name), current_state if block && block.arity > 0
      React::State.set_state(from || self, name, current_state)
      React::Observable.new(current_state) do |update|
        yield name, React::State.get_state(from || self, name), update if block && block.arity > 0
        React::State.set_state(from || self, name, update)
      end
    end
  end
end

#export_component(opts = {}) ⇒ Object



167
168
169
170
171
# File 'lib/react/component/class_methods.rb', line 167

def export_component(opts = {})
  export_name = (opts[:as] || name).split("::")
  first_name = export_name.first
  Native(`window`)[first_name] = add_item_to_tree(Native(`window`)[first_name], [React::API.create_native_react_class(self)] + export_name[1..-1].reverse).to_n
end

#export_state(*states, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/react/component/class_methods.rb', line 113

def export_state(*states, &block)
  default_initial_value = (block && block.arity == 0) ? yield : nil
  states_hash = (states.last.is_a?(Hash)) ? states.pop : {}
  states.each { |name| states_hash[name] = default_initial_value }
  React::State.initialize_states(self, states_hash)
  states_hash.each do |name, initial_value|
    define_state_methods(self, name, self, &block)
    define_state_methods(singleton_class, name, self, &block)
  end
end

#native_mixin(item) ⇒ Object



151
152
153
# File 'lib/react/component/class_methods.rb', line 151

def native_mixin(item)
  native_mixins << item
end

#native_mixinsObject



155
156
157
# File 'lib/react/component/class_methods.rb', line 155

def native_mixins
  @native_mixins ||= []
end

#optional_param(name, options = {}) ⇒ Object



91
92
93
94
# File 'lib/react/component/class_methods.rb', line 91

def optional_param(name, options = {})
  validator.optional(name, options)
  define_param_method(name, options[:type]) unless name == :params
end

#params(&block) ⇒ Object



45
46
47
# File 'lib/react/component/class_methods.rb', line 45

def params(&block)
  validator.build(&block)
end

#process_exception(e, component, reraise = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/react/component/class_methods.rb', line 8

def process_exception(e, component, reraise = nil)
  message = ["Exception raised while rendering #{component}"]
  if e.backtrace && e.backtrace.length > 1 && !@backtrace_off  # seems like e.backtrace is empty in safari???
    message << "    #{e.backtrace[0]}"
    message += e.backtrace[1..-1].collect { |line| line }
  else
    message[0] += ": #{e.message}"
  end
  message = message.join("\n")
  `console.error(message)`
  raise e if reraise
end

#prop_typesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/react/component/class_methods.rb', line 25

def prop_types
  if self.validator
    {
      _componentValidator: %x{
        function(props, propName, componentName) {
          var errors = #{validator.validate(Hash.new(`props`))};
          var error = new Error(#{"In component `" + self.name + "`\n" + `errors`.join("\n")});
          return #{`errors`.count > 0 ? `error` : `undefined`};
        }
      }
    }
  else
    {}
  end
end

#required_param(name, options = {}) ⇒ Object Also known as: require_param



84
85
86
87
# File 'lib/react/component/class_methods.rb', line 84

def required_param(name, options = {})
  validator.requires(name, options)
  define_param_method(name, options[:type])
end

#static_call_back(name, &block) ⇒ Object



159
160
161
# File 'lib/react/component/class_methods.rb', line 159

def static_call_back(name, &block)
  static_call_backs[name] = block
end

#static_call_backsObject



163
164
165
# File 'lib/react/component/class_methods.rb', line 163

def static_call_backs
  @static_call_backs ||= {}
end

#validatorObject



21
22
23
# File 'lib/react/component/class_methods.rb', line 21

def validator
  @validator ||= React::Validator.new
end