Skip to content

Commit

Permalink
Keymaker::Node.find_all_by_cypher
Browse files Browse the repository at this point in the history
- it now returns instances of the "including" class
- add test coverage
  • Loading branch information
therubymug committed Oct 19, 2012
1 parent e2a1486 commit 1f7211a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
23 changes: 15 additions & 8 deletions lib/keymaker/node.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/keymaker/serialization.rb
Expand Up @@ -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])
Expand Down
23 changes: 21 additions & 2 deletions spec/keymaker/node_spec.rb
Expand Up @@ -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

Expand All @@ -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) }
Expand All @@ -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

Expand Down

0 comments on commit 1f7211a

Please sign in to comment.