Class: React::Component::PropsWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/react/component/props_wrapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props, current_props_wrapper = nil) ⇒ PropsWrapper

Returns a new instance of PropsWrapper.



62
63
64
65
66
67
68
69
# File 'lib/react/component/props_wrapper.rb', line 62

def initialize(props, current_props_wrapper=nil)
  @props = props || {}
  @processed_params = if current_props_wrapper
    current_props_wrapper.unchanged_processed_params(props)
  else
    {}
  end
end

Instance Attribute Details

#propsObject (readonly)

Returns the value of attribute props.



4
5
6
# File 'lib/react/component/props_wrapper.rb', line 4

def props
  @props
end

Class Method Details

.define_param(name, param_type, owner) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/react/component/props_wrapper.rb', line 6

def self.define_param(name, param_type, owner)
  owner.define_method("#{name}") do |*args, &block|
    deprecated_params_method("#{name}", *args, &block)
  end
  if param_type == Observable
    owner.define_method("#{name}!") do |*args|
      deprecated_params_method("#{name}!", *args)
    end
    define_method("#{name}") do
      value_for(name)
    end
    define_method("#{name}!") do |*args|
      current_value = value_for(name)
      if args.count > 0
        props[name].call args[0]
        current_value
      else
        # rescue in case we in middle of render... What happens during a
        # render that causes exception?
        # Where does `dont_update_state` come from?
        props[name].call current_value unless @dont_update_state rescue nil
        props[name]
      end
    end
  elsif param_type == Proc
    define_method("#{name}") do |*args, &block|
      props[name].call(*args, &block) if props[name]
    end
  else
    define_method("#{name}") do
      if @processed_params.has_key? name
        @processed_params[name]
      else
        @processed_params[name] = if param_type.respond_to? :_react_param_conversion
          param_type._react_param_conversion props[name]
        elsif param_type.is_a?(Array) &&
          param_type[0].respond_to?(:_react_param_conversion)
          props[name].collect do |param|
            param_type[0]._react_param_conversion param
          end
        else
          props[name]
        end
      end
    end
  end
end

Instance Method Details

#[](prop) ⇒ Object



71
72
73
# File 'lib/react/component/props_wrapper.rb', line 71

def [](prop)
  props[prop]
end

#unchanged_processed_params(new_props) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/react/component/props_wrapper.rb', line 54

def unchanged_processed_params(new_props)
  Hash[
    *@processed_params.collect do |key, value|
      [key, value] if @props[key].equal? new_props[key] # `#{@props[key]} == #{new_props[key]}`
    end.compact.flatten(1)
  ]
end