0.0
No commit activity in last 3 years
No release in over 3 years
In a class inherited this class, constant variables get the behavior like records of ActiveRecord.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

> 1.0.0
~> 5.14
> 10.0

Runtime

 Project Readme

ActiveValue

TEST RELEASE_VERSION GEM_VERSION LICENSE

Overviews

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.

Supported Verisons

Support Ruby 2.3 or later
Unit tests are passed with Ruby [2.3, 2.5, 2,7, 3.0] on GitHub Actions

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" >
# Also can be accessed by FormCategory::CHECK_BOX

FormCategory.find_by(symbol: :checkbox).name
=> "Check Box"

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

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

Getter methods for multiple objects are also provided.

FormCategory.all
=> [<#FormCategory id: 1, symbol: :checkbox, name: "Check Box" >, <#FormCategory id: 2, symbol: :radio, name: "Radio Button" >, ...]

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

# Undefined method calls delegate to the all method.
FormCateory.select { |category| category.name.start_with("Text") }
=> [<#FormCategory id: 4, symbol: :text, name: "Text Box" >, <#FormCategory id: 5, symbol: :textarea, name: "Text Area" >]

Specified Attributes

id and symbol are specified attributes. (not required)

  • If id attribute is defined, you can access a instance using find class method.
  • If symbol attribute is defined, you can test a instance has the symbol like FormCategory.find(1).checkbox?.

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.