0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
SimplerEnum provides Rails like enumerated type
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

SimplerEnum

Build Status Code Climate Test Coverage

SimplerEnum provides Rails like enumerated type.

It reproduces part of ActiveRecord::Enum features.

I hope it will help you when you want to use ActiveRecord::Enum in your not Rails repositories or older Rails version repositories.

Supported Ruby versions

  • Ruby 1.9
  • Ruby 2.x

Installation

Add this line to your application's Gemfile:

gem 'simpler_enum'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simpler_enum

Usage

Any class can include SimplerEnum and you can define an enumerated type like this:

require "simpler_enum"

class Person
  include SimplerEnum

  simpler_enum mood: %i(awesome excellent great good fine)

  def initialize(mood: :fine)
    self.mood = mood
  end
end

Or,

require "simpler_enum"

class Person
  include SimplerEnum

  simpler_enum mood: {
    awesome: 0,
    excellent: 1,
    great: 2,
    good: 3,
    fine: 4
  }

  def initialize(mood: :fine)
    self.mood = mood
  end
end

Both behave like this:

[1] pry(main)> Person.moods
=> {:awesome=>0, :excellent=>1, :great=>2, :good=>3, :fine=>4}
[2] pry(main)> necojackarc = Person.new(mood: :awesome)
=> #<Person:0x007fd5cbbf5dd0 @mood=0>
[3] pry(main)> necojackarc.awesome?
=> true
[4] pry(main)> necojackarc.excellent?
=> false
[5] pry(main)> necojackarc.great!
=> :great
[6] pry(main)> necojackarc.awesome?
=> false
[7] pry(main)> necojackarc.great?
=> true
[8] pry(main)> necojackarc.mood = :fine
=> :fine
[9] pry(main)> necojackarc.great?
=> false
[10] pry(main)> necojackarc.fine?
=> true
[11] pry(main)> necojackarc.mood = 1 # excellent
=> 1
[12] pry(main)> necojackarc.fine?
=> false
[13] pry(main)> necojackarc.excellent?
=> true
[14] pry(main)> necojackarc.mood
=> :excellent
[15] pry(main)> necojackarc.mood = "good"
=> "good"
[16] pry(main)> necojackarc.mood
=> :good

License

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