class StateManager
This class assists in managing the state of a module, class, object, or application.
Attributes
Public Class Methods
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
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.
# 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
Private Instance Methods
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
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.
# 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
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.
# 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
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