Welcome to AppMode

AppMode provides easy management of “states”. This gem can be used to indicate the state that a class, module, library, script, or application is running in.

Getting Started

  1. Install AppMode at the command prompt if you haven’t yet:

    gem install app_mode
    
  2. Require the gem in your gemfile:

    gem 'app_mode', '~> 1.0.0'
    
  3. Require the gem wherever you need state management:

    require 'app_mode'
    

Usage

There are two ways to use this gem.

  1. 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])
    
  2. Use the class methods: This method is really only recommended for the end application. The initial state will be set dynamically, so there is nothing to do except use it.

    if AppMode.state == :development
      # Development code.
    end
    
    AppMode.state = :test
    
    if AppMode.test
      # Test code.
    end
    

Usage in libraries/gems

Following these guidelines consumers of your library or gem will still have access to the state of your gem and can control it, if need be. Otherwise, changing AppMode.state to :development could put every gem that uses AppMode into “development” mode, which is probably not what you want to have happen to your gem or anyone else’s.

The following method is recommended for use in libraries or gems:

module MyModule
  MyModuleMode = StateManager.new
end

or

class MyClass
  MyClassMode = StateManager.new
end

A Word on States

Additional Documentation

rake rdoc:app

License

RakeTasks is released under the LGPLv3 license.