class React::NativeLibrary

Public methods used by auto-import.rb are import_const_from_native and find_and_render_component

Public Class Methods

const_missing(const_name) click to toggle source
Calls superclass method Object::const_missing
# File lib/react/native_library.rb, line 44
def const_missing(const_name)
  import_const_from_native(self, const_name, true) || super
end
import_const_from_native(klass, const_name, create_library) click to toggle source
# File lib/react/native_library.rb, line 35
def import_const_from_native(klass, const_name, create_library)
  native_name = lookup_native_name(const_name) ||
                lookup_native_name(const_name[0].downcase + const_name[1..-1])
  native_name && (
    create_component_wrapper(klass, native_name, const_name) || (
      create_library &&
        create_library_wrapper(klass, native_name, const_name)))
end
imports(native_name) click to toggle source
# File lib/react/native_library.rb, line 16
def imports(native_name)
  @native_prefix = "#{native_name}."
  self
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/react/native_library.rb, line 48
def method_missing(method_name, *args, &block)
  method = method_name.gsub(/_as_node$/, '') # remove once _as_node is deprecated.
  component_class = get_const(method) if const_defined?(method)
  component_class ||= import_const_from_native(self, method, false)
  raise 'could not import a react component named: '\
        "#{scope_native_name method}" unless component_class
  if method == method_name
    React::RenderingContext.render(component_class, *args, &block)
  else # remove once _as_node is deprecated.
    React::RenderingContext.build_only(component_class, *args, &block)
  end
end
rename(rename_list) click to toggle source
# File lib/react/native_library.rb, line 21
def rename(rename_list)
  # rename_list is a hash in the form: native_name => ruby_name, native_name => ruby_name
  rename_list.each do |js_name, ruby_name|
    native_name = lookup_native_name(js_name)
    if lookup_native_name(js_name)
      create_component_wrapper(self, native_name, ruby_name) ||
        create_library_wrapper(self, native_name, ruby_name)
    else
      raise "class #{name} < React::NativeLibrary could not import #{js_name}. "\
      "Native value #{scope_native_name(js_name)} is undefined."
    end
  end
end

Private Class Methods

create_component_wrapper(klass, native_name, ruby_name) click to toggle source
# File lib/react/native_library.rb, line 76
def create_component_wrapper(klass, native_name, ruby_name)
  if React::API.native_react_component?(native_name)
    new_klass = klass.const_set ruby_name, Class.new
    new_klass.class_eval do
      include React::Component
      imports native_name
    end
    new_klass
  end
end
create_library_wrapper(klass, native_name, ruby_name) click to toggle source
# File lib/react/native_library.rb, line 87
def create_library_wrapper(klass, native_name, ruby_name)
  klass.const_set ruby_name, Class.new(React::NativeLibrary).imports(native_name)
end
lookup_native_name(js_name) click to toggle source
# File lib/react/native_library.rb, line 63
def lookup_native_name(js_name)
  native_name = scope_native_name(js_name)
  `eval(#{native_name}) !== undefined && native_name`
# rubocop:disable Lint/RescueException  # that is what eval raises in Opal >= 0.10.
rescue Exception
  nil
  # rubocop:enable Lint/RescueException
end
scope_native_name(js_name) click to toggle source
# File lib/react/native_library.rb, line 72
def scope_native_name(js_name)
  "#{@native_prefix}#{js_name}"
end