Class: ActiveRecord::EavHashes::EavHash

Inherits:
Object
  • Object
show all
Defined in:
lib/eav_hashes/eav_hash.rb

Overview

Wraps a bunch of EavEntries and lets you use them like you would a hash This class should not be used directly and you should instead let eav_hash_for create one for you

Instance Method Summary collapse

Constructor Details

#initialize(owner, options) ⇒ EavHash

Creates a new EavHash. You should really let eav_hash_for do this for you…

Parameters:

  • owner (ActiveRecord::Base)

    the Model which will own this hash

  • options (Hash)

    the options hash which eav_hash generated


9
10
11
12
13
# File 'lib/eav_hashes/eav_hash.rb', line 9

def initialize(owner, options)
  Util::sanity_check options
  @owner = owner
  @options = options
end

Instance Method Details

#<<(dirt) ⇒ Object

I don't know why Ruby hashes don't have a shovel operator, but I will make damn sure that I fight the power and stick it to the man by implementing it.

Parameters:

  • dirt (Hash, EavHash)

    the dirt to shovel (ba dum, tss)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/eav_hashes/eav_hash.rb', line 49

def <<(dirt)
  if dirt.is_a? Hash
    dirt.each do |key, value|
      update_or_create_entry key, value
    end
  elsif dirt.is_a? EavHash
    dirt.entries.each do |key, entry|
      update_or_create_entry key, entry.value
    end
  else
    raise "You can't shovel something that's not a Hash or EavHash here!"
  end

  self
end

#[](key) ⇒ Object

Gets the value of an EAV attribute

Parameters:

  • key (String, Symbol)

32
33
34
35
36
37
# File 'lib/eav_hashes/eav_hash.rb', line 32

def [](key)
  raise "Key must be a string or a symbol!" unless key.is_a?(String) or key.is_a?(Symbol)
  load_entries_if_needed
  return @entries[key].value if @entries[key]
  nil
end

#[]=(key, value) ⇒ Object

Sets the value of the EAV attribute `key` to `value`

Parameters:

  • key (String, Symbol)

    the attribute

  • value (Object)

    the value


42
43
44
# File 'lib/eav_hashes/eav_hash.rb', line 42

def []=(key, value)
  update_or_create_entry key, value
end

#as_hashObject

Returns a hash with each entry key mapped to its actual value, not the internal EavEntry


109
110
111
112
113
114
115
116
117
# File 'lib/eav_hashes/eav_hash.rb', line 109

def as_hash
  load_entries_if_needed
  hsh = {}
  @entries.each do |k, entry|
    hsh[k] = entry.value
  end

  hsh
end

#clearObject

Empties the hash by setting all the values to nil (without committing them, of course)


100
101
102
103
104
105
# File 'lib/eav_hashes/eav_hash.rb', line 100

def clear
  load_entries_if_needed
  @entries.each do |_, entry|
    entry.value = nil
  end
end

#each(&block) ⇒ Object

Emulates Hash.each


89
90
91
# File 'lib/eav_hashes/eav_hash.rb', line 89

def each (&block)
  as_hash.each &block
end

#each_pair(&block) ⇒ Object

Emulates Hash.each_pair (same as each)


94
95
96
# File 'lib/eav_hashes/eav_hash.rb', line 94

def each_pair (&block)
  each &block
end

#entriesObject

Gets the raw hash containing EavEntries by their keys


66
67
68
# File 'lib/eav_hashes/eav_hash.rb', line 66

def entries
  load_entries_if_needed
end

#inspectObject

Take the crap out of #inspect calls


120
121
122
# File 'lib/eav_hashes/eav_hash.rb', line 120

def inspect
  as_hash
end

#keysObject

Gets the keys this EavHash manages


83
84
85
86
# File 'lib/eav_hashes/eav_hash.rb', line 83

def keys
  load_entries_if_needed
  @entries.keys
end

#save_entriesObject

Saves any modified entries and deletes any which have been nil'd to save DB space


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/eav_hashes/eav_hash.rb', line 16

def save_entries
  # The entries are lazy-loaded, so don't do anything if they haven't been accessed or modified
  return unless (@entries and @changes_made)

  @entries.values.each do |entry|
    if entry.value.nil?
      entry.delete
    else
      set_entry_owner(entry)
      entry.save
    end
  end
end

#valuesObject

Gets the actual values this EavHash contains


71
72
73
74
75
76
77
78
79
80
# File 'lib/eav_hashes/eav_hash.rb', line 71

def values
  load_entries_if_needed

  ret = []
  @entries.values.each do |value|
    ret << value
  end

  ret
end