11module React
22 module Component
3+ # class level methods (macros) for components
34 module ClassMethods
45 def backtrace ( *args )
56 @dont_catch_exceptions = ( args [ 0 ] == :none )
@@ -9,39 +10,34 @@ def backtrace(*args)
910 def process_exception ( e , component , reraise = nil )
1011 message = [ "Exception raised while rendering #{ component } " ]
1112 if e . backtrace && e . backtrace . length > 1 && !@backtrace_off
12- message << " #{ e . backtrace [ 0 ] } "
13- message += e . backtrace [ 1 ..-1 ] . collect { |line | line }
13+ append_backtrace ( message , e . backtrace )
1414 else
1515 message [ 0 ] += ": #{ e . message } "
1616 end
17- message = message . join ( "\n " )
18- `console.error(message)`
17+ `console.error(#{ message . join ( "\n " ) } )`
1918 raise e if reraise || @dont_catch_exceptions
2019 end
2120
21+ def append_backtrace ( message_array , backtrace )
22+ message_array << " #{ backtrace [ 0 ] } "
23+ backtrace [ 1 ..-1 ] . each { |line | message_array << line }
24+ end
25+
2226 def deprecation_warning ( message )
2327 @deprecation_messages ||= [ ]
24- message = "Warning: Deprecated feature used in #{ self . name } . #{ message } "
28+ message = "Warning: Deprecated feature used in #{ name } . #{ message } "
2529 unless @deprecation_messages . include? message
2630 @deprecation_messages << message
2731 IsomorphicHelpers . log message , :warning
2832 end
2933 end
3034
31- def render ( container = nil , params = { } , &block )
32- if container
33- if block
34- define_method :render do
35- send ( container , params ) { instance_eval &block }
36- end
35+ def render ( container = nil , params = { } , &block )
36+ define_method :render do
37+ if container
38+ React ::RenderingContext . render ( container , params ) { instance_eval ( &block ) if block }
3739 else
38- define_method :render do
39- send ( container , params )
40- end
41- end
42- else
43- define_method :render do
44- instance_eval &block
40+ instance_eval ( &block )
4541 end
4642 end
4743 end
@@ -176,15 +172,19 @@ def static_call_backs
176172 end
177173
178174 def export_component ( opts = { } )
179- export_name = ( opts [ :as ] || name ) . split ( "::" )
175+ export_name = ( opts [ :as ] || name ) . split ( '::' )
180176 first_name = export_name . first
181- 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
177+ Native ( `window` ) [ first_name ] = add_item_to_tree (
178+ Native ( `window` ) [ first_name ] ,
179+ [ React ::API . create_native_react_class ( self ) ] + export_name [ 1 ..-1 ] . reverse
180+ ) . to_n
182181 end
183182
184183 def imports ( component_name )
185- React ::API . import_native_component ( self ,
186- React ::API . eval_native_react_component ( component_name ) )
187- render { } # define a dummy render method - will never be called...
184+ React ::API . import_native_component (
185+ self , React ::API . eval_native_react_component ( component_name )
186+ )
187+ define_method ( :render ) { } # define a dummy render method - will never be called...
188188 rescue Exception => e # rubocop:disable Lint/RescueException : we need to catch everything!
189189 raise "#{ self } cannot import '#{ component_name } ': #{ e . message } ."
190190 # rubocop:enable Lint/RescueException
@@ -194,9 +194,11 @@ def imports(component_name)
194194
195195 def add_item_to_tree ( current_tree , new_item )
196196 if Native ( current_tree ) . class != Native ::Object || new_item . length == 1
197- new_item . inject { |memo , sub_name | { sub_name => memo } }
197+ new_item . inject { |a , e | { e => a } }
198198 else
199- Native ( current_tree ) [ new_item . last ] = add_item_to_tree ( Native ( current_tree ) [ new_item . last ] , new_item [ 0 ..-2 ] )
199+ Native ( current_tree ) [ new_item . last ] = add_item_to_tree (
200+ Native ( current_tree ) [ new_item . last ] , new_item [ 0 ..-2 ]
201+ )
200202 current_tree
201203 end
202204 end
0 commit comments