Module: StoreField::ClassMethods

Defined in:
lib/store_field.rb

Instance Method Summary collapse

Instance Method Details

#store_field(key, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/store_field.rb', line 9

def store_field(key, options = {})
  raise ArgumentError, ':in is invalid'     if options[:in] and serialized_attributes[options[:in].to_s].nil?
  raise ArgumentError, ':type is invalid'   if options[:type] and ![ Hash, Set ].include?(options[:type])

  klass = options[:type] || Hash
  store_attribute = options[:in] || serialized_attributes.keys.first
  raise ScriptError, 'store method must be defined before store_field' if store_attribute.nil?

  # Accessor
  define_method(key) do
    value = send(store_attribute)[key]
    if value.nil?
      value = klass.new
      send(store_attribute)[key] = value
    end
    value
  end

  # Utility methods for Hash
  if klass == Hash and options[:keys]
    options[:keys].each do |subkey|
      define_method("#{key}_#{subkey}") do
        send(key)[subkey]
      end

      define_method("#{key}_#{subkey}=") do |value|
        send(key)[subkey] = value
      end
    end
  end

  # Utility methods for Set
  if klass == Set
    define_method("set_#{key}")     {|value| send(key).add(value); self }
    define_method("unset_#{key}")   {|value| send(key).delete(value); self }
    define_method("set_#{key}?")    {|value| send(key).include?(value) }
    define_method("unset_#{key}?")  {|value| !send(key).include?(value) }

    if options[:values]
      validate do
        diff = send(key).to_a - options[:values]
        unless diff.empty?
          errors.add(key, "is invalid with #{diff.inspect}")
        end
      end
    end
  end
end