Class: ActiveRecord::EavHashes::EavEntry
- Inherits:
-
Base
- Object
- Base
- ActiveRecord::EavHashes::EavEntry
- Defined in:
- lib/eav_hashes/eav_entry.rb
Overview
Represent an EAV row. This class should NOT be used directly, instead it should be inherited from by the class generated by eav_hash_for.
Constant Summary collapse
- SUPPORTED_TYPES =
Contains the values the value_type column should have based on the type of the value being stored
{ :String => 0, :Symbol => 1, :Integer => 2, :Fixnum => 2, :Bignum => 2, :Float => 3, :Complex => 4, :Rational => 5, :Boolean => 6, # For code readability :TrueClass => 6, :FalseClass => 6, :Object => 7 # anything else (including Hashes, Arrays) will be serialized to yaml and saved as Object }
Class Method Summary collapse
-
.get_value_type(val) ⇒ Object
Gets the value_type column’s value for the type of value passed.
Instance Method Summary collapse
-
#after_initialize ⇒ Object
Does some sanity checks.
- #key ⇒ Object
-
#key=(val) ⇒ Object
Raises an error if you try changing the key (unless no key is set).
-
#value ⇒ Object
Gets the EAV row’s value.
-
#value=(val) ⇒ Object
Sets the EAV row’s value.
Class Method Details
.get_value_type(val) ⇒ Object
Gets the value_type column’s value for the type of value passed
71 72 73 74 75 76 77 78 |
# File 'lib/eav_hashes/eav_entry.rb', line 71 def self.get_value_type (val) return nil if val.nil? ret = SUPPORTED_TYPES[val.class.name.to_sym] if ret.nil? ret = SUPPORTED_TYPES[:Object] end ret end |
Instance Method Details
#after_initialize ⇒ Object
Does some sanity checks.
37 38 39 40 41 |
# File 'lib/eav_hashes/eav_entry.rb', line 37 def after_initialize raise "key should be a string or symbol!" unless key.is_a? String or key.is_a? Symbol raise "value should not be empty!" if @value.is_a? String and value.empty? raise "value should not be nil!" if @value.nil? end |
#key ⇒ Object
56 57 58 59 |
# File 'lib/eav_hashes/eav_entry.rb', line 56 def key k = read_attribute :entry_key (read_attribute :symbol_key) ? k.to_sym : k end |
#key=(val) ⇒ Object
Raises an error if you try changing the key (unless no key is set)
62 63 64 65 66 67 |
# File 'lib/eav_hashes/eav_entry.rb', line 62 def key= (val) raise "Keys are immutable!" if read_attribute(:entry_key) raise "Key must be a string!" unless val.is_a?(String) or val.is_a?(Symbol) write_attribute :entry_key, val.to_s write_attribute :symbol_key, (val.is_a? Symbol) end |
#value ⇒ Object
Gets the EAV row’s value
44 45 46 47 |
# File 'lib/eav_hashes/eav_entry.rb', line 44 def value return nil if @value.is_a? NilPlaceholder @value.nil? ? deserialize_value : @value end |
#value=(val) ⇒ Object
Sets the EAV row’s value
51 52 53 |
# File 'lib/eav_hashes/eav_entry.rb', line 51 def value= (val) @value = (val.nil? ? NilPlaceholder.new : val) end |