|
1 | 1 | module React
|
| 2 | + # NativeLibrary handles importing JS libraries |
| 3 | + # Importing native components is handled by the |
| 4 | + # React::Base. |
2 | 5 | class NativeLibrary
|
3 |
| - def self.renames_and_exclusions |
4 |
| - @renames_and_exclusions ||= {} |
5 |
| - end |
| 6 | + class << self |
| 7 | + def imports(native_name) |
| 8 | + @native_prefix = "#{native_name}." |
| 9 | + self |
| 10 | + end |
6 | 11 |
|
7 |
| - def self.libraries |
8 |
| - @libraries ||= [] |
9 |
| - end |
| 12 | + def rename(rename_list) |
| 13 | + # rename_list is a hash in the form: native_name => ruby_name, native_name => ruby_name |
| 14 | + rename_list.each do |js_name, ruby_name| |
| 15 | + native_name = lookup_native_name(js_name) |
| 16 | + if lookup_native_name(js_name) |
| 17 | + create_wrapper(native_name, ruby_name) |
| 18 | + else |
| 19 | + raise "class #{name} < React::NativeLibrary could not import #{js_name}. "\ |
| 20 | + "Native value #{scope_native_name(js_name)} is undefined." |
| 21 | + end |
| 22 | + end |
| 23 | + end |
10 | 24 |
|
11 |
| - def self.const_missing(name) |
12 |
| - if renames_and_exclusions.has_key? name |
13 |
| - if native_name = renames_and_exclusions[name] |
14 |
| - native_name |
| 25 | + def import_const_from_native(klass, const_name) |
| 26 | + puts "#{klass} is importing_const_from_native: #{const_name}" |
| 27 | + if klass.defined? const_name |
| 28 | + klass.const_get const_name |
15 | 29 | else
|
16 |
| - super |
| 30 | + native_name = lookup_native_name(const_name) || |
| 31 | + lookup_native_name(const_name[0].downcase + const_name[1..-1]) |
| 32 | + wrapper = create_wrapper(klass, native_name, const_name) if native_name |
| 33 | + puts "created #{wrapper} for #{const_name}" |
| 34 | + wrapper |
17 | 35 | end
|
18 |
| - else |
19 |
| - libraries.each do |library| |
20 |
| - native_name = "#{library}.#{name}" |
21 |
| - native_component = `eval(#{native_name})` rescue nil |
22 |
| - React::API.import_native_component(name, native_component) and return name if native_component && `native_component != undefined` |
| 36 | + end |
| 37 | + |
| 38 | + def find_and_render_component(container_class, component_name, args, block, &_failure) |
| 39 | + puts "#{self} is registering #{method_name}" |
| 40 | + component_class = import_const_from_native(container_class, method_name) |
| 41 | + if component_class < React::Component::Base |
| 42 | + React::RenderingContext.build_or_render(nil, component_class, *args, &block) |
| 43 | + else |
| 44 | + yield method_name |
23 | 45 | end
|
24 |
| - name |
25 | 46 | end
|
26 |
| - end |
27 | 47 |
|
28 |
| - def self.method_missing(n, *args, &block) |
29 |
| - name = n |
30 |
| - if name =~ /_as_node$/ |
31 |
| - node_only = true |
32 |
| - name = name.gsub(/_as_node$/, "") |
| 48 | + def const_missing(const_name) |
| 49 | + import_const_from_native(self, const_name) || super |
33 | 50 | end
|
34 |
| - unless name = const_get(name) |
35 |
| - return super |
| 51 | + |
| 52 | + def method_missing(method_name, *args, &block) |
| 53 | + find_and_render_component(self, method_name, args, block) do |
| 54 | + raise "could not import a react component named: #{scope_native_name method_name}" |
| 55 | + end |
36 | 56 | end
|
37 |
| - React::RenderingContext.build_or_render(node_only, name, *args, &block) |
38 |
| - rescue |
39 |
| - end |
40 | 57 |
|
41 |
| - def self.imports(library) |
42 |
| - libraries << library |
43 |
| - end |
| 58 | + private |
44 | 59 |
|
45 |
| - def self.rename(rename_list={}) |
46 |
| - renames_and_exclusions.merge!(rename_list.invert) |
47 |
| - end |
| 60 | + def lookup_native_name(js_name) |
| 61 | + native_name = scope_native_name(js_name) |
| 62 | + `eval(#{native_name}) !== undefined && native_name` |
| 63 | + rescue |
| 64 | + nil |
| 65 | + end |
48 | 66 |
|
49 |
| - def self.exclude(*exclude_list) |
50 |
| - renames_and_exclusions.merge(Hash[exclude_list.map {|k| [k, nil]}]) |
| 67 | + def scope_native_name(js_name) |
| 68 | + "#{@native_prefix}#{js_name}" |
| 69 | + end |
| 70 | + |
| 71 | + def create_wrapper(klass, native_name, ruby_name) |
| 72 | + if React::API.import_native_component native_name |
| 73 | + puts "create wrapper(#{klass.inspect}, #{native_name}, #{ruby_name})" |
| 74 | + new_klass = Class.new |
| 75 | + klass.const_set ruby_name, new_klass |
| 76 | + new_class.class_eval do |
| 77 | + include React::Component::Base |
| 78 | + imports native_name |
| 79 | + end |
| 80 | + puts "successfully created #{klass.inspect}.#{ruby_name} wrapper class for #{native_name}" |
| 81 | + else |
| 82 | + puts "creating wrapper class #{klass}::#{ruby_name} for #{native_name}" |
| 83 | + klass.const_set ruby_name, Class.new(React::NativeLibrary).imports(native_name) |
| 84 | + end |
| 85 | + end |
51 | 86 | end
|
52 | 87 | end
|
53 | 88 | end
|
0 commit comments