Module: React::Component::ClassMethods
- Defined in:
- lib/react/component/class_methods.rb
Instance Method Summary collapse
- #add_item_to_tree(current_tree, new_item) ⇒ Object
- #backtrace(*args) ⇒ Object
- #collect_other_params_as(name) ⇒ Object
- #default_props ⇒ Object
- #define_param(name, param_type) ⇒ Object
- #define_state(*states, &block) ⇒ Object
- #define_state_methods(this, name, from = nil, &block) ⇒ Object
- #deprecation_warning(message) ⇒ Object
- #export_component(opts = {}) ⇒ Object
- #export_state(*states, &block) ⇒ Object
- #native_mixin(item) ⇒ Object
- #native_mixins ⇒ Object
- #optional_param(name, options = {}) ⇒ Object
- #param(*args) ⇒ Object
- #params(&block) ⇒ Object
- #process_exception(e, component, reraise = nil) ⇒ Object
- #prop_types ⇒ Object
- #props_wrapper ⇒ Object
- #required_param(name, options = {}) ⇒ Object (also: #require_param)
- #static_call_back(name, &block) ⇒ Object
- #static_call_backs ⇒ Object
- #validator ⇒ Object
Instance Method Details
#add_item_to_tree(current_tree, new_item) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/react/component/class_methods.rb', line 180 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 102 103 104 105 106 |
# 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 validator_in_lexial_scope = validator props_wrapper.define_method(name) do @_all_others ||= validator_in_lexial_scope.undefined_props(props) end end |
#default_props ⇒ Object
50 51 52 |
# File 'lib/react/component/class_methods.rb', line 50 def default_props validator.default_props end |
#define_param(name, param_type) ⇒ Object
62 63 64 |
# File 'lib/react/component/class_methods.rb', line 62 def define_param(name, param_type) props_wrapper.define_param(name, param_type, self) end |
#define_state(*states, &block) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/react/component/class_methods.rb', line 108 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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/react/component/class_methods.rb', line 129 def define_state_methods(this, name, from = nil, &block) this.define_method("#{name}") do self.class.deprecation_warning "Direct access to state `#{name}`. Use `state.#{name}` instead." if from.nil? || from == this State.get_state(from || self, name) end this.define_method("#{name}=") do |new_state| self.class.deprecation_warning "Direct assignment to state `#{name}`. Use `#{(from && from != this) ? from : 'state'}.#{name}!` instead." yield name, State.get_state(from || self, name), new_state if block && block.arity > 0 State.set_state(from || self, name, new_state) end this.define_method("#{name}!") do |*args| self.class.deprecation_warning "Direct access to state `#{name}`. Use `state.#{name}` instead." if from.nil? or from == this if args.count > 0 yield name, State.get_state(from || self, name), args[0] if block && block.arity > 0 current_value = State.get_state(from || self, name) State.set_state(from || self, name, args[0]) current_value else current_state = State.get_state(from || self, name) yield name, State.get_state(from || self, name), current_state if block && block.arity > 0 State.set_state(from || self, name, current_state) Observable.new(current_state) do |update| yield name, State.get_state(from || self, name), update if block && block.arity > 0 State.set_state(from || self, name, update) end end end end |
#deprecation_warning(message) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/react/component/class_methods.rb', line 21 def deprecation_warning() @deprecation_messages ||= [] = "Warning: Deprecated feature used in #{self.name}. #{}" unless @deprecation_messages.include? @deprecation_messages << IsomorphicHelpers.log , :warning end end |
#export_component(opts = {}) ⇒ Object
174 175 176 177 178 |
# File 'lib/react/component/class_methods.rb', line 174 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
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/react/component/class_methods.rb', line 118 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 } 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
158 159 160 |
# File 'lib/react/component/class_methods.rb', line 158 def native_mixin(item) native_mixins << item end |
#native_mixins ⇒ Object
162 163 164 |
# File 'lib/react/component/class_methods.rb', line 162 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, = {}) deprecation_warning "`optional_param` is deprecated, use `param param_name: default_value` instead." validator.optional(name, ) end |
#param(*args) ⇒ Object
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 66 def param(*args) if args[0].is_a? Hash = args[0] name = .first[0] default = .first[1] .delete(name) .merge!({default: default}) else name = args[0] = args[1] || {} end if [:default] validator.optional(name, ) else validator.requires(name, ) end end |
#params(&block) ⇒ Object
54 55 56 |
# File 'lib/react/component/class_methods.rb', line 54 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) = ["Exception raised while rendering #{component}"] if e.backtrace && e.backtrace.length > 1 && !@backtrace_off # seems like e.backtrace is empty in safari??? << " #{e.backtrace[0]}" += e.backtrace[1..-1].collect { |line| line } else [0] += ": #{e.}" end = .join("\n") `console.error(message)` raise e if reraise end |
#prop_types ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/react/component/class_methods.rb', line 34 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 |
#props_wrapper ⇒ Object
58 59 60 |
# File 'lib/react/component/class_methods.rb', line 58 def props_wrapper @props_wrapper ||= Class.new(PropsWrapper) 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, = {}) deprecation_warning "`required_param` is deprecated, use `param` instead." validator.requires(name, ) end |
#static_call_back(name, &block) ⇒ Object
166 167 168 |
# File 'lib/react/component/class_methods.rb', line 166 def static_call_back(name, &block) static_call_backs[name] = block end |
#static_call_backs ⇒ Object
170 171 172 |
# File 'lib/react/component/class_methods.rb', line 170 def static_call_backs @static_call_backs ||= {} end |