Class: SimpleEnum::Persistence::Hasher
- Inherits:
-
Hash
- Object
- Hash
- SimpleEnum::Persistence::Hasher
- Defined in:
- lib/simple_enum/persistence/hasher.rb
Instance Attribute Summary collapse
-
#enum_key ⇒ Object
readonly
Returns the value of attribute enum_key.
-
#enum_klass ⇒ Object
readonly
Returns the value of attribute enum_klass.
-
#enum_value ⇒ Object
readonly
Returns the value of attribute enum_value.
Instance Method Summary collapse
- #enum_replace(records) ⇒ Object
-
#freeze ⇒ Object
Do nothing when freeze, cause we have to motify the hash later.
-
#initialize(options) ⇒ Hasher
constructor
A new instance of Hasher.
Constructor Details
#initialize(options) ⇒ Hasher
Returns a new instance of Hasher.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/simple_enum/persistence/hasher.rb', line 6 def initialize @enum_klass = [0] = [1] || Hash.new @enum_key = [:key] || :name @enum_value = [:value] || :id unless enum_klass.method_defined? :update_enum_hashers enum_klass.instance_eval do def enum_hashers @enum_hashers ||= [] end def update_enum_hashers records = all.to_a enum_hashers.each do |hasher| hasher.enum_replace records end end end enum_klass.class_eval do def update_enum_hashers self.class.update_enum_hashers end after_commit :update_enum_hashers end end enum_klass.enum_hashers.push self enum_replace enum_klass.all end |
Instance Attribute Details
#enum_key ⇒ Object (readonly)
Returns the value of attribute enum_key.
4 5 6 |
# File 'lib/simple_enum/persistence/hasher.rb', line 4 def enum_key @enum_key end |
#enum_klass ⇒ Object (readonly)
Returns the value of attribute enum_klass.
4 5 6 |
# File 'lib/simple_enum/persistence/hasher.rb', line 4 def enum_klass @enum_klass end |
#enum_value ⇒ Object (readonly)
Returns the value of attribute enum_value.
4 5 6 |
# File 'lib/simple_enum/persistence/hasher.rb', line 4 def enum_value @enum_value end |
Instance Method Details
#enum_replace(records) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/simple_enum/persistence/hasher.rb', line 39 def enum_replace records hash = Hash.new records.each do |record| hash[record.send(enum_key)] = record.send(enum_value) end replace hash end |
#freeze ⇒ Object
Do nothing when freeze, cause we have to motify the hash later
48 49 50 |
# File 'lib/simple_enum/persistence/hasher.rb', line 48 def freeze self end |