Create an object that handles the state of the module, class, or application:
This is the recommended method for managing the states of gems or libraries.
The reason is that the second method is global and changing the state in
a gem or library means the state will change in the application, too.
Therefore, it is better to localize the state to the gem or library.
See notes below on the :dynamic state, which is the default.
my_mode = StateManager.new
my_mode.state = :test
if my_mode == :development
Development code.
end
if my_mode.test
Test code.
end
Or specify the state you want to use when initializing the object:
my_mode = StateManager.new(:development)
If you would like to use states other than those provided, you are free
to do so:
my_mode = StateManager.new(:blue, [:red, :yellow, :green, :blue])
my_mode.state = :yellow
if my_mode.green
Green code.
end
The :dynamic state is still valid with custom states:
my_mode = StateManager.new(:dynamic, [:red, :yellow, :green, :blue])