Class: Enumerations::Base
- Inherits:
-
Object
- Object
- Enumerations::Base
- Extended by:
- FinderMethods
- Includes:
- Value
- Defined in:
- lib/enumerations/base.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Class Method Summary collapse
-
.all ⇒ Object
Returns an array of all enumeration values.
-
.symbols ⇒ Object
Returns an array of all enumeration symbols.
-
.value(symbol, attributes) ⇒ Object
Adding new value to enumeration.
-
.values(values) ⇒ Object
Adding multiple values to enumeration.
Instance Method Summary collapse
-
#initialize(symbol, attributes) ⇒ Base
constructor
A new instance of Base.
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
85 86 87 |
# File 'lib/enumerations/base.rb', line 85 def attributes @attributes end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
85 86 87 |
# File 'lib/enumerations/base.rb', line 85 def symbol @symbol end |
Class Method Details
.all ⇒ Object
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 |
.symbols ⇒ Object
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 |