Class: StateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/app_mode/state_manager.rb

Overview

This class assists in managing the state of a module, class, object, or application.

Direct Known Subclasses

AppMode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state = :dynamic, valid_states = [:development, :test, :rake, :production]) ⇒ StateManager

Constructor.

Input

state : Symbol

The state that should be used initially.

:dynamic is a special state that will determine the best state based on the environment that the code is running in.

valid_states : Array : (:development, :test, :production)

Valid states.

Notes

env must be a member of states.

Examples

StateManager.new                     #=> <StateManager @state=:rake, @valid_states=[:development, :test, :rake, :production]>
StateManager.new(:test)              #=> <StateManager @state=:test, @valid_states=[:development, :test, :rake, :production]>
StateManager.new(:dev, [:abc, :dev]) #=> <StateManager @state=:dev, @valid_states=[:abc, :dev]>


54
55
56
57
58
59
60
# File 'lib/app_mode/state_manager.rb', line 54

def initialize(
    state        = :dynamic,
    valid_states = [:development, :test, :rake, :production])
  @state = state
  @valid_states = valid_states
  set_state @state
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

Allows the getting of the state since valid states may be specified at run time.

Input

method : Symbol

The method that was called.

*args : Array

Any arguments that were passed in.

&block : Block

A block, if specified.



108
109
110
111
# File 'lib/app_mode/state_manager.rb', line 108

def method_missing(method, *args, &block)
  return method == @state if respond_to_missing?(method, false)
  super
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



39
40
41
# File 'lib/app_mode/state_manager.rb', line 39

def state
  @state
end

#valid_statesObject (readonly)

Returns the value of attribute valid_states.



39
40
41
# File 'lib/app_mode/state_manager.rb', line 39

def valid_states
  @valid_states
end

Instance Method Details

#send(method, *args) ⇒ Object

Override the send method.

This was implemented to cover the case where test is used as a state. In that case, the default behavior was to call the private test method from Kernel. This prevents that behavior in cases where a public method is available via method_missing in this class.



68
69
70
71
# File 'lib/app_mode/state_manager.rb', line 68

def send(method, *args)
  return method_missing(method, *args) if respond_to_missing?(method, false)
  super
end