ork 0.1.2 → 0.1.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: 057b1b4f7eb4e0328a9998738e477947bacdacc3
4
- data.tar.gz: d6522930cdaf1e25e16b19988be2901db7968e9b
3
+ metadata.gz: 751f05a6aa58853ff594139a6f305dbbffd7ada2
4
+ data.tar.gz: 6350a3beb54c4dfebbe2494f9591cc1356881074
5
5
  SHA512:
6
- metadata.gz: cbbc733bd2cc6dc9954827ed1d170fe2a27f95847e114bbaa000666a823be271e6a9ef018da6afb7d2e8c3f093364ad039d3f81ede44d3755adde4b1ab58adb2
7
- data.tar.gz: e4ac8f95ace5511046f100a33f7005a56eb17cdeb3589d2d11174848067dfd23d697ae36671c13f3d0545f7f929ca90df99864c4c9671cfc45b2c0eed96e10ae
6
+ metadata.gz: 843c02219e04d26548a4e329e79b0e9e032a81760f19945269e10809808a99c4254a8e9c0aa9c64ba94d349ac95008dd078542ef060310ab1764068115de5f0b
7
+ data.tar.gz: 339e08edc7416c6b8040d827ce4a588c2bc46fa1be1a4459877e6309a7afcd2c903c80836538a309ef5b76f170c3d28233e630eee10ace712a8b83d0e9cd5cb0
data/README.md CHANGED
@@ -7,6 +7,8 @@
7
7
 
8
8
  Ork is a small Ruby modeling layer for **Riak** database, inspired by [Ohm](http://ohm.keyvalue.org).
9
9
 
10
+ ![Ork](http://f.cl.ly/items/2x2O0s3U0v2U0B3N313F/Ork.png)
11
+
10
12
  ## Dependencies
11
13
 
12
14
  `ork` requires Ruby 1.9 or later and the `riak-client` gem to connect to **Riak**.
@@ -40,11 +42,11 @@ Take a look at the example below:
40
42
  class Post
41
43
  include Ork::Document
42
44
 
43
- attribute :name
44
- attribute :age, default: 18
45
+ attribute :title
46
+ attribute :rating, default: 4
45
47
 
46
- index :age
47
- unique :name
48
+ index :rating
49
+ unique :title
48
50
  end
49
51
 
50
52
  class Comment
@@ -62,11 +64,11 @@ It also gives you some helpful **class methods**:
62
64
  |:-------------|:-------------------------------------------------|:-------------------------|
63
65
  | bucket | `Riak::Bucket` The bucket assigned to this class | `#<Riak::Bucket {post}>` |
64
66
  | bucket_name | `String` The bucket name | `"post"` |
65
- | attributes | `Array` Attributes declared | `[:name, :age]` |
66
- | indices | `Array` Indices declared | `[:age]` |
67
- | uniques | `Array` Unique indices declared | `[:name]` |
67
+ | attributes | `Array` Attributes declared | `[:title, :rating]` |
68
+ | indices | `Array` Indices declared | `[:rating]` |
69
+ | uniques | `Array` Unique indices declared | `[:title]` |
68
70
  | embedding | `Array` Embedded attributes declared | `[:post]` |
69
- | defaults | `Hash` Defaults for attributes | `{:age=>18}` |
71
+ | defaults | `Hash` Defaults for attributes | `{:rating=>4}` |
70
72
 
71
73
 
72
74
  And for **instance methods** it defines:
@@ -96,7 +98,7 @@ Core behaviour of `Ork::Model`.
96
98
  An `attribute` is just any value that can be stored. It is composed of a `:name` and an optional `hash`.
97
99
 
98
100
  ```ruby
99
- attribute :age, default: 18
101
+ attribute :rating, default: 4
100
102
  ```
101
103
 
102
104
  #### Options
@@ -120,6 +122,7 @@ containing the foreign key to another model.
120
122
  reference :user, :User
121
123
  ```
122
124
 
125
+
123
126
  ## referenced
124
127
 
125
128
  Provides an accessor to search for _one_ model that `reference` the current model.
@@ -128,14 +131,23 @@ Provides an accessor to search for _one_ model that `reference` the current mode
128
131
  referenced :comment, :Comment
129
132
  ```
130
133
 
134
+
131
135
  ## collection
132
136
 
133
- Provides an accessor to search for _all_ models that `reference` the current model.
137
+ It's a special kind of attribute that references another models.
138
+ Internally, Ork will keep a an array of ids to the models, but you get
139
+ accessors that give you real instances.
140
+
141
+ It won't make a query to retrieve _all_ models taht `reference` the current model.
142
+ This is something that works well on _relational databases_ but is not recomended
143
+ for _document oriented databases_ like **Riak**.
144
+
134
145
 
135
146
  ```ruby
136
147
  collection :comments, :Comment
137
148
  ```
138
149
 
150
+
139
151
  ## embed
140
152
  > Only accepts embeddable objects.
141
153
 
@@ -147,6 +159,7 @@ accessors that give you real instances.
147
159
  embed :comment, :Comment
148
160
  ```
149
161
 
162
+
150
163
  ## embed_collection
151
164
  > Only accepts embeddable objects.
152
165
 
@@ -172,6 +185,24 @@ Provides an accessor to the object that `embeds` the current model.
172
185
  embedded :post, :Post
173
186
  ```
174
187
 
188
+
189
+ ## index
190
+
191
+ Create an index for the previously defined `attribute`.
192
+
193
+ ```ruby
194
+ index :rating
195
+ ```
196
+
197
+ ## unique
198
+
199
+ Create a unique index for the previously defined `attribute`.
200
+
201
+ ```ruby
202
+ unique :title
203
+ ```
204
+
205
+
175
206
  ## Pagination
176
207
 
177
208
  Pagination is a key feature introduced in _Riak 1.4_ and it is supported as well!
data/lib/ork/errors.rb CHANGED
@@ -3,8 +3,13 @@ module Ork
3
3
  class IndexNotFound < RuntimeError; end
4
4
  class UniqueIndexViolation < RuntimeError; end
5
5
 
6
+ # Document
7
+ class InvalidClass < RuntimeError; end
8
+
9
+ # Embedded
6
10
  class NotAnEmbeddableObject < RuntimeError; end
7
11
  class ParentMissing < RuntimeError; end
8
12
 
13
+
9
14
  class NoNextPage < Error; end
10
15
  end
@@ -38,20 +38,18 @@ module Ork::Model
38
38
  reader = :"#{name}_id"
39
39
  writer = :"#{name}_id="
40
40
 
41
+ attribute reader, accessors: :reader
41
42
  index reader
42
43
 
43
- define_method(reader) do
44
- @attributes[reader]
45
- end
46
-
47
44
  define_method(writer) do |value|
48
45
  @_memo.delete(name)
49
46
  @attributes[reader] = value
50
47
  end
51
48
 
52
- define_method(:"#{name}=") do |value|
53
- send(writer, value ? value.id : nil)
54
- @_memo[name] = value
49
+ define_method(:"#{name}=") do |object|
50
+ raise Ork::InvalidClass.new(object) if object.class.name != model.to_s
51
+ send(writer, object ? object.id : nil)
52
+ @_memo[name] = object
55
53
  end
56
54
 
57
55
  define_method(name) do
@@ -117,19 +115,42 @@ module Ork::Model
117
115
  # class User
118
116
  # include Ork::Document
119
117
  #
118
+ # attribute posts_ids
119
+ #
120
120
  # def posts
121
- # Post.find(:user_id => self.id)
121
+ # Post.all(self.posts_ids)
122
+ # end
123
+ #
124
+ # def posts_add(post)
125
+ # self.posts_ids << post.id
122
126
  # end
123
127
  # end
124
128
  #
125
129
  def collection(name, model, reference = to_reference)
126
- define_method name do
130
+ reader = :"#{name}_ids"
131
+
132
+ attribute reader, accessors: :reader
133
+
134
+ define_method(:"#{name}_ids=") do |value|
135
+ @_memo.delete(name)
136
+ @attributes[reader] = value
137
+ end
138
+
139
+ define_method(name) do
127
140
  return [] if self.id.nil?
128
141
  @_memo[name] ||= begin
129
142
  model = Ork::Utils.const(self.class, model)
130
- model.find(:"#{reference}_id", self.id)
143
+ model.all(@attributes[reader].to_a)
131
144
  end
132
145
  end
146
+
147
+ define_method(:"#{name}_add") do |object|
148
+ raise Ork::InvalidClass.new(object) if object.class.name != model.to_s
149
+
150
+ @attributes[reader] = Array(@attributes[reader]) << object.id
151
+ @_memo[name] << object unless @_memo[name].nil?
152
+ end
153
+
133
154
  end
134
155
 
135
156
  # A macro for defining an attribute, and the accessors
@@ -165,7 +186,7 @@ module Ork::Model
165
186
  return nil unless @embedding.has_key? name
166
187
  @_memo[name] ||= begin
167
188
  model = Ork::Utils.const(self.class, model)
168
- model.new(@embedding[name])
189
+ new_embedded model, @embedding[name]
169
190
  end
170
191
  end
171
192
 
@@ -201,7 +222,7 @@ module Ork::Model
201
222
  # # An array of authors
202
223
  # end
203
224
  #
204
- # def add_author(author)
225
+ # def authors_add(author)
205
226
  # # Add an author to the embed collection
206
227
  # end
207
228
  # end
@@ -214,11 +235,13 @@ module Ork::Model
214
235
 
215
236
  @_memo[name] ||= begin
216
237
  model = Ork::Utils.const(self.class, model)
217
- @embedding[name].map{|atts| model.new atts}
238
+ @embedding[name].map do |atts|
239
+ new_embedded model, atts
240
+ end
218
241
  end
219
242
  end
220
243
 
221
- define_method(:"add_#{name}") do |object|
244
+ define_method(:"#{name}_add") do |object|
222
245
  raise Ork::NotAnEmbeddableObject.new(object) unless object.embeddable?
223
246
 
224
247
  object.__parent = self
@@ -21,14 +21,16 @@ module Ork::Model
21
21
  end
22
22
  alias :exists? :exist?
23
23
 
24
- # Find all documents in the Document's bucket and return them.
24
+ # Find all documents of specified keys and return them
25
+ #
26
+ # When nil, find all documents in the Document's bucket and return them.
25
27
  # @return Ork::ResultSet<Document> all found documents in the bucket
26
28
  #
27
- # @Note: This operation is incredibly expensive and should not
29
+ # @Note: This operation can be incredibly expensive and should not
28
30
  # be used in production applications.
29
31
  #
30
- def all
31
- Ork::ResultSet.all(self)
32
+ def all(keys = nil)
33
+ Ork::ResultSet.all(self, keys)
32
34
  end
33
35
  alias :list :all
34
36
 
data/lib/ork/model.rb CHANGED
@@ -34,15 +34,7 @@ module Ork
34
34
  #
35
35
  def update_embedded_attributes(atts)
36
36
  atts.each do |att, val|
37
- if val.is_a? Array
38
- val.each do |object_atts|
39
- model = Ork::Utils.const(self.class, object_atts.delete('_type'))
40
- send(:"add_#{att}", model.new(object_atts))
41
- end
42
- else
43
- model = Ork::Utils.const(self.class, val.delete('_type'))
44
- send(:"#{att}=", model.new(val))
45
- end
37
+ @embedding[att] = val
46
38
  end
47
39
  end
48
40
 
@@ -69,5 +61,12 @@ module Ork
69
61
  def model
70
62
  self.class
71
63
  end
64
+
65
+ def new_embedded(model, attributes)
66
+ attributes[model.__parent_key] = self
67
+ attributes.delete '_type'
68
+
69
+ model.new attributes
70
+ end
72
71
  end
73
72
  end
@@ -16,12 +16,12 @@ module Ork
16
16
  # Find all documents in the Document's bucket and return them.
17
17
  # @return Ork::ResultSet<Document> all the documents in the bucket
18
18
  #
19
- # @Note: This operation is incredibly expensive and should not
19
+ # @Note: This operation can be incredibly expensive and should not
20
20
  # be used in production applications.
21
21
  #
22
- def self.all(model)
22
+ def self.all(model, keys = nil)
23
23
  new(model, nil, nil).tap do |r|
24
- r.instance_variable_set(:@keys, model.bucket.keys)
24
+ r.instance_variable_set(:@keys, keys || model.bucket.keys)
25
25
  end
26
26
  end
27
27
 
data/ork.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'ork'
3
- s.version = '0.1.2'
3
+ s.version = '0.1.3'
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
  s.summary = 'Ruby modeling layer for Riak.'
6
6
  s.description = 'Ork is a small Ruby modeling layer for Riak, inspired by Ohm.'
@@ -17,8 +17,8 @@ Gem::Specification.new do |s|
17
17
  ]
18
18
  s.test_files = Dir['test/*.*']
19
19
 
20
- s.add_dependency 'riak-client'
21
- s.add_development_dependency 'protest'
22
- s.add_development_dependency 'mocha'
20
+ s.add_runtime_dependency 'riak-client', '~> 1.4'
21
+ s.add_development_dependency 'protest', '~> 0'
22
+ s.add_development_dependency 'mocha', '~> 0'
23
23
  end
24
24
 
data/test/helper.rb CHANGED
@@ -14,6 +14,7 @@ require "rubygems"
14
14
  require "protest"
15
15
  require "ork"
16
16
 
17
+ I18n.enforce_available_locales = false
17
18
  Riak.disable_list_keys_warnings = true
18
19
  Protest.report_with(:progress)
19
20
 
@@ -31,6 +31,7 @@ Protest.describe 'ResultSet' do
31
31
 
32
32
  test 'it has the keys set but not the loaded objects' do
33
33
  key1, key2 = Post.bucket.keys
34
+
34
35
  assert @all.instance_variable_get(:@keys).include? key1
35
36
  assert @all.instance_variable_get(:@keys).include? key2
36
37
  assert_equal nil, @all.instance_variable_get(:@all)
@@ -45,6 +46,13 @@ Protest.describe 'ResultSet' do
45
46
  assert @all.include? @post1
46
47
  assert @all.include? @post2
47
48
  end
49
+
50
+ test 'it returns all the listed objects in a keys array' do
51
+ @all = Post.all([@post1.id])
52
+
53
+ assert_equal 1, @all.size
54
+ assert @all.include? @post1
55
+ end
48
56
  end
49
57
 
50
58
  test 'it raises an exception when call next_page if does not have next page' do
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emiliano Mancuso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-16 00:00:00.000000000 Z
11
+ date: 2014-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: riak-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.4'
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: '0'
26
+ version: '1.4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: protest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mocha
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Ork is a small Ruby modeling layer for Riak, inspired by Ohm.
@@ -60,20 +60,20 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - README.md
63
- - rakefile
63
+ - lib/ork.rb
64
64
  - lib/ork/connection.rb
65
65
  - lib/ork/errors.rb
66
+ - lib/ork/model.rb
66
67
  - lib/ork/model/associations.rb
67
68
  - lib/ork/model/class_methods.rb
68
69
  - lib/ork/model/document.rb
69
70
  - lib/ork/model/embedded.rb
70
71
  - lib/ork/model/finders.rb
71
72
  - lib/ork/model/index.rb
72
- - lib/ork/model.rb
73
73
  - lib/ork/result_set.rb
74
74
  - lib/ork/utils.rb
75
- - lib/ork.rb
76
75
  - ork.gemspec
76
+ - rakefile
77
77
  - test/helper.rb
78
78
  - test/result_set_test.rb
79
79
  homepage: http://github.com/emancu/ork
@@ -86,17 +86,17 @@ require_paths:
86
86
  - lib
87
87
  required_ruby_version: !ruby/object:Gem::Requirement
88
88
  requirements:
89
- - - '>='
89
+ - - ">="
90
90
  - !ruby/object:Gem::Version
91
91
  version: '0'
92
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
98
  rubyforge_project:
99
- rubygems_version: 2.1.11
99
+ rubygems_version: 2.2.1
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: Ruby modeling layer for Riak.