Module: SimpleHstoreAccessor

Defined in:
lib/simple_hstore_accessor.rb,
lib/simple_hstore_accessor/version.rb

Constant Summary collapse

VERSION =
'0.3.1'

Instance Method Summary collapse

Instance Method Details

#store_accessor(hstore_attribute, *keys) ⇒ Object

Public: Rails4-like method which defines simple accessors for hstore fields

hstore_attribute - your Hstore column keys - Array of fields in your hstore

Example

class Person < ActiveRecord::Base
  store_accessor :favorites_info, :book, :color
end

Returns nothing



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/simple_hstore_accessor.rb', line 19

def store_accessor(hstore_attribute, *keys)
  unless serialized_attributes.key?(hstore_attribute.to_s)
    if defined?(ActiveRecord::Coders::Hstore)
      serialize hstore_attribute, ActiveRecord::Coders::Hstore
    end
  end

  accessor_keys = Array(keys).flatten.map(&:to_sym)

  accessor_keys.each do |key|
    define_method("#{key}=") do |value|
      send("#{hstore_attribute}_will_change!")
      send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(key.to_s => value))
    end
    define_method(key) do
      send(hstore_attribute) && send(hstore_attribute)[key.to_s]
    end
    define_method("#{key}_will_change!") { attribute_will_change!(key.to_s) }
    define_method("#{key}_changed?") { attribute_changed?(key.to_s) }
  end

  define_method :write_attribute do |attr_name, value|
    if accessor_keys.include?(attr_name.to_sym)
      public_send("#{attr_name}=", value)
    else
      super(attr_name, value)
    end
  end

  define_method :read_attribute do |attr_name|
    if accessor_keys.include?(attr_name.to_sym)
      public_send(attr_name)
    else
      super(attr_name)
    end
  end

  define_method("#{hstore_attribute}=") do |new_properties|
    current_properties = send(hstore_attribute) || {}

    (current_properties.keys | new_properties.keys).each do |key|
      next if current_properties[key].to_s == new_properties[key].to_s || !respond_to?("#{key}_will_change!")
      send("#{key}_will_change!")
    end

    super(new_properties)
  end
end