architect4r 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/ReleaseNotes.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Release Notes
2
2
 
3
+ ## v0.4.3
4
+
5
+ * Allow retrieving multiple nodes with a single call to ```Node#find(id, id2, id3)```
6
+
3
7
  ## v0.4.2
4
8
 
5
9
  * Unify the finder interface and use ```Node#find(id)``` in favor of ```Node#find_by_id(id)```
@@ -16,15 +16,33 @@ module Architect4r
16
16
 
17
17
  # Fetch a record of the specified model based on its id
18
18
  #
19
- def find(id)
20
- data = connection.execute_cypher("start s=node(#{self.model_root.id}), d=node(#{id.to_i}) match s<-[r:model_type]-d return d")
21
- data &&= data['data'] && data['data'].flatten.first
22
- self.build_from_database(data)
19
+ def find(*ids)
20
+ # This code is taken from / inspired by activerecord:
21
+ # activerecord/lib/active_record/base.rb, line 1589
22
+ expects_array = ids.first.kind_of?(Array)
23
+ return ids.first if expects_array && ids.first.empty?
24
+
25
+ ids = ids.flatten.compact.uniq
26
+
27
+ case ids.size
28
+ when 0
29
+ raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} without an id!"))
30
+ when 1
31
+ id = ids.first.to_i
32
+ data = connection.execute_cypher("start s=node(#{self.model_root.id}), d=node(#{id}) match s<-[r:model_type]-d return d")
33
+ data &&= data['data'] && data['data'].flatten.first
34
+ result = self.build_from_database(data)
35
+
36
+ expects_array ? [ result ] : result
37
+ else
38
+ ids = ids.map { |i| i.to_i }.uniq.join(',')
39
+ find_by_cypher("start s=node(#{self.model_root.id}), d=node(#{ids}) match s<-[r:model_type]-d return d", 'd')
40
+ end
23
41
  end
24
42
  alias :find_by_id :find
25
43
 
26
44
  def find!(id)
27
- self.find_by_id(id) || raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} with id #{id}!"))
45
+ self.find(id) || raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} with id #{id}!"))
28
46
  end
29
47
  alias :find_by_id! :find!
30
48
 
@@ -1,3 +1,3 @@
1
1
  module Architect4r
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -36,6 +36,21 @@ describe "Node Queries" do
36
36
  lambda { Person.find_by_id!(person.id) }.should_not raise_error(Architect4r::RecordNotFound)
37
37
  end
38
38
 
39
+ it "should allow fetching multiple nodes by id" do
40
+ person1 = Person.create(:name => 'Agent 1', :human => false)
41
+ person2 = Person.create(:name => 'Agent 2', :human => false)
42
+
43
+ result = Person.find(person1.id, person2.id)
44
+ result.should have(2).persons
45
+ end
46
+
47
+ it "should return an array if the input was one" do
48
+ person = Person.create(:name => 'Agent X', :human => false)
49
+
50
+ result = Person.find([person.id])
51
+ result.should have(1).persons
52
+ end
53
+
39
54
  end
40
55
 
41
56
  describe "counting records" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: architect4r
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 2
10
- version: 0.4.2
9
+ - 3
10
+ version: 0.4.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Maximilian Schulz