Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Rogelio J. Samour authored
23 lib/keymaker/node.rb
@@ -19,7 +19,7 @@ def self.included(base)
19 19 extend Keymaker::Node::ClassMethods
20 20 include Keymaker::Node::InstanceMethods
21 21
22   - attr_writer :new_node
  22 + attr_accessor :new_node
23 23 attr_protected :created_at, :updated_at
24 24 end
25 25
@@ -62,7 +62,7 @@ def find_by_cypher(query, params={})
62 62 end
63 63
64 64 def find_all_by_cypher(query, params={})
65   - neo_service.execute_cypher(query, params)
  65 + neo_service.execute_cypher(query, params).map{ |node| wrap(node) }
66 66 end
67 67
68 68 def find(node_id)
@@ -75,21 +75,28 @@ def find(node_id)
75 75 end
76 76 end
77 77
  78 + def wrap(node_attrs)
  79 + new(node_attrs).tap do |node|
  80 + node.new_node = false
  81 + node.process_attrs(node_attrs) if node_attrs.present?
  82 + end
  83 + end
  84 +
78 85 end
79 86
80 87 module InstanceMethods
81 88
82 89 def initialize(attrs = {})
83   - @new_node = true
  90 + self.new_node = true
84 91 process_attrs(attrs) if attrs.present?
85 92 end
86 93
87   - def neo_service
88   - self.class.neo_service
  94 + def new_node?
  95 + new_node
89 96 end
90 97
91   - def new?
92   - @new_node
  98 + def neo_service
  99 + self.class.neo_service
93 100 end
94 101
95 102 def sanitize(attrs)
@@ -102,7 +109,7 @@ def save
102 109
103 110 def create_or_update
104 111 run_callbacks :save do
105   - new? ? create : update(attributes)
  112 + new_node? ? create : update(attributes)
106 113 end
107 114 end
108 115
2  lib/keymaker/serialization.rb
@@ -20,7 +20,7 @@ def self.included(base)
20 20 end
21 21
22 22 def process_attrs(attrs)
23   - attrs.symbolize_keys!
  23 + attrs = attrs.symbolize_keys
24 24 self.class.properties.delete_if{|p| p == :node_id}.each do |property|
25 25 if property == :active_record_id
26 26 process_attr(property, attrs[:id].present? ? attrs[:id] : attrs[:active_record_id])
23 spec/keymaker/node_spec.rb
@@ -17,7 +17,7 @@ class Terminator
17 17 subject { terminator }
18 18 its(:node_id) { should be_present }
19 19 its(:name) { should == 'T1000' }
20   - it { should_not be_new }
  20 + it { should_not be_new_node }
21 21 it { should be_a(Terminator) }
22 22 end
23 23
@@ -30,6 +30,25 @@ class Terminator
30 30 end
31 31 end
32 32
  33 + describe ".find_all_by_cypher(query, params)" do
  34 +
  35 + subject { Terminator.find_all_by_cypher(query) }
  36 + let(:query) { "START all=node(*) RETURN all" }
  37 +
  38 + context "with existing nodes" do
  39 + before { terminator }
  40 + it { should be_a(Array) }
  41 + its(:first) { should_not be_new_node }
  42 + its(:first) { should be_a(Terminator) }
  43 + end
  44 +
  45 + context "without existing nodes" do
  46 + it { should be_a(Array) }
  47 + it { should be_blank }
  48 + end
  49 +
  50 + end
  51 +
33 52 describe ".find(node_id)" do
34 53
35 54 subject { Terminator.find(node_id) }
@@ -40,7 +59,7 @@ class Terminator
40 59 its(:node_id) { should be_present }
41 60 its(:name) { should == 'T1000' }
42 61 it { should be_present }
43   - it { should_not be_new }
  62 + it { should_not be_new_node }
44 63 it { should be_a(Terminator) }
45 64 end
46 65

0 comments on commit 1f7211a

Please sign in to comment.
Something went wrong with that request. Please try again.