Class: Neo4j::ActiveNode::HasN::AssociationProxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/neo4j/active_node/has_n.rb

Overview

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

Constant Summary collapse

QUERY_PROXY_METHODS =
[:<<, :delete, :create, :pluck, :where, :where_not, :rel_where, :rel_order, :order, :skip, :limit]
CACHED_RESULT_METHODS =
[]

Instance Method Summary collapse

Constructor Details

#initialize(query_proxy, deferred_objects = [], result_cache_proc = nil) ⇒ AssociationProxy

Returns a new instance of AssociationProxy.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/neo4j/active_node/has_n.rb', line 11

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

  @result_cache_proc = result_cache_proc

  # 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/neo4j/active_node/has_n.rb', line 122

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 target.is_a?(Neo4j::ActiveNode::Query::QueryProxy)

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

Instance Method Details

#+(other) ⇒ Object



48
49
50
# File 'lib/neo4j/active_node/has_n.rb', line 48

def +(other)
  self.to_a + other
end

#==(other) ⇒ Object



44
45
46
# File 'lib/neo4j/active_node/has_n.rb', line 44

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

#add_to_cache(object) ⇒ Object



85
86
87
88
# File 'lib/neo4j/active_node/has_n.rb', line 85

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

#cache_query_proxy_resultObject



90
91
92
# File 'lib/neo4j/active_node/has_n.rb', line 90

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

#cache_result(result) ⇒ Object



80
81
82
83
# File 'lib/neo4j/active_node/has_n.rb', line 80

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

#cached?Boolean

Returns:



102
103
104
# File 'lib/neo4j/active_node/has_n.rb', line 102

def cached?
  !!@cached_result
end

#clear_cache_resultObject



98
99
100
# File 'lib/neo4j/active_node/has_n.rb', line 98

def clear_cache_result
  cache_result(nil)
end

#each(&block) ⇒ Object



40
41
42
# File 'lib/neo4j/active_node/has_n.rb', line 40

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

#inspectObject

States: Default



25
26
27
28
29
30
31
# File 'lib/neo4j/active_node/has_n.rb', line 25

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

#replace_with(*args) ⇒ Object



106
107
108
109
110
# File 'lib/neo4j/active_node/has_n.rb', line 106

def replace_with(*args)
  @cached_result = nil

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

#resultObject



52
53
54
# File 'lib/neo4j/active_node/has_n.rb', line 52

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

#result_cache_proc_cacheObject



94
95
96
# File 'lib/neo4j/active_node/has_n.rb', line 94

def result_cache_proc_cache
  @result_cache_proc_cache ||= @result_cache_proc.call if @result_cache_proc
end

#result_idsObject



74
75
76
77
78
# File 'lib/neo4j/active_node/has_n.rb', line 74

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

#result_nodesObject



62
63
64
65
66
67
68
# File 'lib/neo4j/active_node/has_n.rb', line 62

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_objectsObject



70
71
72
# File 'lib/neo4j/active_node/has_n.rb', line 70

def result_objects
  @deferred_objects + result_without_deferred
end

#result_without_deferredObject



56
57
58
59
60
# File 'lib/neo4j/active_node/has_n.rb', line 56

def result_without_deferred
  cache_query_proxy_result if !@cached_result

  @cached_result
end

#serializable_hash(options = {}) ⇒ Object



132
133
134
# File 'lib/neo4j/active_node/has_n.rb', line 132

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