Class: Reacto::Operations::Act

Inherits:
Object
  • Object
show all
Defined in:
lib/reacto/operations/act.rb

Constant Summary collapse

ALL =
%i(value error close)

Instance Method Summary collapse

Constructor Details

#initialize(action = NO_ACTION, on = ALL) ⇒ Act

Returns a new instance of Act.



12
13
14
15
16
17
# File 'lib/reacto/operations/act.rb', line 12

def initialize(action = NO_ACTION, on = ALL)
  @action = action
  @on = on

  @on = ALL if @on == :all
end

Instance Method Details

#call(tracker) ⇒ Object



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
# File 'lib/reacto/operations/act.rb', line 19

def call(tracker)
  value =
    if @on.include?(:value)
      value_action(tracker)
    else
      tracker.method(:on_value)
    end

  error =
    if @on.include?(:error)
      error_action(tracker)
    else
      tracker.method(:on_error)
    end

  close =
    if @on.include?(:close)
      close_action(tracker)
    else
      tracker.method(:on_close)
    end

  Subscriptions::OperationSubscription.new(
    tracker, value: value, error: error, close: close
  )
end

#close_action(tracker) ⇒ Object



60
61
62
63
64
65
# File 'lib/reacto/operations/act.rb', line 60

def close_action(tracker)
  lambda do
    @action.call(OpenStruct.new(type: :close))
    tracker.on_close
  end
end

#error_action(tracker) ⇒ Object



53
54
55
56
57
58
# File 'lib/reacto/operations/act.rb', line 53

def error_action(tracker)
  lambda do |error|
    @action.call(OpenStruct.new(error: error, type: :error))
    tracker.on_error(error)
  end
end

#value_action(tracker) ⇒ Object



46
47
48
49
50
51
# File 'lib/reacto/operations/act.rb', line 46

def value_action(tracker)
  lambda do |value|
    @action.call(OpenStruct.new(value: value, type: :value))
    tracker.on_value(value)
  end
end