active_node 0.0.2.alpha → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
- script: "bundle exec rake neo4j:install['enterprise','2.0.0-M03'] neo4j:start spec --trace"
1
+ script: "bundle exec rake neo4j:install['enterprise','1.9.3'] neo4j:start spec --trace"
2
2
  language: ruby
3
3
  rvm:
4
4
  - 1.9.3
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ active_node
2
+ ===========
3
+
4
+ ActiveRecord style Object Graph Mapping for neo4j
data/active_node.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
24
24
  s.add_dependency "activemodel"
25
25
  s.add_development_dependency "rspec", ">= 2.11"
26
26
  s.add_development_dependency "net-http-spy", "0.2.1"
27
- #s.add_development_dependency "rake", ">= 0.8.7"
27
+ s.add_development_dependency "rake", ">= 0.8.7"
28
28
  s.add_development_dependency "coveralls"
29
29
 
30
30
  end
@@ -8,13 +8,17 @@ module ActiveNode
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  autoload :Association, 'active_node/associations/association'
11
+ autoload :SingularAssociation, 'active_node/associations/singular_association'
11
12
  autoload :CollectionAssociation, 'active_node/associations/collection_association'
13
+ autoload :HasOneAssociation, 'active_node/associations/has_one_association'
12
14
  autoload :HasManyAssociation, 'active_node/associations/has_many_association'
13
15
 
14
16
  module Builder #:nodoc:
15
17
  autoload :Association, 'active_node/associations/builder/association'
18
+ autoload :SingularAssociation, 'active_node/associations/builder/singular_association'
16
19
  autoload :CollectionAssociation, 'active_node/associations/builder/collection_association'
17
20
 
21
+ autoload :HasOne, 'active_node/associations/builder/has_one'
18
22
  autoload :HasMany, 'active_node/associations/builder/has_many'
19
23
  end
20
24
 
@@ -55,6 +59,10 @@ module ActiveNode
55
59
  def has_many(name, options = {})
56
60
  Builder::HasMany.build(self, name, options)
57
61
  end
62
+
63
+ def has_one(name, options = {})
64
+ Builder::HasOne.build(self, name, options)
65
+ end
58
66
  end
59
67
  end
60
68
  end
@@ -39,6 +39,33 @@ module ActiveNode
39
39
  def klass
40
40
  reflection.klass
41
41
  end
42
+
43
+ def load_target
44
+ owner.send(reflection.direction, reflection.type, reflection.klass)
45
+ end
46
+
47
+ # Implements the reader method, e.g. foo.items for Foo.has_many :items
48
+ def reader(force_reload = false)
49
+ @target ||= load_target
50
+ end
51
+
52
+ # Implements the writer method, e.g. foo.items= for Foo.has_many :items
53
+ def writer(records)
54
+ @dirty = true
55
+ @target = records
56
+ end
57
+
58
+ def save
59
+ return unless @dirty
60
+ #delete all relations missing in new target
61
+ owner.node.rels(reflection.type).send(reflection.direction).each do |rel|
62
+ rel.del unless ids_reader.include? rel.other_node(owner.node).neo_id.to_i
63
+ end
64
+ original_target = owner.node.send(reflection.direction, reflection.type)
65
+ original_target_ids = original_target.map(&:neo_id).map(&:to_i)
66
+ #add relations missing in old target
67
+ target_each { |n| original_target << n.node unless original_target_ids.include? n.id }
68
+ end
42
69
  end
43
70
  end
44
71
  end
@@ -4,7 +4,7 @@ module ActiveNode::Associations::Builder
4
4
  attr_accessor :valid_options
5
5
  end
6
6
 
7
- self.valid_options = [:class_name]
7
+ self.valid_options = [:direction, :type, :class_name]
8
8
 
9
9
  attr_reader :model, :name, :options, :reflection
10
10
 
@@ -3,9 +3,5 @@ module ActiveNode::Associations::Builder
3
3
  def macro
4
4
  :has_many
5
5
  end
6
-
7
- def valid_options
8
- super + [:direction, :type]
9
- end
10
6
  end
11
7
  end
@@ -0,0 +1,7 @@
1
+ module ActiveNode::Associations::Builder
2
+ class HasOne < SingularAssociation #:nodoc:
3
+ def macro
4
+ :has_one
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveNode::Associations::Builder
2
+ class SingularAssociation < Association #:nodoc:
3
+ def define_constructors
4
+ mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
5
+ def build_#{name}(*args, &block)
6
+ association(:#{name}).build(*args, &block)
7
+ end
8
+
9
+ def create_#{name}(*args, &block)
10
+ association(:#{name}).create(*args, &block)
11
+ end
12
+
13
+ def create_#{name}!(*args, &block)
14
+ association(:#{name}).create!(*args, &block)
15
+ end
16
+ CODE
17
+ end
18
+ end
19
+ end
@@ -26,17 +26,6 @@ module ActiveNode
26
26
  # +load_target+ and the +loaded+ flag are your friends.
27
27
  class CollectionAssociation < Association #:nodoc:
28
28
 
29
- # Implements the reader method, e.g. foo.items for Foo.has_many :items
30
- def reader(force_reload = false)
31
- @target ||= load_target
32
- end
33
-
34
- # Implements the writer method, e.g. foo.items= for Foo.has_many :items
35
- def writer(records)
36
- @dirty = true
37
- @target = records
38
- end
39
-
40
29
  # Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
41
30
  def ids_reader
42
31
  reader.map(&:id)
@@ -47,20 +36,8 @@ module ActiveNode
47
36
  writer klass.find(ids.reject(&:blank?).map!(&:to_i))
48
37
  end
49
38
 
50
- def load_target
51
- owner.send(reflection.direction, reflection.type, reflection.klass)
52
- end
53
-
54
- def save
55
- return unless @dirty
56
- #delete all relations missing in new target
57
- owner.node.rels(reflection.type).send(reflection.direction).each do |rel|
58
- rel.del unless ids_reader.include? rel.other_node(owner.node).neo_id.to_i
59
- end
60
- original_target = owner.node.send(reflection.direction, reflection.type)
61
- original_target_ids = original_target.map(&:neo_id).map(&:to_i)
62
- #add relations missing in old target
63
- @target.each { |n| original_target << n.node unless original_target_ids.include? n.id }
39
+ def target_each
40
+ target.each {|n| yield n}
64
41
  end
65
42
 
66
43
  def reset
@@ -0,0 +1,7 @@
1
+ module ActiveNode
2
+ # = Active Record Belongs To Has One Association
3
+ module Associations
4
+ class HasOneAssociation < SingularAssociation #:nodoc:
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveNode
2
+ module Associations
3
+ class SingularAssociation < Association #:nodoc:
4
+ def load_target
5
+ super.first
6
+ end
7
+
8
+ def target_each
9
+ yield target
10
+ end
11
+
12
+ def ids_reader
13
+ [target.try(:id)].compact
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveNode
2
- VERSION = "0.0.2.alpha"
3
- end
2
+ VERSION = "0.0.3"
3
+ end
@@ -73,5 +73,18 @@ describe ActiveNode::Associations do
73
73
  id = Person.create!(children: [Person.create!, Person.create!]).id
74
74
  Person.find(id).children.size.should == 2
75
75
  end
76
+
77
+ it 'can set has_one relation' do
78
+ father = Person.create!
79
+ child = Person.create!
80
+ child.father = father
81
+ child.save
82
+ father.children.should == [child]
83
+ end
84
+
85
+ it 'can handle has_one reverse relationship' do
86
+ father = Person.create!(children: [Person.create!])
87
+ father.children.first.father.should == father
88
+ end
76
89
  end
77
90
  end
@@ -1,7 +1,6 @@
1
1
  class Client < ActiveNode::Base
2
2
  attribute :name, type: String
3
3
 
4
- #has_many :phases
5
4
  has_many :users, type: :client, direction: :incoming, class_name: 'NeoUser'
6
5
 
7
6
  validates :name, presence: true
@@ -1,5 +1,5 @@
1
1
  class Person < ActiveNode::Base
2
2
  has_many :people
3
3
  has_many :children, class_name: "Person"
4
- #has_one :father, type: :child, direction: :incoming, class_name: "Person"
4
+ has_one :father, type: :child, direction: :incoming, class_name: "Person"
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_node
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.alpha
5
- prerelease: 6
4
+ version: 0.0.3
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Heinrich Klobuczek
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-06 00:00:00.000000000 Z
12
+ date: 2013-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_attr
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - '='
108
108
  - !ruby/object:Gem::Version
109
109
  version: 0.2.1
110
+ - !ruby/object:Gem::Dependency
111
+ name: rake
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.8.7
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 0.8.7
110
126
  - !ruby/object:Gem::Dependency
111
127
  name: coveralls
112
128
  requirement: !ruby/object:Gem::Requirement
@@ -134,6 +150,7 @@ files:
134
150
  - .travis.yml
135
151
  - Gemfile
136
152
  - LICENSE
153
+ - README.md
137
154
  - Rakefile
138
155
  - active_node.gemspec
139
156
  - lib/active_node.rb
@@ -142,8 +159,12 @@ files:
142
159
  - lib/active_node/associations/builder/association.rb
143
160
  - lib/active_node/associations/builder/collection_association.rb
144
161
  - lib/active_node/associations/builder/has_many.rb
162
+ - lib/active_node/associations/builder/has_one.rb
163
+ - lib/active_node/associations/builder/singular_association.rb
145
164
  - lib/active_node/associations/collection_association.rb
146
165
  - lib/active_node/associations/has_many_association.rb
166
+ - lib/active_node/associations/has_one_association.rb
167
+ - lib/active_node/associations/singular_association.rb
147
168
  - lib/active_node/base.rb
148
169
  - lib/active_node/callbacks.rb
149
170
  - lib/active_node/core.rb
@@ -174,9 +195,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
195
  required_rubygems_version: !ruby/object:Gem::Requirement
175
196
  none: false
176
197
  requirements:
177
- - - ! '>'
198
+ - - ! '>='
178
199
  - !ruby/object:Gem::Version
179
- version: 1.3.1
200
+ version: '0'
180
201
  requirements: []
181
202
  rubyforge_project: active_node
182
203
  rubygems_version: 1.8.25