hari 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +15 -0
  2. data/.travis.yml +15 -0
  3. data/LICENSE +15 -19
  4. data/README.md +22 -0
  5. data/hari.gemspec +2 -1
  6. data/lib/hari.rb +56 -1
  7. data/lib/hari/entity.rb +8 -11
  8. data/lib/hari/entity/property.rb +2 -2
  9. data/lib/hari/entity/property/builder.rb +9 -1
  10. data/lib/hari/entity/repository.rb +3 -2
  11. data/lib/hari/entity/serialization/float.rb +2 -0
  12. data/lib/hari/entity/serialization/integer.rb +3 -10
  13. data/lib/hari/node.rb +20 -3
  14. data/lib/hari/node/queries.rb +33 -0
  15. data/lib/hari/node/queries/list.rb +147 -0
  16. data/lib/hari/node/queries/relation.rb +100 -0
  17. data/lib/hari/node/queries/relation/backend/list.rb +35 -0
  18. data/lib/hari/node/queries/relation/backend/sorted_set.rb +95 -0
  19. data/lib/hari/node/queries/relation/runnable.rb +24 -0
  20. data/lib/hari/node/queries/relation/start.rb +19 -0
  21. data/lib/hari/node/queries/relation/step.rb +17 -0
  22. data/lib/hari/node/queries/set.rb +116 -0
  23. data/lib/hari/node/queries/sorted_set.rb +130 -0
  24. data/lib/hari/node/repository.rb +24 -0
  25. data/lib/hari/node/serialization.rb +24 -0
  26. data/lib/hari/relation.rb +72 -0
  27. data/lib/hari/relation/linked_list.rb +16 -0
  28. data/lib/hari/relation/sorted_set.rb +16 -0
  29. data/lib/hari/version.rb +1 -1
  30. data/spec/hari/configuration_spec.rb +5 -0
  31. data/spec/hari/entity/repository_spec.rb +4 -4
  32. data/spec/hari/entity/serialization_spec.rb +2 -2
  33. data/spec/hari/node/lists_spec.rb +126 -0
  34. data/spec/hari/node/sets_spec.rb +85 -0
  35. data/spec/hari/node/sorted_sets_spec.rb +89 -0
  36. data/spec/hari/node_spec.rb +79 -0
  37. data/spec/hari_spec.rb +37 -0
  38. data/spec/spec_helper.rb +11 -1
  39. metadata +48 -32
  40. data/lib/hari/relationship.rb +0 -72
  41. data/lib/hari/relationship/linked_list.rb +0 -22
  42. data/lib/hari/relationship/sorted_set.rb +0 -22
  43. data/lib/hari/scripts.rb +0 -26
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hari::Node do
4
+
5
+ let(:joao) { TestNode.create name: 'Joao', model_id: 25 }
6
+ let(:teresa) { TestNode.create name: 'Teresa', model_id: 26 }
7
+ let(:raimundo) { TestNode.create name: 'Raimundo', model_id: 27 }
8
+ let(:maria) { TestNode.create name: 'Maria', model_id: 28 }
9
+ let(:joaquim) { TestNode.create name: 'Joaquim', model_id: 29 }
10
+ let(:lili) { TestNode.create name: 'Lili', model_id: 30 }
11
+
12
+ specify 'find' do
13
+ joao.name.should eq('Joao')
14
+ TestNode.find('test_node#25').name.should eq('Joao')
15
+ TestNode.find(25).name.should eq('Joao')
16
+ TestNode.find('25').name.should eq('Joao')
17
+ end
18
+
19
+ describe 'queries' do
20
+ before do
21
+ Hari.relation! :follow, joao, teresa
22
+ Hari.relation! :follow, joao, raimundo
23
+ Hari.relation! :follow, joao, lili
24
+
25
+ Hari.relation! :follow, teresa, raimundo
26
+ Hari.relation! :follow, teresa, maria
27
+
28
+ Hari.relation! :follow, raimundo, maria
29
+ Hari.relation! :follow, raimundo, joaquim
30
+ end
31
+
32
+ it 'can has queries' do
33
+ joao.out(:follow).count.should eq(3)
34
+ joao.out(:follow).to_a.should eq [lili, raimundo, teresa]
35
+ joao.out(:follow).limit(1).to_a.should eq [lili]
36
+ joao.out(:follow).limit(2).to_a.should eq [lili, raimundo]
37
+ joao.out(:follow).nodes_ids.to_a.should eq %w(test_node#30 test_node#27 test_node#26)
38
+ joao.out(:follow).nodes_ids!.should eq %w(test_node#30 test_node#27 test_node#26)
39
+
40
+ lili.out(:follow).nodes!.should eq []
41
+
42
+ Hari.node(test_node: 25).out(:follow).to_a.should eq [lili, raimundo, teresa]
43
+ end
44
+
45
+ it 'can chain queries' do
46
+ followers_following = joao.out(:follow).out(:follow).to_a
47
+ followers_following.map(&:id).sort.should eq [raimundo, maria, joaquim].map(&:id).sort
48
+ end
49
+ end
50
+
51
+ describe 'queries with pagination' do
52
+ before do
53
+ followings = [teresa, raimundo, maria, joaquim, lili]
54
+ x = 5
55
+
56
+ followings.each do |following|
57
+ Delorean.time_travel_to x.minutes.ago do
58
+ Hari.relation! :follow, joao, following
59
+ end
60
+
61
+ x += 5
62
+ end
63
+ end
64
+
65
+ it 'paginates queries' do
66
+ following = joao.out(:follow).from(15.minutes.ago.to_f).to_a
67
+ following.map(&:id).sort.should eq [teresa, raimundo].map(&:id).sort
68
+ end
69
+
70
+ it 'paginates chained queries' do
71
+ Hari.relation! :follow, teresa, joao
72
+
73
+ following = teresa.out(:follow).out(:follow).from(15.minutes.ago.to_f)
74
+ following.to_a.map(&:id).sort.should eq [teresa, raimundo].map(&:id).sort
75
+ following.nodes!.map(&:id).sort.should eq [teresa, raimundo].map(&:id).sort
76
+ end
77
+ end
78
+
79
+ end
data/spec/hari_spec.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hari do
4
+
5
+ let(:model) { TestNode.create name: 'Tom', model_id: '1' }
6
+
7
+ describe '.node_key' do
8
+ specify { Hari.node_key('user#1').should eq('user#1') }
9
+ specify { Hari.node_key(user: 1).should eq('user#1') }
10
+ specify { Hari.node_key(model).should eq('test_node#1') }
11
+ end
12
+
13
+ describe '.node_id' do
14
+ specify { Hari.node_id('user#1').should eq('1') }
15
+ specify { Hari.node_id('user' => '1').should eq('1') }
16
+ specify { Hari.node_id(model).should eq('1') }
17
+ end
18
+
19
+ describe '.node_type' do
20
+ specify { Hari.node_type('user#1').should eq('user') }
21
+ specify { Hari.node_type('user' => '1').should eq('user') }
22
+ specify { Hari.node_type(model).should eq('test_node') }
23
+ end
24
+
25
+ specify '.node' do
26
+ node = Hari.node(user: 1)
27
+
28
+ node.model_id.should eq(1)
29
+ node.node_type.should eq('user')
30
+
31
+ node = Hari(user: 1)
32
+
33
+ node.model_id.should eq(1)
34
+ node.node_type.should eq('user')
35
+ end
36
+
37
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,9 +2,19 @@ $:.push '../lib'
2
2
 
3
3
  require 'hari'
4
4
  require 'pry'
5
+ require 'delorean'
6
+ require 'active_support/core_ext/numeric/time'
5
7
 
6
- class TestModel < Hari::Entity
8
+ class TestEntity < Hari::Entity
7
9
  property :name
8
10
  property :birth, type: Date
9
11
  property :points, type: Integer
10
12
  end
13
+
14
+ class TestNode < Hari::Node
15
+ property :name
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ config.before(:each) { Hari.redis.flushdb }
20
+ end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hari
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Victor Rodrigues
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-02 00:00:00.000000000 Z
11
+ date: 2013-07-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: redis
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: redis-namespace
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: hiredis
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: activemodel
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ~>
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :runtime
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ~>
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: activesupport
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ~>
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :runtime
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ~>
92
81
  - !ruby/object:Gem::Version
@@ -94,7 +83,6 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: yajl-ruby
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
87
  - - ~>
100
88
  - !ruby/object:Gem::Version
@@ -102,7 +90,6 @@ dependencies:
102
90
  type: :runtime
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
94
  - - ~>
108
95
  - !ruby/object:Gem::Version
@@ -110,7 +97,6 @@ dependencies:
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: bundler
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
101
  - - ~>
116
102
  - !ruby/object:Gem::Version
@@ -118,7 +104,6 @@ dependencies:
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
108
  - - ~>
124
109
  - !ruby/object:Gem::Version
@@ -126,7 +111,6 @@ dependencies:
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: rspec
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
115
  - - ~>
132
116
  - !ruby/object:Gem::Version
@@ -134,7 +118,6 @@ dependencies:
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
122
  - - ~>
140
123
  - !ruby/object:Gem::Version
@@ -142,7 +125,6 @@ dependencies:
142
125
  - !ruby/object:Gem::Dependency
143
126
  name: rake
144
127
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
128
  requirements:
147
129
  - - ~>
148
130
  - !ruby/object:Gem::Version
@@ -150,7 +132,6 @@ dependencies:
150
132
  type: :development
151
133
  prerelease: false
152
134
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
135
  requirements:
155
136
  - - ~>
156
137
  - !ruby/object:Gem::Version
@@ -158,7 +139,6 @@ dependencies:
158
139
  - !ruby/object:Gem::Dependency
159
140
  name: pry
160
141
  requirement: !ruby/object:Gem::Requirement
161
- none: false
162
142
  requirements:
163
143
  - - ! '>='
164
144
  - !ruby/object:Gem::Version
@@ -166,7 +146,20 @@ dependencies:
166
146
  type: :development
167
147
  prerelease: false
168
148
  version_requirements: !ruby/object:Gem::Requirement
169
- none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: delorean
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ! '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
170
163
  requirements:
171
164
  - - ! '>='
172
165
  - !ruby/object:Gem::Version
@@ -180,6 +173,7 @@ extensions: []
180
173
  extra_rdoc_files: []
181
174
  files:
182
175
  - .gitignore
176
+ - .travis.yml
183
177
  - Gemfile
184
178
  - LICENSE
185
179
  - README.md
@@ -203,11 +197,22 @@ files:
203
197
  - lib/hari/errors.rb
204
198
  - lib/hari/node.rb
205
199
  - lib/hari/node/queries.rb
206
- - lib/hari/relationship.rb
207
- - lib/hari/relationship/linked_list.rb
208
- - lib/hari/relationship/sorted_set.rb
209
- - lib/hari/scripts.rb
200
+ - lib/hari/node/queries/list.rb
201
+ - lib/hari/node/queries/relation.rb
202
+ - lib/hari/node/queries/relation/backend/list.rb
203
+ - lib/hari/node/queries/relation/backend/sorted_set.rb
204
+ - lib/hari/node/queries/relation/runnable.rb
205
+ - lib/hari/node/queries/relation/start.rb
206
+ - lib/hari/node/queries/relation/step.rb
207
+ - lib/hari/node/queries/set.rb
208
+ - lib/hari/node/queries/sorted_set.rb
209
+ - lib/hari/node/repository.rb
210
+ - lib/hari/node/serialization.rb
211
+ - lib/hari/relation.rb
212
+ - lib/hari/relation/linked_list.rb
213
+ - lib/hari/relation/sorted_set.rb
210
214
  - lib/hari/version.rb
215
+ - spec/hari/configuration_spec.rb
211
216
  - spec/hari/entity/repository_spec.rb
212
217
  - spec/hari/entity/serialization/boolean_spec.rb
213
218
  - spec/hari/entity/serialization/date_spec.rb
@@ -217,33 +222,38 @@ files:
217
222
  - spec/hari/entity/serialization/string_spec.rb
218
223
  - spec/hari/entity/serialization/time_spec.rb
219
224
  - spec/hari/entity/serialization_spec.rb
225
+ - spec/hari/node/lists_spec.rb
226
+ - spec/hari/node/sets_spec.rb
227
+ - spec/hari/node/sorted_sets_spec.rb
228
+ - spec/hari/node_spec.rb
229
+ - spec/hari_spec.rb
220
230
  - spec/spec_helper.rb
221
- homepage: http://github.com/rodrigues/hari
231
+ homepage: http://github.com/Clubjudge/hari
222
232
  licenses:
223
233
  - MIT
234
+ metadata: {}
224
235
  post_install_message:
225
236
  rdoc_options: []
226
237
  require_paths:
227
238
  - lib
228
239
  required_ruby_version: !ruby/object:Gem::Requirement
229
- none: false
230
240
  requirements:
231
241
  - - ! '>='
232
242
  - !ruby/object:Gem::Version
233
243
  version: '0'
234
244
  required_rubygems_version: !ruby/object:Gem::Requirement
235
- none: false
236
245
  requirements:
237
246
  - - ! '>='
238
247
  - !ruby/object:Gem::Version
239
248
  version: '0'
240
249
  requirements: []
241
250
  rubyforge_project:
242
- rubygems_version: 1.8.25
251
+ rubygems_version: 2.0.6
243
252
  signing_key:
244
- specification_version: 3
253
+ specification_version: 4
245
254
  summary: Hari is a graph library on top of Redis + Lua scripts
246
255
  test_files:
256
+ - spec/hari/configuration_spec.rb
247
257
  - spec/hari/entity/repository_spec.rb
248
258
  - spec/hari/entity/serialization/boolean_spec.rb
249
259
  - spec/hari/entity/serialization/date_spec.rb
@@ -253,4 +263,10 @@ test_files:
253
263
  - spec/hari/entity/serialization/string_spec.rb
254
264
  - spec/hari/entity/serialization/time_spec.rb
255
265
  - spec/hari/entity/serialization_spec.rb
266
+ - spec/hari/node/lists_spec.rb
267
+ - spec/hari/node/sets_spec.rb
268
+ - spec/hari/node/sorted_sets_spec.rb
269
+ - spec/hari/node_spec.rb
270
+ - spec/hari_spec.rb
256
271
  - spec/spec_helper.rb
272
+ has_rdoc:
@@ -1,72 +0,0 @@
1
- module Hari
2
- class Relationship < Entity
3
-
4
- properties :label, :start_node_id, :end_node_id, presence: true
5
-
6
- def generate_id
7
- 'rel:' + start_node.id + ':' + end_node.id + ':' + backend.id + ':' + SecureRandom.hex(3)
8
- end
9
-
10
- def start_node
11
- @start_node ||= Hari::Node.find(start_node_id)
12
- end
13
-
14
- def end_node
15
- @end_node ||= Hari::Node.find(end_node_id)
16
- end
17
-
18
- def self.create(label, start_node, end_node, attributes = {})
19
- attrs = attributes.merge(label: label,
20
- start_node_id: node_id(start_node),
21
- end_node_id: node_id(end_node))
22
- new(attrs).save
23
- end
24
-
25
- def self.use!(backend)
26
- @backend = backend.kind_of?(Module) ? backend
27
- : "Hari::Relationship::#{backend.to_s.camelize}".constantize
28
- end
29
-
30
- def self.backend
31
- @backend ||= Hari::Relationship::Backend::SortedSet
32
- end
33
-
34
- def backend
35
- self.class.backend
36
- end
37
-
38
- def weight(dir)
39
- Time.now
40
- end
41
-
42
- private
43
-
44
- def node_id(node)
45
- node.kind_of?(::String) ? node : node.try(:id)
46
- end
47
-
48
- def key(dir = nil)
49
- case dir
50
- when nil
51
- "#{start_node_id}:#{label}:#{end_node_id}"
52
- else
53
- "#{start_node_id}:#{label}:#{dir}"
54
- end
55
- end
56
-
57
- def create
58
- super
59
- backend.create self
60
- Hari.redis.set key, id
61
- self
62
- end
63
-
64
- def delete
65
- backend.delete self
66
- Hari.redis.del key
67
- super
68
- self
69
- end
70
-
71
- end
72
- end