AssociationProxy

Return this object from associations It uses a QueryProxy to get results But also caches results and can have results cached on it

Constants

  • QUERY_PROXY_METHODS
  • CACHED_RESULT_METHODS

Methods

#+

def +(other)
  self.to_a + other
end

#==

def ==(other)
  self.to_a == other.to_a
end

#add_to_cache

def add_to_cache(object)
  @cached_result ||= []
  @cached_result << object
end

#cache_query_proxy_result

def cache_query_proxy_result
  @query_proxy.to_a.tap { |result| cache_result(result) }
end

#cache_result

def cache_result(result)
  @cached_result = result
  @enumerable = (@cached_result || @query_proxy)
end

#cached?

def cached?
  !!@cached_result
end

#clear_cache_result

def clear_cache_result
  cache_result(nil)
end

#each

def each(&block)
  result_nodes.each(&block)
end

#initialize

def initialize(query_proxy, deferred_objects = [], cached_result = nil)
  @query_proxy = query_proxy
  @deferred_objects = deferred_objects

  cache_result(cached_result)

  # Represents the thing which can be enumerated
  # default to @query_proxy, but will be set to
  # @cached_result if that is set
  @enumerable = @query_proxy
end
#inspect

States: Default

def inspect
  if @cached_result
    result_nodes.inspect
  else
    "#<AssociationProxy @query_proxy=#{@query_proxy.inspect}>"
  end
end

#method_missing

def method_missing(method_name, *args, &block)
  target = target_for_missing_method(method_name)
  super if target.nil?

  cache_query_proxy_result if !cached? && !target.is_a?(Neo4j::ActiveNode::Query::QueryProxy)
  clear_cache_result if !QUERY_PROXY_METHODS.include?(method_name) && target.is_a?(Neo4j::ActiveNode::Query::QueryProxy)

  target.public_send(method_name, *args, &block)
end

#replace_with

def replace_with(*args)
  @cached_result = nil

  @query_proxy.public_send(:replace_with, *args)
end

#result

def result
  (@deferred_objects || []) + result_without_deferred
end

#result_ids

def result_ids
  result.map do |object|
    object.is_a?(Neo4j::ActiveNode) ? object.id : object
  end
end

#result_nodes

def result_nodes
  return result_objects if !@query_proxy.model

  result_objects.map do |object|
    object.is_a?(Neo4j::ActiveNode) ? object : @query_proxy.model.find(object)
  end
end

#result_objects

def result_objects
  @deferred_objects + result_without_deferred
end

#result_without_deferred

def result_without_deferred
  cache_query_proxy_result if !@cached_result

  @cached_result
end

#serializable_hash

def serializable_hash(options = {})
  to_a.map { |record| record.serializable_hash(options) }
end