Module: Couchbase::Model::ComplexAttributes::ClassMethods

Defined in:
lib/couchbase/model/complex_attributes.rb

Instance Method Summary collapse

Instance Method Details

#array_attribute(*names) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/couchbase/model/complex_attributes.rb', line 7

def array_attribute(*names)
  options = names.extract_options!
  class_name = options.delete(:class_name)

  names.each do |name|
    name = name.to_s

    (@_array_attributes ||= {})[name] = class_name

    attribute name, {default: proc { [] }}.merge(options)

    define_method("#{name}=") do |values|
      actual_values = values.map do |value|
        if value.is_a?(String) && value =~ /json_class/
          JSON.load value
        else
          value
        end
      end

      write_attribute name, actual_values
    end
  end
end

#array_attribute_class(name) ⇒ Object



32
33
34
# File 'lib/couchbase/model/complex_attributes.rb', line 32

def array_attribute_class(name)
  @_array_attributes[name.to_s]
end