Class: React::Observable

Inherits:
Object show all
Defined in:
lib/react/observable.rb

Instance Method Summary collapse

Constructor Details

#initialize(value, on_change = nil, &block) ⇒ Observable

Returns a new instance of Observable.



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

def initialize(value, on_change = nil, &block)
  @value = value
  @on_change = on_change || block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(method_sym, *args, &block)
  @value.send(method_sym, *args, &block).tap { |result| @on_change.call @value }
end

Instance Method Details

#call(new_value) ⇒ Object



20
21
22
23
# File 'lib/react/observable.rb', line 20

def call(new_value)
  @on_change.call new_value
  @value = new_value
end

#respond_to?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/react/observable.rb', line 12

def respond_to?(method, *args)
  if [:call, :to_proc].include? method
    true
  else
    @value.respond_to? method, *args
  end
end

#to_procObject



25
26
27
# File 'lib/react/observable.rb', line 25

def to_proc
  lambda { |arg = @value| @on_change.call arg }
end