Class: ActiveRecord::EavHashes::EavEntry

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.get_value_type(val) ⇒ Object

Gets the value_type column's value for the type of value passed

Parameters:

  • val (Object)

    the object whose value_type to determine


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_initializeObject

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

#keyObject


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

#valueObject

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

Parameters:

  • val (Object)

    the value


51
52
53
# File 'lib/eav_hashes/eav_entry.rb', line 51

def value= (val)
  @value = (val.nil? ? NilPlaceholder.new : val)
end