Class: Gameplan::State

Inherits:
Object
  • Object
show all
Includes:
Describe
Defined in:
lib/gameplan/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Describe

#desc, #description, #description_list

Constructor Details

#initialize(app, name, pretty_name, &blk) ⇒ State

Returns a new instance of State.



7
8
9
10
11
12
13
# File 'lib/gameplan/state.rb', line 7

def initialize(app, name, pretty_name, &blk)
  @app = app
  @name = name
  @pretty_name = pretty_name
  @endpoints = []
  @blk = blk
end

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



5
6
7
# File 'lib/gameplan/state.rb', line 5

def endpoints
  @endpoints
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/gameplan/state.rb', line 5

def name
  @name
end

#pretty_nameObject (readonly)

Returns the value of attribute pretty_name.



5
6
7
# File 'lib/gameplan/state.rb', line 5

def pretty_name
  @pretty_name
end

Instance Method Details

#add_error(str) ⇒ Object



48
49
50
# File 'lib/gameplan/state.rb', line 48

def add_error(str)
  @app.add_error(str)
end

#compileObject



15
16
17
# File 'lib/gameplan/state.rb', line 15

def compile
  instance_eval(&@blk)
end

#endpoint(state, desc) ⇒ Object



27
28
29
# File 'lib/gameplan/state.rb', line 27

def endpoint(state, desc)
  @endpoints << Endpoint.new(@app, state, desc)
end

#traverse(user, target_state, count, transitions) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/gameplan/state.rb', line 31

def traverse(user, target_state, count, transitions)
  return if count.zero?
  transitions.emit if @name == target_state
  @endpoints.each do |endpoint|
    transitions.add(@name, endpoint.change_name) do
      @app.states[endpoint.state_name].traverse(user, target_state, count - 1, transitions)
    end
  end
end

#use_common(name, mapping = {}) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/gameplan/state.rb', line 19

def use_common(name, mapping = {})
  common_state = @app.common_states[name]
  desc common_state.description
  @app.common_states[name].endpoints.each do |ep|
    endpoint(ep.state == :self ? self.name : mapping[ep.state] || ep.state, ep.description)
  end
end

#validateObject



41
42
43
44
45
46
# File 'lib/gameplan/state.rb', line 41

def validate
  add_error "State #{@name} has no endpoints" if @endpoints.empty?
  @endpoints.each do |ep|
    add_error "Endpoint `#{ep.state}' (#{ep.description}) in #{@name} doesn't exist" unless @app.states[ep.state]
  end
end