Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Keymaker::Node.find_all_by_cypher

- it now returns instances of the "including" class
- add test coverage
  • Loading branch information...
commit 1f7211a496a959f88320524b75226b224720e5bf 1 parent e2a1486
@therubymug authored
View
23 lib/keymaker/node.rb
@@ -19,7 +19,7 @@ def self.included(base)
extend Keymaker::Node::ClassMethods
include Keymaker::Node::InstanceMethods
- attr_writer :new_node
+ attr_accessor :new_node
attr_protected :created_at, :updated_at
end
@@ -62,7 +62,7 @@ def find_by_cypher(query, params={})
end
def find_all_by_cypher(query, params={})
- neo_service.execute_cypher(query, params)
+ neo_service.execute_cypher(query, params).map{ |node| wrap(node) }
end
def find(node_id)
@@ -75,21 +75,28 @@ def find(node_id)
end
end
+ def wrap(node_attrs)
+ new(node_attrs).tap do |node|
+ node.new_node = false
+ node.process_attrs(node_attrs) if node_attrs.present?
+ end
+ end
+
end
module InstanceMethods
def initialize(attrs = {})
- @new_node = true
+ self.new_node = true
process_attrs(attrs) if attrs.present?
end
- def neo_service
- self.class.neo_service
+ def new_node?
+ new_node
end
- def new?
- @new_node
+ def neo_service
+ self.class.neo_service
end
def sanitize(attrs)
@@ -102,7 +109,7 @@ def save
def create_or_update
run_callbacks :save do
- new? ? create : update(attributes)
+ new_node? ? create : update(attributes)
end
end
View
2  lib/keymaker/serialization.rb
@@ -20,7 +20,7 @@ def self.included(base)
end
def process_attrs(attrs)
- attrs.symbolize_keys!
+ attrs = attrs.symbolize_keys
self.class.properties.delete_if{|p| p == :node_id}.each do |property|
if property == :active_record_id
process_attr(property, attrs[:id].present? ? attrs[:id] : attrs[:active_record_id])
View
23 spec/keymaker/node_spec.rb
@@ -17,7 +17,7 @@ class Terminator
subject { terminator }
its(:node_id) { should be_present }
its(:name) { should == 'T1000' }
- it { should_not be_new }
+ it { should_not be_new_node }
it { should be_a(Terminator) }
end
@@ -30,6 +30,25 @@ class Terminator
end
end
+ describe ".find_all_by_cypher(query, params)" do
+
+ subject { Terminator.find_all_by_cypher(query) }
+ let(:query) { "START all=node(*) RETURN all" }
+
+ context "with existing nodes" do
+ before { terminator }
+ it { should be_a(Array) }
+ its(:first) { should_not be_new_node }
+ its(:first) { should be_a(Terminator) }
+ end
+
+ context "without existing nodes" do
+ it { should be_a(Array) }
+ it { should be_blank }
+ end
+
+ end
+
describe ".find(node_id)" do
subject { Terminator.find(node_id) }
@@ -40,7 +59,7 @@ class Terminator
its(:node_id) { should be_present }
its(:name) { should == 'T1000' }
it { should be_present }
- it { should_not be_new }
+ it { should_not be_new_node }
it { should be_a(Terminator) }
end
Please sign in to comment.
Something went wrong with that request. Please try again.