module Neo4j::ActiveNode::HasN

Public Instance Methods

association_proxy(name, options = {}) click to toggle source
    # File lib/neo4j/active_node/has_n.rb
178 def association_proxy(name, options = {})
179   name = name.to_sym
180   hash = association_proxy_hash(name, options)
181   association_proxy_cache_fetch(hash) do
182     if result_cache = self.instance_variable_get('@source_proxy_result_cache')
183       cache = nil
184       result_cache.inject(nil) do |proxy_to_return, object|
185         proxy = fresh_association_proxy(name, options.merge(start_object: object),
186                                         proc { (cache ||= previous_proxy_results_by_previous_id(result_cache, name))[object.neo_id] })
187 
188         object.association_proxy_cache[hash] = proxy
189 
190         (self == object ? proxy : proxy_to_return)
191       end
192     else
193       fresh_association_proxy(name, options)
194     end
195   end
196 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/neo4j/active_node/has_n.rb
159 def association_proxy_cache
160   @association_proxy_cache ||= {}
161 end
association_proxy_cache_fetch(key) { || ... } click to toggle source
    # File lib/neo4j/active_node/has_n.rb
163 def association_proxy_cache_fetch(key)
164   association_proxy_cache.fetch(key) do
165     value = yield
166     association_proxy_cache[key] = value
167   end
168 end
association_proxy_hash(name, options = {}) click to toggle source
    # File lib/neo4j/active_node/has_n.rb
174 def association_proxy_hash(name, options = {})
175   [name.to_sym, options.values_at(:node, :rel, :labels, :rel_length)].hash
176 end
association_query_proxy(name, options = {}) click to toggle source
    # File lib/neo4j/active_node/has_n.rb
170 def association_query_proxy(name, options = {})
171   self.class.send(:association_query_proxy, name, {start_object: self}.merge!(options))
172 end

Private Instance Methods

fresh_association_proxy(name, options = {}, result_cache_proc = nil) click to toggle source
    # File lib/neo4j/active_node/has_n.rb
200 def fresh_association_proxy(name, options = {}, result_cache_proc = nil)
201   AssociationProxy.new(association_query_proxy(name, options), deferred_nodes_for_association(name), result_cache_proc)
202 end
previous_proxy_results_by_previous_id(result_cache, association_name) click to toggle source
    # File lib/neo4j/active_node/has_n.rb
204 def previous_proxy_results_by_previous_id(result_cache, association_name)
205   query_proxy = self.class.as(:previous).where(neo_id: result_cache.map(&:neo_id))
206   query_proxy = self.class.send(:association_query_proxy, association_name, previous_query_proxy: query_proxy, node: :next, optional: true)
207 
208   Hash[*query_proxy.pluck('ID(previous)', 'collect(next)').flatten(1)].each do |_, records|
209     records.each do |record|
210       record.instance_variable_set('@source_proxy_result_cache', records)
211     end
212   end
213 end