Class: Enumerations::Base

Inherits:
Object
  • Object
show all
Extended by:
FinderMethods
Includes:
Value
Defined in:
lib/enumerations/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FinderMethods

find, find_by, find_by_id, find_by_key, where

Methods included from Value

#==, #to_i, #to_param, #to_s, #to_sym

Constructor Details

#initialize(symbol, attributes) ⇒ Base

Returns a new instance of Base.



87
88
89
90
91
92
# File 'lib/enumerations/base.rb', line 87

def initialize(symbol, attributes)
  @symbol = symbol
  @attributes = attributes

  create_instance_methods
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



85
86
87
# File 'lib/enumerations/base.rb', line 85

def attributes
  @attributes
end

#symbolObject (readonly)

Returns the value of attribute symbol.



85
86
87
# File 'lib/enumerations/base.rb', line 85

def symbol
  @symbol
end

Class Method Details

.allObject

Returns an array of all enumeration values

Example:

Role.all => # [#<Enumerations::Value: @base=Role, @symbol=:admin...>,
               #<Enumerations::Value: @base=Role, @symbol=:manager...>,
               #<Enumerations::Value: @base=Role, @symbol=:staff...>]


81
82
83
# File 'lib/enumerations/base.rb', line 81

def self.all
  _values.values
end

.symbolsObject

Returns an array of all enumeration symbols

Example:

Role.symbols => # [:admin, :manager, :staff]


69
70
71
# File 'lib/enumerations/base.rb', line 69

def self.symbols
  _values.keys
end

.value(symbol, attributes) ⇒ Object

Adding new value to enumeration

Example:

value :admin, id: 1, name: 'Admin', description: 'Some description...'

Role.admin.id             => # 1
Role.find(:admin).name    => # "Admin"
Role.find(1).description  => # "Some description..."


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/enumerations/base.rb', line 25

def self.value(symbol, attributes)
  raise 'Enumeration id is required' if attributes[:id].nil?
  raise "Duplicate symbol #{symbol}" if find(symbol)
  raise "Duplicate id #{attributes[:id]}" if find(attributes[:id])

  self._values = _values.merge(symbol => new(symbol, attributes))
  self._symbol_index = _symbol_index.merge(symbol => attributes[:id])

  # Adds name base finder methods
  #
  # Example:
  #
  #    Role.admin => #<Enumerations::Value: @base=Role, @symbol=:admin...>
  #    Role.staff => #<Enumerations::Value: @base=Role, @symbol=:staff...>
  #
  singleton_class.send(:define_method, symbol) do
    find(symbol)
  end
end

.values(values) ⇒ Object

Adding multiple values to enumeration

Example:

values admin:           { id: 1, name: 'Admin' },
       manager:         { id: 2, name: 'Manager' },
       staff:           { id: 3, name: 'Staff', description: 'Some description...' }

Role.admin.id => # 1
Role.find(:manager).name => # "Manager"
Role.find(3).description => # "Some description..."


57
58
59
60
61
# File 'lib/enumerations/base.rb', line 57

def self.values(values)
  values.each do |symbol, attributes|
    value(symbol, attributes)
  end
end