class ActiveGraph::Node::HasN::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
- CACHED_RESULT_METHODS
- QUERY_PROXY_METHODS
Public Class Methods
new(query_proxy, deferred_objects = [], result_cache_proc = nil)
click to toggle source
# File lib/active_graph/node/has_n.rb 11 def initialize(query_proxy, deferred_objects = [], result_cache_proc = nil) 12 @query_proxy = query_proxy 13 @deferred_objects = deferred_objects 14 15 @result_cache_proc = result_cache_proc 16 17 # Represents the thing which can be enumerated 18 # default to @query_proxy, but will be set to 19 # @cached_result if that is set 20 @enumerable = @query_proxy 21 end
Public Instance Methods
+(other)
click to toggle source
# File lib/active_graph/node/has_n.rb 64 def +(other) 65 self.to_a + other 66 end
==(other)
click to toggle source
# File lib/active_graph/node/has_n.rb 60 def ==(other) 61 self.to_a == other.to_a 62 end
add_to_cache(object, rel = nil)
click to toggle source
# File lib/active_graph/node/has_n.rb 104 def add_to_cache(object, rel = nil) 105 (@cached_rels ||= []) << rel if rel 106 (@cached_result ||= []).tap { |results| results << object unless results.include?(object) } 107 end
cache_query_proxy_result()
click to toggle source
# File lib/active_graph/node/has_n.rb 113 def cache_query_proxy_result 114 (result_cache_proc_cache || @query_proxy).to_a.tap { |result| cache_result(result) } 115 end
cache_result(result)
click to toggle source
# File lib/active_graph/node/has_n.rb 94 def cache_result(result) 95 @cached_result = result 96 @enumerable = (@cached_result || @query_proxy) 97 end
cached?()
click to toggle source
# File lib/active_graph/node/has_n.rb 125 def cached? 126 !!@cached_result 127 end
clear_cache_result()
click to toggle source
# File lib/active_graph/node/has_n.rb 121 def clear_cache_result 122 cache_result(nil) 123 end
each(&block)
click to toggle source
# File lib/active_graph/node/has_n.rb 37 def each(&block) 38 result_nodes.each(&block) 39 end
each_rel(&block)
click to toggle source
# File lib/active_graph/node/has_n.rb 41 def each_rel(&block) 42 rels.each(&block) 43 end
empty?(*args)
click to toggle source
# File lib/active_graph/node/has_n.rb 56 def empty?(*args) 57 @deferred_objects.empty? && @enumerable.empty?(*args) 58 end
init_cache()
click to toggle source
# File lib/active_graph/node/has_n.rb 99 def init_cache 100 @cached_rels ||= [] 101 @cached_result ||= [] 102 end
inspect()
click to toggle source
States: Default
# File lib/active_graph/node/has_n.rb 25 def inspect 26 formatted_nodes = ::ActiveGraph::Node::NodeListFormatter.new(result_nodes) 27 "#<AssociationProxy #{@query_proxy.context} #{formatted_nodes.inspect}>" 28 end
length()
click to toggle source
# File lib/active_graph/node/has_n.rb 48 def length 49 @deferred_objects.length + @enumerable.length 50 end
method_missing(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/active_graph/node/has_n.rb 150 def method_missing(method_name, *args, &block) 151 target = target_for_missing_method(method_name) 152 super if target.nil? 153 154 cache_query_proxy_result if !cached? && !target.is_a?(ActiveGraph::Node::Query::QueryProxy) 155 clear_cache_result if target.is_a?(ActiveGraph::Node::Query::QueryProxy) 156 157 target.public_send(method_name, *args, &block) 158 end
rels()
click to toggle source
Calls superclass method
# File lib/active_graph/node/has_n.rb 109 def rels 110 @cached_rels || super 111 end
replace_with(*args)
click to toggle source
# File lib/active_graph/node/has_n.rb 129 def replace_with(*args) 130 nodes = @query_proxy.replace_with(*args).to_a 131 if @query_proxy.start_object.try(:new_record?) 132 @cached_result = nil 133 else 134 cache_result(nodes) 135 end 136 end
result()
click to toggle source
# File lib/active_graph/node/has_n.rb 68 def result 69 (@deferred_objects || []) + result_without_deferred 70 end
result_cache_proc_cache()
click to toggle source
# File lib/active_graph/node/has_n.rb 117 def result_cache_proc_cache 118 @result_cache_proc_cache ||= @result_cache_proc.call if @result_cache_proc 119 end
result_ids()
click to toggle source
# File lib/active_graph/node/has_n.rb 88 def result_ids 89 result.map do |object| 90 object.is_a?(ActiveGraph::Node) ? object.id : object 91 end 92 end
result_nodes()
click to toggle source
# File lib/active_graph/node/has_n.rb 78 def result_nodes 79 return result_objects if !@query_proxy.model 80 81 map_results_as_nodes(result_objects) 82 end
result_objects()
click to toggle source
# File lib/active_graph/node/has_n.rb 84 def result_objects 85 @deferred_objects + result_without_deferred 86 end
result_without_deferred()
click to toggle source
# File lib/active_graph/node/has_n.rb 72 def result_without_deferred 73 cache_query_proxy_result if !@cached_result 74 75 @cached_result 76 end
serializable_hash(options = {})
click to toggle source
# File lib/active_graph/node/has_n.rb 160 def serializable_hash(options = {}) 161 to_a.map { |record| record.serializable_hash(options) } 162 end
size()
click to toggle source
# File lib/active_graph/node/has_n.rb 52 def size 53 @deferred_objects.size + @enumerable.size 54 end
Private Instance Methods
map_results_as_nodes(result)
click to toggle source
# File lib/active_graph/node/has_n.rb 166 def map_results_as_nodes(result) 167 result.map do |object| 168 object.is_a?(ActiveGraph::Node) ? object : @query_proxy.model.find(object) 169 end 170 end
target_for_missing_method(method_name)
click to toggle source
# File lib/active_graph/node/has_n.rb 172 def target_for_missing_method(method_name) 173 case method_name 174 when *CACHED_RESULT_METHODS 175 @cached_result 176 else 177 if @cached_result && @cached_result.respond_to?(method_name) 178 @cached_result 179 elsif @query_proxy.respond_to?(method_name) 180 @query_proxy 181 end 182 end 183 end