Class: Enumeration::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/enumerations/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, attributes) ⇒ Base

Returns a new instance of Base.



117
118
119
120
121
# File 'lib/enumerations/base.rb', line 117

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

Instance Attribute Details

#symbolObject (readonly)

Returns the value of attribute symbol.



123
124
125
# File 'lib/enumerations/base.rb', line 123

def symbol
  @symbol
end

Class Method Details

.allObject

Returns an array of all enumeration values

Example:

Role.all => # [#<Enumeration::Value:0x007f8ed7f46100 @base=Role, @symbol=:admin...>,
               #<Enumeration::Value:0x007f8ed7f45de0 @base=Role, @symbol=:manager...>,
               #<Enumeration::Value:0x007f8ed7f45ae8 @base=Role, @symbol=:staff...>]


78
79
80
# File 'lib/enumerations/base.rb', line 78

def self.all
  _values.values
end

.find(key) ⇒ Object

Finds an enumeration by symbol, id or name

Example:

Role.find(:admin)  => #<Enumeration::Value:0x007f8ed7f46100 @base=Role, @symbol=:admin...>
Role.find(2)       => #<Enumeration::Value:0x007f8ed7f45de0 @base=Role, @symbol=:manager...>
Role.find('2')     => #<Enumeration::Value:0x007f8ed7f45de0 @base=Role, @symbol=:manager...>
Role.find('staff') => #<Enumeration::Value:0x007f8ed7f45ae8 @base=Role, @symbol=:staff...>


91
92
93
94
95
96
97
# File 'lib/enumerations/base.rb', line 91

def self.find(key)
  case key
  when Symbol then find_by_key(key)
  when String then find_by_key(key.to_sym) || find_by_id(key.to_i)
  when Integer then find_by_id(key)
  end
end

.find_by(**args) ⇒ Object

Finds an enumeration by defined attribute. Simmilar to AcriveRecord::FinderMethods#find_by

Example:

Role.find_by(name: 'Admin')  => #<Enumeration::Value:0x007f8ed7f46100 @base=Role, @symbol=:admin...>


105
106
107
# File 'lib/enumerations/base.rb', line 105

def self.find_by(**args)
  _values.values.find { |value| args.map { |k, v| value.send(k) == v }.all? }
end

.find_by_id(id) ⇒ Object



113
114
115
# File 'lib/enumerations/base.rb', line 113

def self.find_by_id(id)
  _values[_symbol_index.key(id)]
end

.find_by_key(key) ⇒ Object



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

def self.find_by_key(key)
  _values[key]
end

.symbolsObject

Returns an array of all enumeration symbols

Example:

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


66
67
68
# File 'lib/enumerations/base.rb', line 66

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


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/enumerations/base.rb', line 20

def self.value(symbol, attributes)
  # TODO: make this errors better if needed
  # TODO: test this errors
  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 => #<Enumeration::Value:0x007fff45d7ec30 @base=Role, @symbol=:admin...>
  #    Role.staff => #<Enumeration::Value:0x007f980e9cb0a0 @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..."


54
55
56
57
58
# File 'lib/enumerations/base.rb', line 54

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

Instance Method Details

#==(other) ⇒ Object

Comparison by id, symbol or object

Example:

Role.admin == 1           => true
Role.admin == :admin      => true
Role.admin == Role.admin  => true
Role.admin == 2           => false
Role.admin == :staff      => false
Role.admin == Role.staff  => false

TODO: test if case..when is working with this



153
154
155
156
157
158
159
# File 'lib/enumerations/base.rb', line 153

def ==(other)
  case other
  when Fixnum then other == id
  when Symbol then other == @symbol
  else super
  end
end

#to_iObject



125
126
127
# File 'lib/enumerations/base.rb', line 125

def to_i
  id
end

#to_paramObject



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

def to_param
  id
end

#to_sObject



129
130
131
# File 'lib/enumerations/base.rb', line 129

def to_s
  name
end

#to_symObject



133
134
135
# File 'lib/enumerations/base.rb', line 133

def to_sym
  @symbol
end