Class: Enumerations::Base

Inherits:
ActiveSupport::Multibyte::Chars
  • 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_key, find_by_primary_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.



157
158
159
160
161
162
163
164
# File 'lib/enumerations/base.rb', line 157

def initialize(symbol, attributes)
  super(symbol.to_s)

  @symbol = symbol
  @attributes = attributes

  create_instance_methods
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



155
156
157
# File 'lib/enumerations/base.rb', line 155

def attributes
  @attributes
end

#symbolObject (readonly)

Returns the value of attribute symbol.



155
156
157
# File 'lib/enumerations/base.rb', line 155

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...>]


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

def self.all
  _values.values
end

.foreign_key_suffixObject

Gets foreign key suffix for enumeration class.

Example:

Status.foreign_key_suffix  => # nil

class Status < Enumeration::Base
  foreign_key_suffix = :id
end

Status.foreign_key_suffix  => # :id


136
137
138
# File 'lib/enumerations/base.rb', line 136

def self.foreign_key_suffix
  _foreign_key_suffix || Enumerations.configuration.foreign_key_suffix
end

.foreign_key_suffix=(suffix) ⇒ Object

Sets foreign key suffix for enumeration class.

Example:

class Status < Enumeration::Base
  foreign_key_suffix = :id
end


120
121
122
# File 'lib/enumerations/base.rb', line 120

def self.foreign_key_suffix=(suffix)
  self._foreign_key_suffix = suffix && suffix.to_sym
end

.primary_keyObject

Gets primary key for enumeration class.

Example:

Status.primary_key  => # nil

class Status < Enumeration::Base
  primary_key = :id
end

Status.primary_key  => # :id


108
109
110
# File 'lib/enumerations/base.rb', line 108

def self.primary_key
  _primary_key || Enumerations.configuration.primary_key
end

.primary_key=(key) ⇒ Object

Sets primary key for enumeration class.

Example:

class Status < Enumeration::Base
  primary_key = :id
end


92
93
94
# File 'lib/enumerations/base.rb', line 92

def self.primary_key=(key)
  self._primary_key = key && key.to_sym
end

.symbolsObject

Returns an array of all enumeration symbols

Example:

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


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

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.find(:admin).name          => # "Admin"
Role.find(:admin).description   => # "Some description..."


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

def self.value(symbol, attributes = {})
  validate_symbol_and_primary_key(symbol, attributes)

  self._values = _values.merge(symbol => new(symbol, attributes))

  # 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(:manager).description   => # "Some description..."


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

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