Module: ActiveGraph::Node::HasN

Extended by:
ActiveSupport::Concern
Included in:
ActiveGraph::Node
Defined in:
lib/active_graph/node/has_n.rb,
lib/active_graph/node/has_n/association.rb,
lib/active_graph/node/has_n/association/rel_factory.rb,
lib/active_graph/node/has_n/association_cypher_methods.rb

Defined Under Namespace

Modules: AssociationCypherMethods, ClassMethods Classes: Association, AssociationProxy, HasOneConstraintError, NonPersistedNodeError

Instance Method Summary collapse

Instance Method Details

#association_proxy(name, options = {}) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/active_graph/node/has_n.rb', line 228

def association_proxy(name, options = {})
  name = name.to_sym
  hash = association_proxy_hash(name, options)
  association_proxy_cache_fetch(hash) do
    if result_cache = self.instance_variable_get('@source_proxy_result_cache')
      cache = nil
      result_cache.inject(nil) do |proxy_to_return, object|
        proxy = fresh_association_proxy(name, options.merge(start_object: object),
                                        proc { (cache ||= previous_proxy_results_by_previous_id(result_cache, name))[object.neo_id] })

        object.association_proxy_cache[hash] = proxy

        (self == object ? proxy : proxy_to_return)
      end
    else
      fresh_association_proxy(name, options)
    end
  end
end

#association_proxy_cacheObject

Returns the current AssociationProxy cache for the association cache. It is in the format { :association_name => AssociationProxy} This is so that we

  • don’t need to re-build the QueryProxy objects

  • also because the QueryProxy object caches it’s results

  • so we don’t need to query again

  • so that we can cache results from association calls or eager loading



209
210
211
# File 'lib/active_graph/node/has_n.rb', line 209

def association_proxy_cache
  @association_proxy_cache ||= {}
end

#association_proxy_cache_fetch(key) ⇒ Object



213
214
215
216
217
218
# File 'lib/active_graph/node/has_n.rb', line 213

def association_proxy_cache_fetch(key)
  association_proxy_cache.fetch(key) do
    value = yield
    association_proxy_cache[key] = value
  end
end

#association_proxy_hash(name, options = {}) ⇒ Object



224
225
226
# File 'lib/active_graph/node/has_n.rb', line 224

def association_proxy_hash(name, options = {})
  [name.to_sym, options.values_at(:node, :rel, :labels, :rel_length)].hash
end

#association_query_proxy(name, options = {}) ⇒ Object



220
221
222
# File 'lib/active_graph/node/has_n.rb', line 220

def association_query_proxy(name, options = {})
  self.class.send(:association_query_proxy, name, {start_object: self}.merge!(options))
end

#delete_has_one_rel!(rel) ⇒ Object



265
266
267
268
# File 'lib/active_graph/node/has_n.rb', line 265

def delete_has_one_rel!(rel)
  send("#{rel.name}", :n, :r, chainable: true).query.delete(:r).exec
  association_proxy_cache.clear
end

#delete_reverse_has_one_core_rel(association) ⇒ Object



248
249
250
251
# File 'lib/active_graph/node/has_n.rb', line 248

def delete_reverse_has_one_core_rel(association)
  reverse_assoc = reverse_association(association)
  delete_has_one_rel!(reverse_assoc) if reverse_assoc && reverse_assoc.type == :has_one
end

#delete_reverse_has_one_relationship(relationship, direction, other_node) ⇒ Object



260
261
262
263
# File 'lib/active_graph/node/has_n.rb', line 260

def delete_reverse_has_one_relationship(relationship, direction, other_node)
  rel = relationship_corresponding_rel(relationship, direction, other_node.class)
  delete_has_one_rel!(rel.last) if rel && rel.last.type == :has_one
end

#relationship_corresponding_rel(relationship, direction, target_class) ⇒ Object



270
271
272
273
274
275
276
277
# File 'lib/active_graph/node/has_n.rb', line 270

def relationship_corresponding_rel(relationship, direction, target_class)
  self.class.associations.find do |_key, assoc|
    assoc.direction == direction && (
      assoc.relationship_class_name == relationship.class.name ||
      (assoc.relationship_type == relationship.type.to_sym && assoc.target_class == target_class)
    )
  end
end

#reverse_association(association) ⇒ Object



253
254
255
256
257
258
# File 'lib/active_graph/node/has_n.rb', line 253

def reverse_association(association)
  reverse_assoc = self.class.associations.find do |_key, assoc|
    association.inverse_of?(assoc) || assoc.inverse_of?(association)
  end
  reverse_assoc && reverse_assoc.last
end