Class: AppMode

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

Overview

This class manages the mode that the executing code is running in.

Constant Summary collapse

@@mode =

Tracks a global mode setting.

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Constructor.

Input

env : Symbol

The environment that the mode should be set to.

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

Valid states.

Notes

env must be a member of states.

Examples

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


92
93
94
95
96
97
98
# File 'lib/app_mode/app_mode.rb', line 92

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 mode.

Input

method : Symbol

The method that was called.

*args : Array

Any arguments that were passed in.

&block : Block

A block, if specified.



154
155
156
157
# File 'lib/app_mode/app_mode.rb', line 154

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.



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

def state
  @state
end

#valid_statesObject (readonly)

Returns the value of attribute valid_states.



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

def valid_states
  @valid_states
end

Class 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.



50
51
52
53
# File 'lib/app_mode/app_mode.rb', line 50

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

.setup(*args) ⇒ Object

Initializes the global mode setting.



56
57
58
# File 'lib/app_mode/app_mode.rb', line 56

def setup(*args)
  @@mode = self.new(*args)
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.



113
114
115
116
# File 'lib/app_mode/app_mode.rb', line 113

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