Class: React::Component::Styles

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

Instance Method Summary collapse

Constructor Details

#initialize(native, props_prop = false) ⇒ Styles

Returns a new instance of Styles.



5
6
7
8
# File 'lib/react/component/styles.rb', line 5

def initialize(native, props_prop = false)
  @native = native
  @props_prop = props_prop
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(prop, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/react/component/styles.rb', line 50

def method_missing(prop, *args, &block)
  %x{
    let value;
    if (#@props_prop) {
      if (!#@native.props[#@props_prop] || typeof #@native.props[#@props_prop][prop] === 'undefined') {
        console.warn("Style/Theme key '" + prop + "' returning nil!");
        return #{nil};
      }
      value = #@native.props[#@props_prop][prop];
    } else {
      if (!#@native || typeof #@native[prop] === 'undefined') {
        console.warn("Style/Theme key '" + prop + "' returning nil!");
        return #{nil};
      }
      value = #@native[prop];
    }
    if (typeof value === 'string' || typeof value === 'number' || Array.isArray(value)) { return value; }
    if (typeof value === 'function') { return value.apply(#@native, args); }
    return Opal.React.Component.Styles.$new(value);
  }
end

Instance Method Details

#[](prop) ⇒ Object



10
11
12
# File 'lib/react/component/styles.rb', line 10

def [](prop)
  method_missing(prop)
end

#[]=(prop, val) ⇒ Object



14
15
16
# File 'lib/react/component/styles.rb', line 14

def []=(prop, val)
  method_missing(prop, val)
end

#deep_merge(a_hash) ⇒ Object



39
40
41
42
# File 'lib/react/component/styles.rb', line 39

def deep_merge(a_hash)
  native_hash = a_hash.to_n
  React::Component::Styles.new(`mergeDeep(#@native, native_hash)`)
end

#deep_merge!(a_hash) ⇒ Object



44
45
46
47
48
# File 'lib/react/component/styles.rb', line 44

def deep_merge!(a_hash)
  native_hash = a_hash.to_n
  `#@native = mergeDeep(#@native, native_hash)`
  self
end

#to_hObject



72
73
74
75
76
77
# File 'lib/react/component/styles.rb', line 72

def to_h
  %x{
    if (#@props_prop) { return Opal.Hash.$new(#@native.props[#@props_prop]); }
    else { return Opal.Hash.$new(#@native); }
  }
end

#to_nObject



79
80
81
82
83
84
# File 'lib/react/component/styles.rb', line 79

def to_n
  %x{
    if (#@props_prop) { return #@native.props[#@props_prop]; }
    else { return #@native; }
  }
end