Class: StateShifter::Definition::Contents

Inherits:
Object
  • Object
show all
Defined in:
lib/state_shifter/definition/contents.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&definition) ⇒ Contents

Returns a new instance of Contents.



7
8
9
10
11
12
# File 'lib/state_shifter/definition/contents.rb', line 7

def initialize &definition
  @states     = {}
  @events     = {}
  @state_tags = {}
  instance_eval &definition if block_given?
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



5
6
7
# File 'lib/state_shifter/definition/contents.rb', line 5

def events
  @events
end

#initial_stateObject

Returns the value of attribute initial_state.



5
6
7
# File 'lib/state_shifter/definition/contents.rb', line 5

def initial_state
  @initial_state
end

#on_transition_procObject

Returns the value of attribute on_transition_proc.



5
6
7
# File 'lib/state_shifter/definition/contents.rb', line 5

def on_transition_proc
  @on_transition_proc
end

#state_tagsObject

Returns the value of attribute state_tags.



5
6
7
# File 'lib/state_shifter/definition/contents.rb', line 5

def state_tags
  @state_tags
end

#statesObject

Returns the value of attribute states.



5
6
7
# File 'lib/state_shifter/definition/contents.rb', line 5

def states
  @states
end

Instance Method Details

#event(hash_or_sym, hash = nil) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/state_shifter/definition/contents.rb', line 36

def event hash_or_sym, hash=nil
  this_event =
    if hash.nil?
      if hash_or_sym.is_a?(Symbol)
        # looping event
        event_name = hash_or_sym

        Event.new @current_state.name.to_sym, event_name
      else
        # normal event
        event_guards = hash_or_sym.delete(:if)
        event_name = hash_or_sym.keys.first
        event_next_state = hash_or_sym[event_name.to_sym]

        Event.new @current_state.name.to_sym, event_name, event_next_state, event_guards
      end
    else
      event_guards = hash.delete(:if)
      event_name = hash_or_sym
      event_callback = hash.delete(:call)

      Event.new @current_state.name.to_sym, event_name, @current_state.name.to_sym, event_guards, event_callback
    end

  raise RedifiningEvent, this_event.name if @events.has_key?(this_event.name.to_sym)

  @events[this_event.name.to_sym] = this_event
  @current_state.events[event_name.to_sym] = this_event
end

#get(key, what) ⇒ Object

end of DSL methods



81
82
83
84
85
86
87
88
# File 'lib/state_shifter/definition/contents.rb', line 81

def get key, what
  case key
  when :event
    @events[what.to_sym] || @events[what.to_s.gsub('!','').to_sym]
  when :state
    @states[what.to_sym] || @states[what.to_s.gsub('!','').to_sym]
  end
end

#has_on_transition_proc?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/state_shifter/definition/contents.rb', line 90

def has_on_transition_proc?
  !@on_transition_proc.nil?
end

#on_entry(event_name = nil, *event_args, &proc_contents) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/state_shifter/definition/contents.rb', line 66

def on_entry event_name=nil, *event_args, &proc_contents
  if event_name.nil?
    @current_state.entry_callback = proc_contents
  else
    @current_state.entry_callback = event_name
    @current_state.entry_callback_args = ( event_args.size == 1 ? event_args.first : event_args ) unless event_args.empty?
  end
end

#on_transition(&proc_contents) ⇒ Object



75
76
77
# File 'lib/state_shifter/definition/contents.rb', line 75

def on_transition &proc_contents
  @on_transition_proc = proc_contents
end

#state(name, &events_and_stuff) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/state_shifter/definition/contents.rb', line 14

def state name, &events_and_stuff
  if @states.empty? # first state declared is the initial one
    this_state = State.new(name, true)
    @initial_state = this_state
  else
    this_state = State.new(name)
  end

  raise RedifiningState, this_state.name if @states.has_key?(this_state.name.to_sym)

  @states[this_state.name.to_sym] = this_state
  @current_state = this_state
  instance_eval &events_and_stuff if events_and_stuff
end

#tags(*names) ⇒ Object



29
30
31
32
33
34
# File 'lib/state_shifter/definition/contents.rb', line 29

def tags *names
  names.each do |name|
    @state_tags[name] ||= []
    @state_tags[name] << @current_state.name
  end
end