class StateManager

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

Attributes

state[R]
valid_states[R]

Public Class Methods

new( state = :dynamic, valid_states = [:development, :test, :rake, :production]) click to toggle source

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]>
# 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

Public Instance Methods

send(method, *args) click to toggle source

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.

Calls superclass method
# 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
state=(value) click to toggle source

Sets the state instance variable.

Input

value : Symbol

The value that will be used for the state.

# File lib/app_mode/state_manager.rb, line 76
def state=(value)
  set_state value
end

Private Instance Methods

dynamic_state() click to toggle source

Returns the appropriate state to use when setting the state dynamically.

Output

Symbol

The state that should be used when setting the state dynamically.

# File lib/app_mode/state_manager.rb, line 87
def dynamic_state
  call = origin
  return @valid_states[0] unless call.sub(/^\.\//, '').match(/\//)

  case call
    when /rake_test_loader\.rb/,
        %r[/tests?/test_\w+?.rb], %r[/tests?/\w+?_test.rb]
      return @valid_states[1]
    when %r[/bin/rake]
      return @valid_states[2]
  end

  return @valid_states.last
end
method_missing(method, *args, &block) click to toggle source

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.

Calls superclass method
# 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
origin() click to toggle source

Returns the first call in the stack.

Output

String

The file that made the first call in the stack.

Notes

This method is overridden during tests.

# File lib/app_mode/state_manager.rb, line 118
def origin
  caller.last
end
respond_to_missing?(method, include_private) click to toggle source

Ensure that the object knows what it can respond to via method_missing.

Input

method : Symbol

The method to check for a response to.

include_private : Boolean

Whether to include private methods.

Output

Boolean

Indicates whether the object will respond to the specified method.

Calls superclass method
# File lib/app_mode/state_manager.rb, line 128
def respond_to_missing?(method, include_private)
  return true if @valid_states.include?(method)
  super
end
set_state(value) click to toggle source

Sets the state.

Input

value : Symbol

The value to use for the state.

# File lib/app_mode/state_manager.rb, line 136
def set_state(value)
  unless @valid_states.include?(value) || value == :dynamic
    raise "Invalid environment setting: '#{value}'."
  end

  if value == :dynamic
    @state = dynamic_state || @valid_states.last
  else
    @state = value
  end
end