lib/neo4j/active_node/has_n.rb in neo4j-5.2.15 vs lib/neo4j/active_node/has_n.rb in neo4j-6.0.0.alpha.1
- old
+ new
@@ -20,11 +20,11 @@
# States:
# Default
def inspect
if @cached_result
- @cached_result.inspect
+ result_nodes.inspect
else
"#<AssociationProxy @query_proxy=#{@query_proxy.inspect}>"
end
end
@@ -34,26 +34,53 @@
end
include Enumerable
def each(&block)
- result.each(&block)
+ result_nodes.each(&block)
end
+ def ==(other)
+ self.to_a == other.to_a
+ end
+
+ def +(other)
+ self.to_a + other
+ end
+
def result
return @cached_result if @cached_result
cache_query_proxy_result
@cached_result
end
+ def result_nodes
+ return result if !@query_proxy.model
+
+ @cached_result = result.map do |object|
+ object.is_a?(Neo4j::ActiveNode) ? object : @query_proxy.model.find(object)
+ end
+ end
+
+ def result_ids
+ result.map do |object|
+ object.is_a?(Neo4j::ActiveNode) ? object.id : object
+ end
+ end
+
def cache_result(result)
@cached_result = result
@enumerable = (@cached_result || @query_proxy)
end
+ def add_to_cache(object)
+ @cached_result ||= []
+ @cached_result << object
+ end
+
def cache_query_proxy_result
@query_proxy.to_a.tap do |result|
cache_result(result)
end
end
@@ -72,11 +99,11 @@
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)
+ 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
def serializable_hash(options = {})
@@ -121,13 +148,17 @@
def association_query_proxy(name, options = {})
self.class.send(:association_query_proxy, name, {start_object: self}.merge!(options))
end
+ def association_proxy_hash(name, options = {})
+ [name.to_sym, options.values_at(:node, :rel, :labels, :rel_length)].hash
+ end
+
def association_proxy(name, options = {})
name = name.to_sym
- hash = [name, options.values_at(:node, :rel, :labels, :rel_length)].hash
+ hash = association_proxy_hash(name, options)
association_proxy_cache_fetch(hash) do
if result_cache = self.instance_variable_get('@source_query_proxy_result_cache')
result_by_previous_id = previous_proxy_results_by_previous_id(result_cache, name)
result_cache.inject(nil) do |proxy_to_return, object|
@@ -299,22 +330,23 @@
def define_has_many_methods(name)
define_method(name) do |node = nil, rel = nil, options = {}|
# return [].freeze unless self._persisted_obj
- options, node = node, nil if node.is_a?(Hash)
+ if node.is_a?(Hash)
+ options = node
+ node = nil
+ end
association_proxy(name, {node: node, rel: rel, source_object: self, labels: options[:labels]}.merge!(options))
end
define_has_many_setter(name)
define_has_many_id_methods(name)
define_class_method(name) do |node = nil, rel = nil, options = {}|
- options, node = node, nil if node.is_a?(Hash)
-
association_proxy(name, {node: node, rel: rel, labels: options[:labels]}.merge!(options))
end
end
def define_has_many_setter(name)
@@ -325,11 +357,11 @@
end
end
def define_has_many_id_methods(name)
define_method_unless_defined("#{name.to_s.singularize}_ids") do
- association_proxy(name).pluck(:uuid)
+ association_proxy(name).result_ids
end
define_method_unless_defined("#{name.to_s.singularize}_ids=") do |ids|
association_proxy(name).replace_with(ids)
end
@@ -338,53 +370,52 @@
association_proxy(name).pluck(:neo_id)
end
end
def define_method_unless_defined(method_name, &block)
- define_method(method_name, block) unless method_defined?(method_name)
+ define_method(method_name, block) unless respond_to?(method_name)
end
def define_has_one_methods(name)
define_has_one_getter(name)
define_has_one_setter(name)
define_has_one_id_methods(name)
define_class_method(name) do |node = nil, rel = nil, options = {}|
- options, node = node, nil if node.is_a?(Hash)
-
association_proxy(name, {node: node, rel: rel, labels: options[:labels]}.merge!(options))
end
end
def define_has_one_id_methods(name)
- define_method_unless_defined("#{name}_id") do
- association_proxy(name).pluck(:uuid).first
+ define_method("#{name}_id") do
+ association_proxy(name).result_ids.first
end
define_method_unless_defined("#{name}_id=") do |id|
association_proxy(name).replace_with(id)
end
- define_method_unless_defined("#{name}_neo_id") do
+ define_method("#{name}_neo_id") do
association_proxy(name).pluck(:neo_id).first
end
end
def define_has_one_getter(name)
define_method(name) do |node = nil, rel = nil, options = {}|
- return nil unless self._persisted_obj
+ if node.is_a?(Hash)
+ options = node
+ node = nil
+ end
- options, node = node, nil if node.is_a?(Hash)
-
# Return all results if a variable-length relationship length was given
- results = association_proxy(name, {node: node, rel: rel}.merge!(options))
+ association_proxy = association_proxy(name, {node: node, rel: rel}.merge!(options))
if options[:rel_length] && !options[:rel_length].is_a?(Fixnum)
- results
+ association_proxy
else
- results.result.first
+ association_proxy.result_nodes.first
end
end
end
def define_has_one_setter(name)
@@ -393,10 +424,10 @@
other_node.save if other_node.respond_to?(:persisted?) && !other_node.persisted?
association_proxy_cache.clear # TODO: Should probably just clear for this association...
Neo4j::Transaction.run { association_proxy(name).replace_with(other_node) }
# handle_non_persisted_node(other_node)
else
- association_proxy(name).defer_create(other_node, {}, :'=')
+ association_proxy(name).defer_create(other_node)
end
end
end
def define_class_method(*args, &block)