Class: React::StateWrapper

Inherits:
BasicObject
Defined in:
lib/react/state.rb

Instance Method Summary collapse

Constructor Details

#initialize(native, from) ⇒ StateWrapper

Returns a new instance of StateWrapper.



3
4
5
6
# File 'lib/react/state.rb', line 3

def initialize(native, from)
  @state_hash = Hash.new(`#{native}.state`)
  @from = from
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/react/state.rb', line 16

def method_missing(method, *args)
  if match = method.match(/^(.+)\!$/)
    if args.count > 0
      current_value = State.get_state(@from, match[1])
      State.set_state(@from, $1, args[0])
      current_value
    else
      current_state = State.get_state(@from, match[1])
      State.set_state(@from, $1, current_state)
      Observable.new(current_state) do |update|
        State.set_state(@from, $1, update)
      end
    end
  else
    State.get_state(@from, method)
  end
end

Instance Method Details

#[](state) ⇒ Object



8
9
10
# File 'lib/react/state.rb', line 8

def [](state)
  @state_hash[state]
end

#[]=(state, new_value) ⇒ Object



12
13
14
# File 'lib/react/state.rb', line 12

def []=(state, new_value)
  @state_hash[state] = new_value
end