Git Product home page Git Product logo

magic-enum's Introduction

MagicEnum

MagicEnum is a simple ActiveRecord plugin that makes it easier to maintain ENUM-like attributes in your models.

Examples:

Statuses = {
  :draft => 0,
  :published => 1,
  :approved => 2
}
define_enum :status

How to Use

Before using define_enum, you should define constant with ENUM options. Constant name would be pluralized enum attribute name. Additional constant named like YourEnumInverted would be created automatically and would contain inverted hash.

Please note: nil and 0 are not the same values!

You could specify additional options:

  • :default - value which will be used when current state of ENUM attribute is invalid or wrong value received. If it has not been specified, min value of the ENUM would be used.

  • :raise_on_invalid - if true an exception would be raised on invalid enum value received. If it is false, default value would be used instead of wrong one.

  • :simple_accessors - if true, additional methods for each ENUM value would be defined in form value?. Methods returns true when ENUM attribute has corresponding value.

  • :enum - string with name of the ENUM hash.

Look the following example:

Statuses = {
  :unknown => 0,
  :draft => 1,
  :published => 2,
  :approved => 3
}
define_enum :status, :default => 1, :raise_on_invalid => true, :simple_accessors => true

This example is identical to:

Statuses = {
  :unknown => 0,
  :draft => 1,
  :published => 2,
  :approved => 3
}
StatusesInverted = Statuses.invert

def status
  return StatusesInverted[self[:status].to_i] || StatusesInverted[1]
end

def status=(value)
  raise ArgumentError, "Invalid value \"#{value}\" for :status attribute of the #{self.class} model" if Statuses[value].nil?
  self[:status] = Statuses[value]
end

def unknown?
  status == :unknown
end

def draft?
  status == :draft
end

def published?
  status == :published
end

def approved?
  status == :approved
end

Who are the authors?

This plugin was originally developed for BestTechVideos project (www.bestechvideos.com) by Dmytro Shteflyuk (kpumuk.info) and later cleaned up in Scribd repository and released to the public by Oleksiy Kovyrin. All the code in this package is released under the MIT license. For more details, see the LICENSE file.

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.