neomirror 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91d4c19efa74a09c812dfbffc777ebb884f3289c
4
- data.tar.gz: 06e831911c25f4022c41038d5af73d1ca20085e6
3
+ metadata.gz: 9033be66b0aaf79a9d2f5b946e8e3a19dcc50270
4
+ data.tar.gz: 56b46cf350db86ff7a25fcc6b9f8d5376ac4cd41
5
5
  SHA512:
6
- metadata.gz: 171520bb5e991c0605a03d5adb5d8774e16cf31bc494c8f8e4beb635505af69038be2881831a84d78bd2b7b1610897d7d61a7f8956c1b05e0caeb59f9118fd2b
7
- data.tar.gz: 13de4c46626309fd61fc6046156a405909e9805a67039ae0a7f8804fe43d1d063338f9b50b904e2b5846e1bd93cf1738ad3b30dde3dcf8c3cecf8e6bf1dce8c9
6
+ metadata.gz: c00f6a547613e9bfecae74de9d892f2e20a6ff2d09be8ce961dfa5ff87fc657be2cdd8aca5370983a44fb2b2ef111bf44ddbc790a25a9c39e6051d8654baa1a8
7
+ data.tar.gz: d7ef2a0cad7a5cedffeed87095c219c7d3a4a66834b743d641862e1a804c457428a85cca48831c98a7d77ef3eca77cb862aba3fc5d9418b09d4b11f4473013ee
data/README.md CHANGED
@@ -163,7 +163,7 @@ class Staff < ActiveRecord::Base
163
163
  end
164
164
  ```
165
165
 
166
- Even possible reflect model as node (vertex) and relation(s) (edge).
166
+ Even possible reflect model as node (vertex) and relationship(s) (edge).
167
167
 
168
168
  ```ruby
169
169
  class Group < ActiveRecord::Base
@@ -19,7 +19,7 @@ module Neomirror::Relationship
19
19
 
20
20
  # Find declaration by partial options.
21
21
  def rel_mirror(p)
22
- return rel_mirrors.first unless p
22
+ return rel_mirrors.first unless p.present?
23
23
  rel_mirrors.find { |m| (!p[:start_node] || p[:start_node] == m[:start_node]) &&
24
24
  (!p[:end_node] || p[:end_node] == m[:end_node]) && (!p[:type] || p[:type] == m[:type]) }
25
25
  end
@@ -35,6 +35,7 @@ module Neomirror::Relationship
35
35
  m[:properties] = Neomirror::PropertyCollector.new(&block).properties if block_given?
36
36
  m[:if] = m[:if].to_proc if m[:if]
37
37
  m[:index_name] = "#{m[:start_node] == :self ? class_name.downcase : m[:start_node]}_#{m[:type]}_#{m[:end_node] == :self ? class_name.downcase : m[:end_node]}"
38
+ m[:complete] = true # indicates a completely filled rel mirror hash (not partial, that used for search)
38
39
  ::Neomirror.neo.create_relationship_index(m[:index_name])
39
40
  rel_mirrors << m
40
41
  end
@@ -46,59 +47,66 @@ module Neomirror::Relationship
46
47
  end
47
48
 
48
49
  def neo_relationship(partial_mirror = nil)
49
- find_neo_relationship(partial_mirror) || create_neo_relationship(partial_mirror)
50
+ raise "Couldn't find neo_relationship declaration" unless rel_mirror = self.class.rel_mirror(partial_mirror)
51
+ find_neo_relationship(rel_mirror) || create_neo_relationship(rel_mirror)
50
52
  end
51
53
  alias_method :neo_rel, :neo_relationship
52
54
 
53
- def neo_relationship_properties(partial_mirror = nil)
54
- raise "Couldn't find neo_relationship declaration" unless rel_mirror = self.class.rel_mirror(partial_mirror)
55
+ def neo_relationship_properties(rel_mirror = {})
56
+ raise "Couldn't find neo_relationship declaration" unless rel_mirror[:complete] || (rel_mirror = self.class.rel_mirror(rel_mirror))
55
57
  return nil unless rel_mirror[:properties]
56
58
  rel_mirror[:properties].reduce({}) { |hash, (property, rule)| hash[property] = rule.call(self); hash }
57
59
  end
58
60
 
59
- def neo_relationship_must_exist?(partial_mirror = nil)
60
- raise "Couldn't find neo_relationship declaration" unless rel_mirror = self.class.rel_mirror(partial_mirror)
61
+ def neo_relationship_must_exist?(rel_mirror = {})
62
+ raise "Couldn't find neo_relationship declaration" unless rel_mirror[:complete] || (rel_mirror = self.class.rel_mirror(rel_mirror))
61
63
  !rel_mirror[:if] || !!rel_mirror[:if].call(self)
62
64
  end
63
65
 
64
- def find_neo_relationship(partial_mirror = nil)
65
- raise "Couldn't find neo_relationship declaration" unless rel_mirror = self.class.rel_mirror(partial_mirror)
66
+ def find_neo_relationship(rel_mirror = {})
67
+ raise "Couldn't find neo_relationship declaration" unless rel_mirror[:complete] || (rel_mirror = self.class.rel_mirror(rel_mirror))
66
68
  return nil unless rel = ::Neomirror.neo.get_relationship_index(rel_mirror[:index_name], :id, self.__send__(self.class.neo_primary_key))
67
- @neo_rel = ::Neography::Relationship.load(rel, ::Neomirror.neo)
69
+ ::Neography::Relationship.load(rel, ::Neomirror.neo)
68
70
  end
69
71
 
70
- def create_neo_relationship(partial_mirror = nil)
71
- raise "Couldn't find neo_relationship declaration" unless rel_mirror = self.class.rel_mirror(partial_mirror)
72
+ def create_neo_relationship(rel_mirror = {})
73
+ raise "Couldn't find neo_relationship declaration" unless rel_mirror[:complete] || (rel_mirror = self.class.rel_mirror(rel_mirror))
72
74
  return nil unless (m1 = related_object(rel_mirror[:start_node])) && (m2 = related_object(rel_mirror[:end_node])) &&
73
75
  neo_relationship_must_exist?(rel_mirror)
74
- @neo_rel = ::Neography::Relationship.create(rel_mirror[:type], m1.neo_node, m2.neo_node, neo_relationship_properties(rel_mirror))
75
- ::Neomirror.neo.add_relationship_to_index(rel_mirror[:index_name], :id, self.__send__(self.class.neo_primary_key), @neo_rel)
76
- @neo_rel
76
+ neo_rel = ::Neography::Relationship.create(rel_mirror[:type], m1.neo_node, m2.neo_node, neo_relationship_properties(rel_mirror))
77
+ ::Neomirror.neo.add_relationship_to_index(rel_mirror[:index_name], :id, self.__send__(self.class.neo_primary_key), neo_rel)
78
+ neo_rel
77
79
  end
78
80
 
79
81
  def related_object(method_name)
80
82
  method_name == :self ? self : self.__send__(method_name)
81
83
  end
82
84
 
83
- def update_neo_relationship(partial_mirror = nil)
84
- raise "Couldn't find neo_relationship declaration" unless rel_mirror = self.class.rel_mirror(partial_mirror)
85
- if find_neo_relationship(rel_mirror)
85
+ def update_neo_relationship(rel_mirror = {})
86
+ raise "Couldn't find neo_relationship declaration" unless rel_mirror[:complete] || (rel_mirror = self.class.rel_mirror(rel_mirror))
87
+ if neo_rel = find_neo_relationship(rel_mirror)
86
88
  if related_object(rel_mirror[:start_node]) && related_object(rel_mirror[:end_node]) && neo_relationship_must_exist?(rel_mirror)
87
- ::Neomirror.neo.reset_relationship_properties(@neo_rel, neo_relationship_properties(rel_mirror))
89
+ if related_object(rel_mirror[:start_node]).neo_node.id != neo_rel.start_node.id || related_object(rel_mirror[:end_node]).neo_node.id != neo_rel.end_node.id
90
+ destroy_neo_relationship(rel_mirror, neo_rel: neo_rel)
91
+ create_neo_relationship(rel_mirror)
92
+ else
93
+ ::Neomirror.neo.reset_relationship_properties(neo_rel, neo_relationship_properties(rel_mirror))
94
+ end
88
95
  else
89
- ::Neomirror.neo.remove_relationship_from_index(rel_mirror[:index_name], :id, self.__send__(self.class.neo_primary_key), @neo_rel)
90
- ::Neomirror.neo.delete_relationship(@neo_rel)
96
+ destroy_neo_relationship(rel_mirror, neo_rel: neo_rel)
91
97
  end
92
98
  else
93
99
  create_neo_relationship(rel_mirror) if neo_relationship_must_exist?(rel_mirror)
94
100
  end
95
101
  end
96
102
 
97
- def destroy_neo_relationship(partial_mirror = nil)
98
- raise "Couldn't find neo_relationship declaration" unless rel_mirror = self.class.rel_mirror(partial_mirror)
99
- if find_neo_relationship(rel_mirror)
100
- ::Neomirror.neo.remove_relationship_from_index(rel_mirror[:index_name], :id, self.__send__(self.class.neo_primary_key), @neo_rel)
101
- ::Neomirror.neo.delete_relationship(@neo_rel)
103
+ def destroy_neo_relationship(rel_mirror = {}, options = {})
104
+ raise "Couldn't find neo_relationship declaration" unless rel_mirror[:complete] || (rel_mirror = self.class.rel_mirror(rel_mirror))
105
+ if neo_rel = options.fetch(:neo_rel, find_neo_relationship(rel_mirror))
106
+ ::Neomirror.neo.remove_relationship_from_index(rel_mirror[:index_name], :id, self.__send__(self.class.neo_primary_key), neo_rel)
107
+ ::Neomirror.neo.delete_relationship(neo_rel)
108
+ else
109
+ true
102
110
  end
103
111
  end
104
112
 
@@ -1,3 +1,3 @@
1
1
  module Neomirror
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "neography", ">= 1.5.2"
21
+ spec.add_dependency "neography", ">= 1.3.7"
22
22
  spec.add_development_dependency "bundler", "~> 1.5"
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "rspec", "~> 2.11"
@@ -4,6 +4,7 @@ describe Neomirror do
4
4
  describe "Reflection of model as node and relation" do
5
5
  let(:group) { create(:group) }
6
6
  let(:child_group) { create(:group, parent: group) }
7
+ let(:other_group) { create(:group) }
7
8
 
8
9
  it "creates neo4j nodes" do
9
10
  group.find_neo_node.should be_a Neography::Node
@@ -21,5 +22,12 @@ describe Neomirror do
21
22
  child_group.find_neo_relationship.should be_nil
22
23
  Neomirror.neo.execute_query("MATCH (:Group {id: #{child_group.id}})-[r:CHILD_OF]->(:Group {id: #{group.id}}) RETURN r")["data"].first.should be_nil
23
24
  end
25
+
26
+ it "recreates itself if at least one of nodes is changed on update" do
27
+ child_group.update(parent_id: other_group.id)
28
+ child_group.find_neo_relationship.should be_a Neography::Relationship
29
+ Neomirror.neo.execute_query("MATCH (:Group {id: #{child_group.id}})-[r:CHILD_OF]->(:Group {id: #{group.id}}) RETURN r")["data"].first.should be_nil
30
+ Neomirror.neo.execute_query("MATCH (:Group {id: #{child_group.id}})-[r:CHILD_OF]->(:Group {id: #{other_group.id}}) RETURN r")["data"].first.should be_present
31
+ end
24
32
  end
25
33
  end
@@ -8,6 +8,7 @@ describe Neomirror::Relationship do
8
8
  let(:staff_of_premises) { create(:staff, user: user, premises: premises) }
9
9
  let(:staff_of_group) { create(:staff, user: user, group: group) }
10
10
  let(:staff) { create(:staff, user: user, premises: premises, group: group) }
11
+ let(:other_premises) { create(:premises) }
11
12
 
12
13
  describe "#find_neo_relationship" do
13
14
  it "searches neo4j relationship, returns Neography::Relationship instance" do
@@ -99,6 +100,14 @@ describe Neomirror::Relationship do
99
100
  staff_of_premises.update(premises_id: nil)
100
101
  Neomirror.neo.execute_query("MATCH (:User {id: #{user.id}})-[r:STAFF_OF]->(:Premises {id: #{premises.id}}) RETURN r")["data"].first.should be_nil
101
102
  end
103
+
104
+ it "removes itself and recreate if at least one of related object has changed" do
105
+ staff_of_premises
106
+ Neomirror.neo.execute_query("MATCH (:User {id: #{user.id}})-[r:STAFF_OF]->(:Premises {id: #{premises.id}}) RETURN r")["data"].first.should be_present
107
+ staff_of_premises.update(premises_id: other_premises.id)
108
+ Neomirror.neo.execute_query("MATCH (:User {id: #{user.id}})-[r:STAFF_OF]->(:Premises {id: #{premises.id}}) RETURN r")["data"].first.should be_nil
109
+ Neomirror.neo.execute_query("MATCH (:User {id: #{user.id}})-[r:STAFF_OF]->(:Premises {id: #{other_premises.id}}) RETURN r")["data"].first.should be_present
110
+ end
102
111
  end
103
112
 
104
113
  describe "#destroy_neo_relationship" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neomirror
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Avoyants
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-01 00:00:00.000000000 Z
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: neography
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.5.2
19
+ version: 1.3.7
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.5.2
26
+ version: 1.3.7
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement