Giter Club home page Giter Club logo

simpler_enum's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.