Class: Switcher::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/switcher/statement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, spec, state_current = nil, state_prev = nil) ⇒ Statement

Returns a new instance of Statement.



3
4
5
6
7
8
9
10
# File 'lib/switcher/statement.rb', line 3

def initialize(instance, spec, state_current=nil, state_prev=nil)
  @instance = instance

  @spec = spec

  @state_prev    = state_prev
  @state_current = state_current
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/switcher/statement.rb', line 12

def name
  @name
end

#statesObject (readonly)

Returns the value of attribute states.



12
13
14
# File 'lib/switcher/statement.rb', line 12

def states
  @states
end

Instance Method Details

#force_state(state) ⇒ Object



54
55
56
57
58
# File 'lib/switcher/statement.rb', line 54

def force_state(state)
  return unless @spec.states_list.include?(state.to_sym) # FIXME - raise exception
  @state_prev    = state_current
  @state_current = state.to_sym
end

#publish(event, original, args) ⇒ Object



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/switcher/statement.rb', line 22

def publish(event, original, args)
  states = @spec.states
  state  = state_current

  return unless states.has_key?(state)

  facade = Facade.new(original, args)

  ["before_#{event}", event.to_s].each do |ev|
    states[state].trigger(ev, facade, @instance, args)
  end

  set_state(facade)

  states[state].trigger("after_#{event}", facade, @instance, args)

  return if facade.bubble_cancelled

  if @spec.targets.length > 0
    @spec.targets.each do |target|
      target_instance = @instance.send(target.to_sym)
      if target_instance.respond_to?(:each)
        target_instance.each do |ti|
          ti.switch_from(original, event, args) if ti.respond_to?(:switch_from)
        end
      else
        target_instance.switch_from(original, event, args) if target_instance.respond_to?(:switch_from)
      end
    end
  end
end

#state_currentObject



14
15
16
# File 'lib/switcher/statement.rb', line 14

def state_current
  (@state_current || @spec.states_list.first).to_sym
end

#state_prevObject



18
19
20
# File 'lib/switcher/statement.rb', line 18

def state_prev
  @state_prev ? @state_prev.to_sym : nil
end