Module: Platanus::Enum

Defined in:
lib/platanus/enum.rb

Overview

Adds attr_enum property generator to a module.

When attr_enum is called on one of the model properties name:

  • A getter and setter for the <name>_str property are added, this allows the property to be accessed as a string,

the string representations are obtained from the enumeration module’s constants ::downcased

names.

  • An inclusion validation is added for the property, only values included in the enumeration module’s constants are allowed

Given the following configuration:

module Test
  ONE = 1
  TWO = 2
  THREE = 3
end

class Model
  include Platanus::Enum

  attr_enum :target, Test
end

One could do:

t = Model.new
t.target = Test.ONE
t.target_str = 'one' # Same as above
t.target = 5 # Generates a validation error
t.target_str =  # Raises an InvalidEnumName exception

Defined Under Namespace

Modules: ClassMethods Classes: InvalidEnumName

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



41
42
43
# File 'lib/platanus/enum.rb', line 41

def self.included(base)
  base.extend ClassMethods
end