module React::Component::Tags

contains the name of all HTML tags, and the mechanism to register a component class as a new tag

Constants

HTML_TAGS

Public Class Methods

html_tag_class_for(tag) click to toggle source
# File lib/react/component/tags.rb, line 73
def self.html_tag_class_for(tag)
  downcased_tag = tag.downcase
  if tag =~ /[A-Z]+/ && HTML_TAGS.include?(downcased_tag)
    Object.const_set tag, React.create_element(downcased_tag)
  end
end
included(component) click to toggle source
# File lib/react/component/tags.rb, line 99
def included(component)
  _name, parent = find_name_and_parent(component)
  class << parent
    define_method _name do |*params, &children|
      React::RenderingContext.render(component, *params, &children)
    end
    # handle deprecated _as_node style
    define_method "#{_name}_as_node" do |*params, &children|
      React::RenderingContext.build_only(component, *params, &children)
    end
  end
end

Private Class Methods

find_name_and_parent(component) click to toggle source
# File lib/react/component/tags.rb, line 114
def find_name_and_parent(component)
  split_name = component.name && component.name.split('::')
  if split_name && split_name.length > 1
    [split_name.last, split_name.inject([Module]) { |a, e| a + [a.last.const_get(e)] }[-2]]
  end
end

Public Instance Methods

method_missing(name, *params, &children) click to toggle source

use method_missing to look up component names in the form of “Foo(..)” where there is no preceeding scope.

# File lib/react/component/tags.rb, line 83
def method_missing(name, *params, &children)
  if name =~ /_as_node$/
    # handle deprecated _as_node style
    component = find_component(name.gsub(/_as_node$/, ''))
    return React::RenderingContext.build_only(component, *params, &children) if component
  else
    component = find_component(name)
    return React::RenderingContext.render(component, *params, &children) if component
  end
  Object.method_missing(name, *params, &children)
end
present(component, *params, &children) click to toggle source

the present method is retained as a legacy behavior

# File lib/react/component/tags.rb, line 34
def present(component, *params, &children)
  React::RenderingContext.render(component, *params, &children)
end
present_as_node(component, *params, &children) click to toggle source
# File lib/react/component/tags.rb, line 38
def present_as_node(component, *params, &children)
  React::RenderingContext.build_only(component, *params, &children)
end

Private Instance Methods

find_component(name) click to toggle source
# File lib/react/component/tags.rb, line 124
def find_component(name)
  component = lookup_const(name)
  if component && !component.method_defined?(:render)
    raise "#{name} does not appear to be a react component."
  end
  component
end
lookup_const(name) click to toggle source
# File lib/react/component/tags.rb, line 132
def lookup_const(name)
  return nil unless name =~ /^[A-Z]/
  #html_tag = React::Component::Tags.html_tag_class(name)
  #return html_tag if html_tag
  scopes = self.class.name.to_s.split('::').inject([Module]) do |nesting, next_const|
    nesting + [nesting.last.const_get(next_const)]
  end.reverse
  scope = scopes.detect { |s| s.const_defined?(name) }
  scope.const_get(name) if scope
end