ork 0.1.3 → 0.1.4

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: 751f05a6aa58853ff594139a6f305dbbffd7ada2
4
- data.tar.gz: 6350a3beb54c4dfebbe2494f9591cc1356881074
3
+ metadata.gz: 2c07bdc3fa0fd2f3ed00a327a9ca745315e33243
4
+ data.tar.gz: 9059ca0b44c7d24afdec3db8ce3ad8a2de5b05be
5
5
  SHA512:
6
- metadata.gz: 843c02219e04d26548a4e329e79b0e9e032a81760f19945269e10809808a99c4254a8e9c0aa9c64ba94d349ac95008dd078542ef060310ab1764068115de5f0b
7
- data.tar.gz: 339e08edc7416c6b8040d827ce4a588c2bc46fa1be1a4459877e6309a7afcd2c903c80836538a309ef5b76f170c3d28233e630eee10ace712a8b83d0e9cd5cb0
6
+ metadata.gz: 8da194455a375e7e84f75bbec0776ef9e5b5bcfa068b36eb7a27528b9bea1263a220a2ac3826921e94f22eea7e163469b87fb8e4b9cb705d050569d3fb72bcd8
7
+ data.tar.gz: d5e4c535d1f791a17994af104700173dcc5cd58ebc5968088931628e98fa15dbf826234987ba9294cc7d89e46754cb957a4710aaac83c41a40f9a2895341a22d
@@ -3,11 +3,13 @@ module Ork
3
3
  class IndexNotFound < RuntimeError; end
4
4
  class UniqueIndexViolation < RuntimeError; end
5
5
 
6
+ class NotOrkObject < StandardError; end
7
+
6
8
  # Document
7
9
  class InvalidClass < RuntimeError; end
8
10
 
9
11
  # Embedded
10
- class NotAnEmbeddableObject < RuntimeError; end
12
+ class NotEmbeddable < StandardError; end
11
13
  class ParentMissing < RuntimeError; end
12
14
 
13
15
 
@@ -68,5 +68,16 @@ module Ork
68
68
 
69
69
  model.new attributes
70
70
  end
71
+
72
+ def assert_valid_class(object, model)
73
+ raise Ork::NotOrkObject.new(object) unless object.class.include? Ork::Document
74
+ raise Ork::InvalidClass.new(object) if object.class.name != model.to_s
75
+ end
76
+
77
+ def assert_embeddable(object)
78
+ unless object.respond_to?(:embeddable?) && object.embeddable?
79
+ raise Ork::NotEmbeddable.new(object)
80
+ end
81
+ end
71
82
  end
72
83
  end
@@ -47,7 +47,8 @@ module Ork::Model
47
47
  end
48
48
 
49
49
  define_method(:"#{name}=") do |object|
50
- raise Ork::InvalidClass.new(object) if object.class.name != model.to_s
50
+ assert_valid_class object, model
51
+
51
52
  send(writer, object ? object.id : nil)
52
53
  @_memo[name] = object
53
54
  end
@@ -145,12 +146,18 @@ module Ork::Model
145
146
  end
146
147
 
147
148
  define_method(:"#{name}_add") do |object|
148
- raise Ork::InvalidClass.new(object) if object.class.name != model.to_s
149
+ assert_valid_class object, model
149
150
 
150
151
  @attributes[reader] = Array(@attributes[reader]) << object.id
151
- @_memo[name] << object unless @_memo[name].nil?
152
+ @_memo[name] << object if @_memo[name]
152
153
  end
153
154
 
155
+ define_method(:"#{name}_remove") do |object|
156
+ assert_valid_class object, model
157
+
158
+ @_memo[name].delete(object) if @_memo[name]
159
+ @attributes[reader].delete(object.id) and object if @attributes[reader]
160
+ end
154
161
  end
155
162
 
156
163
  # A macro for defining an attribute, and the accessors
@@ -191,9 +198,7 @@ module Ork::Model
191
198
  end
192
199
 
193
200
  define_method(:"#{name}=") do |object|
194
- unless object.respond_to?(:embeddable?) && object.embeddable?
195
- raise Ork::NotAnEmbeddableObject.new(object)
196
- end
201
+ assert_embeddable object
197
202
 
198
203
  @embedding[name] = object.attributes
199
204
  object.__parent = self
@@ -242,12 +247,20 @@ module Ork::Model
242
247
  end
243
248
 
244
249
  define_method(:"#{name}_add") do |object|
245
- raise Ork::NotAnEmbeddableObject.new(object) unless object.embeddable?
250
+ assert_embeddable object
246
251
 
247
252
  object.__parent = self
248
- @_memo[name] << object unless @_memo[name].nil?
253
+ @_memo[name] << object if @_memo[name]
249
254
  @embedding[name] = Array(@embedding[name]) << object.attributes
250
255
  end
256
+
257
+ define_method(:"#{name}_remove") do |object|
258
+ assert_embeddable object
259
+
260
+ object.__parent = nil
261
+ @_memo[name].delete(object) if @_memo[name]
262
+ @embedding[name].delete(object.attributes) and object if @embedding[name]
263
+ end
251
264
  end
252
265
 
253
266
  private
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'ork'
3
- s.version = '0.1.3'
3
+ s.version = '0.1.4'
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.'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emiliano Mancuso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-14 00:00:00.000000000 Z
11
+ date: 2014-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: riak-client