Module: AlterEgo

Includes:
FailFast::Assertions
Defined in:
lib/alter_ego.rb

Defined Under Namespace

Modules: ClassMethods Classes: AnyMatcher, InvalidDefinitionError, InvalidRequestError, InvalidTransitionError, NotNilMatcher, RequestFilter, State, StateError, StateHook, WrongStateError

Constant Summary collapse

VERSION =
'1.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(klass) ⇒ Object

End ClassMethods



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/alter_ego.rb', line 329

def self.append_features(klass)
  # Give the other module my instance methods at the class level
  klass.extend(ClassMethods)
  klass.extend(Forwardable)

  state_proxy = Module.new
  klass.instance_variable_set :@state_proxy, state_proxy
  klass.send(:include, state_proxy)

  super(klass)
end

.proc_from_symbol_or_block(symbol = nil, &block) ⇒ Object



380
381
382
383
384
385
386
387
388
389
# File 'lib/alter_ego.rb', line 380

def self.proc_from_symbol_or_block(symbol = nil, &block)
  if symbol then
    proc do
      self.send(symbol)
    end
  elsif block then
    block
  else raise "Should never get here"
  end
end

Instance Method Details

#all_handled_requestsObject



364
365
366
# File 'lib/alter_ego.rb', line 364

def all_handled_requests
  self.class.all_handled_requests
end

#current_stateObject



341
342
343
344
# File 'lib/alter_ego.rb', line 341

def current_state
  state_id = self.state
  state_id ? self.class.states[state_id] : nil
end

#execute_request_filters(state, request, new_state) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
# File 'lib/alter_ego.rb', line 368

def execute_request_filters(state, request, new_state)
  pattern = RequestFilter.new(state, request, new_state)
  self.class.request_filters.grep(pattern) do |filter|
    result = catch(:cancel) do
      self.instance_eval(&filter.action)
      true
    end
    return false unless result
  end
  true
end

#stateObject



346
347
348
349
350
# File 'lib/alter_ego.rb', line 346

def state
  result = (@state || self.class.default_state)
  assert(result.nil? || self.class.states.keys.include?(result))
  result
end

#state=(identifier) ⇒ Object



352
353
354
# File 'lib/alter_ego.rb', line 352

def state=(identifier)
  @state = identifier
end

#state_for_identifier(identifier) ⇒ Object



356
357
358
# File 'lib/alter_ego.rb', line 356

def state_for_identifier(identifier)
  self.class.states[identifier]
end

#transition_to(new_state, request = nil, *args) ⇒ Object



360
361
362
# File 'lib/alter_ego.rb', line 360

def transition_to(new_state, request=nil, *args)
  current_state.transition_to(self, request, new_state, *args)
end