Module: CouchbaseOrm::Associations::ClassMethods

Defined in:
lib/couchbase-orm/associations.rb

Instance Method Summary collapse

Instance Method Details

#associationsObject



138
139
140
# File 'lib/couchbase-orm/associations.rb', line 138

def associations
  @associations || []
end

#belongs_to(name, **options) ⇒ Object

Defines a belongs_to association for the model



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
57
58
59
60
61
62
# File 'lib/couchbase-orm/associations.rb', line 12

def belongs_to(name, **options)
  @associations ||= []
  @associations << [name.to_sym, options[:dependent]]

  ref = options[:foreign_key] || :"#{name}_id"
  ref_ass = :"#{ref}="
  instance_var = :"@__assoc_#{name}"

    # Class reference
  assoc = (options[:class_name] || name.to_s.camelize).to_s

    # Create the local setter / getter
  attribute(ref) { |value|
    remove_instance_variable(instance_var) if instance_variable_defined?(instance_var)
    value
  }

    # Define reader
  define_method(name) do
    return instance_variable_get(instance_var) if instance_variable_defined?(instance_var)

    val = if options[:polymorphic]
            ::CouchbaseOrm.try_load(self.send(ref))
          else
            assoc.constantize.find(self.send(ref), quiet: true)
          end
    instance_variable_set(instance_var, val)
    val
  end

    # Define writer
  attr_writer name

  define_method(:"#{name}=") do |value|
    if value
      if !options[:polymorphic]
        klass = assoc.constantize
        raise ArgumentError.new("type mismatch on association: #{klass.design_document} != #{value.class.design_document}") if klass.design_document != value.class.design_document
      end
      self.send(ref_ass, value.id)
    else
      self.send(ref_ass, nil)
    end

    instance_variable_set(instance_var, value)
  end

  define_method(:"#{name}_reset") do
    remove_instance_variable(instance_var) if instance_variable_defined?(instance_var)
  end
end

#define_non_cyclic_method(name, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/couchbase-orm/associations.rb', line 142

def define_non_cyclic_method(name, &block)
  return if method_defined?(name)

  define_method(name) do |*_args|
    result = true
    @_already_called ||= {}
      # Loop prevention for validation of associations
    unless @_already_called[name]
      begin
        @_already_called[name] = true
        result = instance_eval(&block)
      ensure
        @_already_called[name] = false
      end
    end
    result
  end
end

#has_and_belongs_to_many(name, **options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/couchbase-orm/associations.rb', line 64

def has_and_belongs_to_many(name, **options)
  @associations ||= []
  @associations << [name.to_sym, options[:dependent]]

  ref = options[:foreign_key] || :"#{name.to_s.singularize}_ids"
  ref_ass = :"#{ref}="
  instance_var = :"@__assoc_#{name}"

    # Class reference
  assoc = (options[:class_name] || name.to_s.singularize.camelize).to_s

    # Create the local setter / getter
  attribute(ref) { |value|
    remove_instance_variable(instance_var) if instance_variable_defined?(instance_var)
    value
  }

    # Define reader
  define_method(name) do
    return instance_variable_get(instance_var) if instance_variable_defined?(instance_var)

    ref_value = self.send(ref)
    ref_value = nil if ref_value.respond_to?(:empty?) && ref_value.empty?
    val = if ref_value.nil?
            nil
          elsif options[:polymorphic]
            ::CouchbaseOrm.try_load(ref_value)
          else
            assoc.constantize.find(ref_value, quiet: true)
          end
    val = Array.wrap(val || [])
    instance_variable_set(instance_var, val)
    val
  end

    # Define writer
  attr_writer name

  define_method(:"#{name}=") do |value|
    if value
      if !options[:polymorphic]
        klass = assoc.constantize
        value.each do |v|
          raise ArgumentError.new("type mismatch on association: #{klass.design_document} != #{v.class.design_document}") if klass.design_document != v.class.design_document
        end
      end
      self.send(ref_ass, value.map(&:id))
    else
      self.send(ref_ass, nil)
    end

    instance_variable_set(instance_var, value)
  end

  define_method(:"#{name}_reset") do
    self.remove_instance_variable(instance_var) if self.instance_variable_defined?(instance_var)
  end

  return unless options[:autosave]

  save_method = :"autosave_associated_records_for_#{name}"

  define_non_cyclic_method(save_method) do
    old, new = previous_changes[ref]
    adds = (new || []) - (old || [])
    subs = (old || []) - (new || [])
    update_has_and_belongs_to_many_reverse_association(assoc, adds, true, **options) if adds.any?
    update_has_and_belongs_to_many_reverse_association(assoc, subs, false, **options) if subs.any?
  end

  after_create save_method
  after_update save_method
end