State store

Build Status Gemnasium status Code Climate

Installation

gem install state_store

require 'state_store'

For Bundler add this in you Gemfile

gem state_store, '~>0.0.4'

And then run

bundle install

Usage

State store all to keep different states in one attribute. It provide module for mixin, it is pure ruby so it is possible to use it in any ruby class.

   class MyFile
      include StateStore
      attr_accessor :permission

      has_states :read,:write,:execute, :in => :permission
   end

   file = MyFile.new
   file.states = [:read,:write]
   file.has_permission?(:read) #=> true
   file.has_permission?(:execute) #=> false
   file.permission #=> 5
   file.states #=> [:read,:write]

It is possible to configure behaviour for #has_states

  • :in is method name where states are stored in numeric format
  • :as allow to change method for states (default is :states). This is useful if you have more than one state attribute per class.

Adding new states

When you need to add new states and you keep want to previous states configurations be valid, then you should add new state as first in queue.

   class Wolf
      include StateStore
      has_states :big,:bad, :in => :characteristic, :as => :nature
   end

   wolf = Wolf.new()
   wolf.nature = [:bad]
   wolf.characteristic #=> 1

Then if you decide to add hungry then you change states as shown below and that will keep previous configuration valid as well as new one.

   class Wolf
      include StateStore
      has_states :hungry, :big, :bad, :in => :characteristic, :as => :nature
   end

   wolf = Wolf.new()
   wolf.nature = [:bad]
   wolf.characteristic #=> 1
   wolf.nature = [:hungry,:bad]
   wolf.has_charasteristic?(:bad) #=> true
   wolf.characteristic #=> 5   

Changing states

It is possible to alter states Array and changes storage attribute will be updated automaticly.

   class Apple
     attr_accessor :status
     include StateStore
     has_states :big, :red, :"with-worm", :in => :status
   end

   apple = Apple.new
   apple.states = [:big,:red]
   apple.status #=> 6
   apple.states.remove(:red)
   apple.status #=> 4
   apple.states.add(:"with-worm")
   apple.status #=> 5

But remember if you will modify states with other methods, like << or delete or others changes will not be stored in status.

Copyright (c) 2012 Arturs Meisters. See LICENSE.txt for further details.