Class: AlterEgo::State

Inherits:
Object
  • Object
show all
Extended by:
FailFast::Assertions
Includes:
FailFast::Assertions, HookR::Hooks
Defined in:
lib/alter_ego.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handle(request, method = nil, &block) ⇒ Object



141
142
143
# File 'lib/alter_ego.rb', line 141

def self.handle(request, method = nil, &block)
  define_contextual_method_from_symbol_or_block(request, method, &block)
end

.handled_requestsObject



133
134
135
# File 'lib/alter_ego.rb', line 133

def self.handled_requests
  public_instance_methods(false)
end

.identifierObject



125
126
127
# File 'lib/alter_ego.rb', line 125

def self.identifier
  self
end

.make_hook(name, parent, params) ⇒ Object



145
146
147
# File 'lib/alter_ego.rb', line 145

def self.make_hook(name, parent, params)
  ::AlterEgo::StateHook.new(name, parent, params)
end

.request_filtersObject



137
138
139
# File 'lib/alter_ego.rb', line 137

def self.request_filters
  (@request_filters ||= [])
end

.transition(options, &trans_action) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/alter_ego.rb', line 98

def self.transition(options, &trans_action)
  assert_only_keys(options, :to, :on, :if)
  assert_keys(options, :to)
  guard    = options[:if]
  to_state = options[:to]
  request   = options[:on]
  if request
    handle(request) do
      transition_to(to_state, request)
    end
  end
  valid_transitions << to_state unless valid_transitions.include?(to_state)
  if guard
    method = guard.kind_of?(Symbol) ? guard : nil
    block  = guard.kind_of?(Proc) ? guard : nil
    predicate = AlterEgo.proc_from_symbol_or_block(method, &block)
    guard_proc = proc do
      result = instance_eval(&predicate)
      throw :cancel unless result
    end
    add_request_filter(request, to_state, guard_proc)
  end
  if trans_action
    add_request_filter(request, to_state, trans_action)
  end
end

.valid_transitionsObject



129
130
131
# File 'lib/alter_ego.rb', line 129

def self.valid_transitions
  (@valid_transitions ||= [])
end

Instance Method Details

#==(other) ⇒ Object



161
162
163
# File 'lib/alter_ego.rb', line 161

def ==(other)
  (self.identifier == other) or super(other)
end

#can_handle_request?(request) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
168
# File 'lib/alter_ego.rb', line 165

def can_handle_request?(request)
  return true if respond_to?(request)
  return false
end

#identifierObject



157
158
159
# File 'lib/alter_ego.rb', line 157

def identifier
  self.class.identifier
end

#to_sObject



153
154
155
# File 'lib/alter_ego.rb', line 153

def to_s
  "<State:#{identifier}>"
end

#transition_to(context, request, new_state, *args) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/alter_ego.rb', line 170

def transition_to(context, request, new_state, *args)
  return true if context.state == new_state
  new_state_obj = context.state_for_identifier(new_state)
  unless new_state_obj
    raise(InvalidTransitionError,
          "Context #{context.inspect} has no state '#{new_state}' defined")
  end

  continue = context.execute_request_filters(self.class.identifier,
                                             request,
                                             new_state)
  return false unless continue

  unless valid_transitions.empty? || valid_transitions.include?(new_state)
    raise(InvalidTransitionError,
          "Not allowed to transition from #{self.identifier} to #{new_state}")
  end

  execute_hook(:on_exit, context)
  new_state_obj.execute_hook(:on_enter, context)
  context.state = new_state
  assert(new_state == context.state)
  true
end

#valid_transitionsObject



149
150
151
# File 'lib/alter_ego.rb', line 149

def valid_transitions
  self.class.valid_transitions
end