Class: React::Generators::ComponentGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/react/component_generator.rb

Constant Summary collapse

REACT_PROP_TYPES =
{
  "node" =>        'React.PropTypes.node',
  "bool" =>        'React.PropTypes.bool',
  "boolean" =>     'React.PropTypes.bool',
  "string" =>      'React.PropTypes.string',
  "number" =>      'React.PropTypes.number',
  "object" =>      'React.PropTypes.object',
  "array" =>       'React.PropTypes.array',
  "shape" =>       'React.PropTypes.shape({})',
  "element" =>     'React.PropTypes.element',
  "func" =>        'React.PropTypes.func',
  "function" =>    'React.PropTypes.func',
  "any" =>         'React.PropTypes.any',

  "instanceOf" => ->(type) {
    'React.PropTypes.instanceOf(%s)' % type.to_s.camelize
  },

  "oneOf" => ->(*options) {
    enums = options.map{|k| "'#{k.to_s}'"}.join(',')
    'React.PropTypes.oneOf([%s])' % enums
  },

  "oneOfType" => ->(*options) {
    types = options.map{|k| "#{lookup(k.to_s, k.to_s)}" }.join(',')
    'React.PropTypes.oneOfType([%s])' % types
  },
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lookup(type = "node", options = "") ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/generators/react/component_generator.rb', line 124

def self.lookup(type = "node", options = "")
  react_prop_type = REACT_PROP_TYPES[type]
  if react_prop_type.blank?
    if type =~ /^[[:upper:]]/
      react_prop_type = REACT_PROP_TYPES['instanceOf']
    else
      react_prop_type = REACT_PROP_TYPES['node']
    end
  end

  options = options.to_s.gsub(/[{}]/, '').split(',')

  react_prop_type = react_prop_type.call(*options) if react_prop_type.respond_to? :call
  react_prop_type
end

Instance Method Details

#create_component_fileObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/generators/react/component_generator.rb', line 92

def create_component_file
  extension = case
                when options[:es6]
                  'es6.jsx'
                when options[:coffee]
                  'js.jsx.coffee'
                else
                  'js.jsx'
              end

  file_path = File.join('app/assets/javascripts/components', "#{file_name}.#{extension}")
  template("component.#{extension}", file_path)
end