module ActiveGraph::Node::HasN

Public Instance Methods

association_proxy(name, options = {}) click to toggle source
    # File lib/active_graph/node/has_n.rb
212 def association_proxy(name, options = {})
213   name = name.to_sym
214   hash = association_proxy_hash(name, options)
215   association_proxy_cache_fetch(hash) do
216     if result_cache = self.instance_variable_get('@source_proxy_result_cache')
217       cache = nil
218       result_cache.inject(nil) do |proxy_to_return, object|
219         proxy = fresh_association_proxy(name, options.merge(start_object: object),
220                                         proc { (cache ||= previous_proxy_results_by_previous_id(result_cache, name))[object.neo_id] })
221 
222         object.association_proxy_cache[hash] = proxy
223 
224         (self == object ? proxy : proxy_to_return)
225       end
226     else
227       fresh_association_proxy(name, options)
228     end
229   end
230 end
association_proxy_cache() click to toggle source

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

    # File lib/active_graph/node/has_n.rb
193 def association_proxy_cache
194   @association_proxy_cache ||= {}
195 end
association_proxy_cache_fetch(key) { || ... } click to toggle source
    # File lib/active_graph/node/has_n.rb
197 def association_proxy_cache_fetch(key)
198   association_proxy_cache.fetch(key) do
199     value = yield
200     association_proxy_cache[key] = value
201   end
202 end
association_proxy_hash(name, options = {}) click to toggle source
    # File lib/active_graph/node/has_n.rb
208 def association_proxy_hash(name, options = {})
209   [name.to_sym, options.values_at(:node, :rel, :labels, :rel_length)].hash
210 end
association_query_proxy(name, options = {}) click to toggle source
    # File lib/active_graph/node/has_n.rb
204 def association_query_proxy(name, options = {})
205   self.class.send(:association_query_proxy, name, {start_object: self}.merge!(options))
206 end
delete_has_one_rel!(rel) click to toggle source
    # File lib/active_graph/node/has_n.rb
249 def delete_has_one_rel!(rel)
250   send("#{rel.name}", :n, :r, chainable: true).query.delete(:r).exec
251   association_proxy_cache.clear
252 end
delete_reverse_has_one_core_rel(association) click to toggle source
    # File lib/active_graph/node/has_n.rb
232 def delete_reverse_has_one_core_rel(association)
233   reverse_assoc = reverse_association(association)
234   delete_has_one_rel!(reverse_assoc) if reverse_assoc && reverse_assoc.type == :has_one
235 end
delete_reverse_has_one_relationship(relationship, direction, other_node) click to toggle source
    # File lib/active_graph/node/has_n.rb
244 def delete_reverse_has_one_relationship(relationship, direction, other_node)
245   rel = relationship_corresponding_rel(relationship, direction, other_node.class)
246   delete_has_one_rel!(rel.last) if rel && rel.last.type == :has_one
247 end
relationship_corresponding_rel(relationship, direction, target_class) click to toggle source
    # File lib/active_graph/node/has_n.rb
254 def relationship_corresponding_rel(relationship, direction, target_class)
255   self.class.associations.find do |_key, assoc|
256     assoc.relationship_class_name == relationship.class.name ||
257       (assoc.relationship_type == relationship.type.to_sym && assoc.target_class == target_class && assoc.direction == direction)
258   end
259 end
reverse_association(association) click to toggle source
    # File lib/active_graph/node/has_n.rb
237 def reverse_association(association)
238   reverse_assoc = self.class.associations.find do |_key, assoc|
239     association.inverse_of?(assoc) || assoc.inverse_of?(association)
240   end
241   reverse_assoc && reverse_assoc.last
242 end

Private Instance Methods

fresh_association_proxy(name, options = {}, result_cache_proc = nil) click to toggle source
    # File lib/active_graph/node/has_n.rb
263 def fresh_association_proxy(name, options = {}, result_cache_proc = nil)
264   AssociationProxy.new(association_query_proxy(name, options), deferred_nodes_for_association(name), result_cache_proc)
265 end
previous_proxy_results_by_previous_id(result_cache, association_name) click to toggle source
    # File lib/active_graph/node/has_n.rb
267 def previous_proxy_results_by_previous_id(result_cache, association_name)
268   query_proxy = self.class.as(:previous).where(neo_id: result_cache.map(&:neo_id))
269   query_proxy = self.class.send(:association_query_proxy, association_name, previous_query_proxy: query_proxy, node: :next, optional: true)
270 
271   Hash[*query_proxy.pluck('ID(previous)', 'collect(next)').flatten(1)].each_value do |records|
272     records.each do |record|
273       record.instance_variable_set('@source_proxy_result_cache', records)
274     end
275   end
276 end