Class: Enumerations::Base
- Inherits:
-
ActiveSupport::Multibyte::Chars
- Object
- ActiveSupport::Multibyte::Chars
- 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.
-
.primary_key ⇒ Object
Gets primary key for enumeration class.
-
.primary_key=(key) ⇒ Object
Sets primary key for enumeration class.
-
.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_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.
128 129 130 131 132 133 134 135 |
# File 'lib/enumerations/base.rb', line 128 def initialize(symbol, attributes) super(symbol.to_s) @symbol = symbol @attributes = attributes create_instance_methods end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
126 127 128 |
# File 'lib/enumerations/base.rb', line 126 def attributes @attributes end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
126 127 128 |
# File 'lib/enumerations/base.rb', line 126 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...>]
79 80 81 |
# File 'lib/enumerations/base.rb', line 79 def self.all _values.values end |
.primary_key ⇒ Object
Gets primary key for enumeration class.
Example:
Status.primary_key => # nil
class Status < Enumeration::Base
primary_key = :id
end
Status.primary_key => # :id
107 108 109 |
# File 'lib/enumerations/base.rb', line 107 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
91 92 93 |
# File 'lib/enumerations/base.rb', line 91 def self.primary_key=(key) self._primary_key = key && key.to_sym end |
.symbols ⇒ Object
Returns an array of all enumeration symbols
Example:
Role.symbols => # [:admin, :manager, :staff]
67 68 69 |
# File 'lib/enumerations/base.rb', line 67 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..."
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/enumerations/base.rb', line 26 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..."
55 56 57 58 59 |
# File 'lib/enumerations/base.rb', line 55 def self.values(values) values.each do |symbol, attributes| value(symbol, attributes) end end |