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
# File 'lib/switcher/statement.rb', line 3

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/switcher/statement.rb', line 10

def name
  @name
end

#statesObject (readonly)

Returns the value of attribute states.



10
11
12
# File 'lib/switcher/statement.rb', line 10

def states
  @states
end

Instance Method Details

#force_state(state) ⇒ Object

def publish



54
55
56
57
58
59
60
61
# 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

  return @instance
end

#publish(event, target, args) ⇒ Object



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
45
46
47
48
49
50
51
52
# File 'lib/switcher/statement.rb', line 20

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

  return unless states.has_key?(state)

  listener = states[state]
  facade   = Facade.new(target, args)

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

  set_state(facade)

  listener.trigger("after_#{event}", facade, @instance, args)

  return if facade.bubble_cancelled

  unless @spec.subscribers.empty?
    @spec.subscribers.each do |sub|
      sub_instance = @instance.send(sub.to_sym)

      if sub_instance.respond_to?(:each)
        sub_instance.each { |ti| sub_switch(ti, target, event, args) }
      else
        sub_switch(sub_instance, target, event, args)
      end
    end # each
  end # unless

  return @instance
end

#state_currentObject



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

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

#state_prevObject



16
17
18
# File 'lib/switcher/statement.rb', line 16

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