Module: CouchbaseOrm::Associations::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#associationsObject



129
130
131
# File 'lib/couchbase-orm/associations.rb', line 129

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
# 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
            raise CouchbaseOrm::StrictLoadingViolationError, "#{self.class} is marked as strict_loading and #{assoc} cannot be lazily loaded." if strict_loading?

            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, "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
end

#define_non_cyclic_method(name, &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/couchbase-orm/associations.rb', line 133

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



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

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 options[:polymorphic]
            ::CouchbaseOrm.try_load(ref_value) if ref_value
        else
            raise CouchbaseOrm::StrictLoadingViolationError, "#{self.class} is marked as strict_loading and #{assoc} cannot be lazily loaded." if strict_loading?

            assoc.constantize.find(ref_value) if ref_value
        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, "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

    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