TEST RELEASE_VERSION GEM_VERSION LICENSE

ActiveValue

ActiveValue::Base is base class for immutable value object that has interfaces like ActiveRecord.
In a class inherited this class, constant variables get the behavior like records of ActiveRecord.

Installation

Add this line to your application's Gemfile:

gem 'active_value'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install active_value

Usage

  1. Define the target class inherited from ActiveValue::Base
  2. List attributes of the object using attr_accessor
  3. Declare constant variables as this instance in this class
class FormCategory < ActiveValue::Base

  attr_accessor :id, :symbol, :name

  CHECK_BOX  = new id: 1, symbol: :checkbox, name: "Check Box"
  RADIO      = new id: 2, symbol: :radio,    name: "Radio Button"
  SELECT_BOX = new id: 3, symbol: :select,   name: "Select Box"
  TEXT       = new id: 4, symbol: :text,     name: "Text Box"
  TEXTAREA   = new id: 5, symbol: :textarea, name: "Text Area"

end

Then, constant variables in the class can be accessed by following methods.

FormCategory.find(1)
=> <#FormCategory id: 1, symbol: :checkbox, name: "Check Box" >

FormCategory.find_by(symbol: :radio).name
=> "Radio Button"

FormCategory.find(1).checkbox?
=> true

FormCategory.pluck(:id, :name)
=> [[1, "Check Box"], [2, "Radio Button"], [3, "Select Box"], ...]

FormCategory.find(2).to_h
=> { :id => 2, :symbol => :radio, :name => "Radio Button" }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hiratai/active_value.

License

The gem is available as open source under the terms of the MIT License.