reorm 0.1.4 → 0.1.8

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: b41dda011f62f508e399da5e164dc26df0dbf50f
4
- data.tar.gz: 36237b21f166740f1b39ee93ea1d5fe46fe0ce9f
3
+ metadata.gz: 0926ae773897fa1ab0198afe1c4577ccfdeea99c
4
+ data.tar.gz: 2f6d59a5ff0e3bbc41daa49262d62a20007f0956
5
5
  SHA512:
6
- metadata.gz: c2d218a94642edcb778b26eb449a728e3117809d4433770efb311c811f1f039f28c23e715e737846c66f5779320e0d6babf7d7f96405ee486f0de4662f33653b
7
- data.tar.gz: ba982ccbe3f4f15c709cd0adeb6f2679063c7aba8c9d85f6ea42f8cced3fa2bd04d451856b7047de2089a77246441b4565a54e7a9fa50f42433d4c504a8e7978
6
+ metadata.gz: 048f6fd28385581a438c97804d93fc4e2e384af5e2e6f5efcaf35f26a810a6fa74c4f03cf32c62c0836eae0f78201d86216e0244a91d6c4ed9bc17d69212dffb
7
+ data.tar.gz: 4c1198b1673b3b7867c49caaeef97ed0a7dfd3c2b1e266223305e159973d887ec90cb05d6cc7db6810aecd9d5cd2d23e184664c3abe8706629e9011a20b53b47
data/lib/reorm/cursor.rb CHANGED
@@ -22,8 +22,12 @@ module Reorm
22
22
  end
23
23
  alias :reset :close
24
24
 
25
- def filter(predicate)
26
- Cursor.new(model_class, @query.filter(predicate), @order_by)
25
+ def filter(predicate=nil, &block)
26
+ if predicate.nil?
27
+ Cursor.new(model_class, @query.filter(&block), @order_by)
28
+ else
29
+ Cursor.new(model_class, @query.filter(predicate), @order_by)
30
+ end
27
31
  end
28
32
 
29
33
  def count
@@ -113,6 +117,12 @@ module Reorm
113
117
  Cursor.new(model_class, @query, arguments)
114
118
  end
115
119
 
120
+ def delete
121
+ Reorm.connection do |connection|
122
+ @query.delete.run(connection)
123
+ end
124
+ end
125
+
116
126
  private
117
127
 
118
128
  def open
@@ -41,13 +41,21 @@ module Reorm
41
41
  break
42
42
  else
43
43
  if index == @path.length - 1
44
- result = [value[field], true]
44
+ result = [value_from_object(field, value), true]
45
45
  else
46
- value = value[field]
46
+ value = value_from_object(field, value)
47
47
  end
48
48
  end
49
49
  end
50
50
  result
51
51
  end
52
+
53
+ def value_from_object(value, object)
54
+ if object.respond_to?(value)
55
+ object.send(value)
56
+ else
57
+ object[value]
58
+ end
59
+ end
52
60
  end
53
61
  end
data/lib/reorm/model.rb CHANGED
@@ -13,14 +13,8 @@ module Reorm
13
13
 
14
14
  def initialize(properties={})
15
15
  @properties = {}
16
- properties.each do |key, value|
17
- if self.respond_to?(setter_name(key))
18
- self.send(setter_name(key), value)
19
- else
20
- @properties[key.to_sym] = value
21
- end
22
- end
23
16
  @errors = PropertyErrors.new
17
+ properties.each {|key, value| set_property(key, value)}
24
18
  end
25
19
  attr_reader :errors
26
20
 
@@ -36,6 +30,10 @@ module Reorm
36
30
  self
37
31
  end
38
32
 
33
+ def include?(field)
34
+ @properties.include?(property_name(field))
35
+ end
36
+
39
37
  def save(validated=true)
40
38
  if validated && !valid?
41
39
  raise Error, "Validation error encountered saving an instance of the #{self.class.name} class."
@@ -69,6 +67,31 @@ module Reorm
69
67
  else
70
68
  fire_events(events: [:after_save, :after_update])
71
69
  end
70
+ self
71
+ end
72
+
73
+ def update(properties={})
74
+ properties.each do |property, value|
75
+ set_property(property, value)
76
+ end
77
+ self.save if !properties.empty?
78
+ self
79
+ end
80
+
81
+ def delete
82
+ key = get_property(primary_key)
83
+ if key
84
+ Reorm.connection do |connection|
85
+ fire_events(events: [:before_delete])
86
+ result = r.table(table_name).get(key).delete.run(connection)
87
+ if result["deleted"] != 1
88
+ raise Error, "Deletion of record for a #{self.class.name} class instance with a primary key of #{key} failed."
89
+ end
90
+ fire_events(events: [:after_delete])
91
+ set_property(primary_key, nil)
92
+ end
93
+ end
94
+ self
72
95
  end
73
96
 
74
97
  def [](property_name)
@@ -80,6 +103,26 @@ module Reorm
80
103
  value
81
104
  end
82
105
 
106
+ def has_property?(property)
107
+ @properties.include?(property_name(property))
108
+ end
109
+
110
+ def get_property(property)
111
+ has_property?(property) ? self.__send__(property_name(property)) : nil
112
+ end
113
+
114
+ def set_property(property, value)
115
+ self.__send__(setter_name(property), value)
116
+ self
117
+ end
118
+
119
+ def set_properties(settings={})
120
+ settings.each do |property, value|
121
+ set_property(property, value)
122
+ end
123
+ self
124
+ end
125
+
83
126
  def respond_to?(method_name, include_private=false)
84
127
  method_name.to_s[-1, 1] == "=" || @properties.include?(property_name(method_name)) || super
85
128
  end
@@ -15,6 +15,10 @@ module Reorm
15
15
  store_event_handlers(:after_create, *methods)
16
16
  end
17
17
 
18
+ def after_delete(*methods)
19
+ store_event_handlers(:after_delete, *methods)
20
+ end
21
+
18
22
  def after_save(*methods)
19
23
  store_event_handlers(:after_save, *methods)
20
24
  end
@@ -31,6 +35,10 @@ module Reorm
31
35
  store_event_handlers(:before_create, *methods)
32
36
  end
33
37
 
38
+ def before_delete(*methods)
39
+ store_event_handlers(:before_delete, *methods)
40
+ end
41
+
34
42
  def before_save(*methods)
35
43
  store_event_handlers(:before_save, *methods)
36
44
  end
@@ -72,7 +80,7 @@ module Reorm
72
80
  events.each do |event|
73
81
  if handlers.include?(event)
74
82
  handlers[event].each do |handler|
75
- if !object.respond_to?(handler)
83
+ if !object.respond_to?(handler, true)
76
84
  raise Error, "Unable to locate a method called '#{handler}' for an instance of the #{object.class.name} class."
77
85
  end
78
86
  object.__send__(handler)
data/lib/reorm/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Reorm
2
2
  MAJOR_VERSION = 0
3
3
  MINOR_VERSION = 1
4
- BUILD_VERSION = 4
4
+ BUILD_VERSION = 8
5
5
  VERSION = "#{MAJOR_VERSION}.#{MINOR_VERSION}.#{BUILD_VERSION}"
6
6
  end
@@ -211,4 +211,14 @@ describe Reorm::Cursor do
211
211
  expect(indices).to eq([5, 4, 3, 2, 1])
212
212
  end
213
213
  end
214
+
215
+ describe "#delete()" do
216
+ it "deletes all records" do
217
+ query = CursorTestModel.filter do |record|
218
+ (record["index"].eq(1) | record["index"].eq(3) | record["index"].eq(5))
219
+ end
220
+ query.delete
221
+ expect(CursorTestModel.all.count).to eq(2)
222
+ end
223
+ end
214
224
  end
@@ -3,63 +3,63 @@ require "spec_helper"
3
3
  describe Reorm::FieldPath do
4
4
  describe "#name()" do
5
5
  subject {
6
- Reorm::FieldPath.new(:first, :second, :third)
6
+ Reorm::FieldPath.new(:one, :two, :three)
7
7
  }
8
8
 
9
9
  it "returns the last element of the field path as the name" do
10
- expect(subject.name).to eq(:third)
10
+ expect(subject.name).to eq(:three)
11
11
  end
12
12
  end
13
13
 
14
14
  describe "#value()" do
15
15
  it "fetches the value from the input" do
16
- field = Reorm::FieldPath.new(:first)
17
- expect(field.value({first: 1})).to eq(1)
16
+ field = Reorm::FieldPath.new(:one)
17
+ expect(field.value({one: 1})).to eq(1)
18
18
  end
19
19
 
20
20
  it "traverses a hierarchy if one has been specified" do
21
- field = Reorm::FieldPath.new(:first, :second, :third)
22
- expect(field.value({first: {second: {third: 3}}})).to eq(3)
21
+ field = Reorm::FieldPath.new(:one, :two, :three)
22
+ expect(field.value({one: {two: {three: 3}}})).to eq(3)
23
23
  end
24
24
 
25
25
  it "returns nil of the field does not exist" do
26
- field = Reorm::FieldPath.new(:first, :second, :third)
27
- expect(field.value({first: 1})).to be_nil
26
+ field = Reorm::FieldPath.new(:one, :two, :three)
27
+ expect(field.value({one: 1})).to be_nil
28
28
  end
29
29
  end
30
30
 
31
31
  describe "#value!()" do
32
32
  it "raises an exception if the value does not exist" do
33
- field = Reorm::FieldPath.new(:first, :second, :third)
33
+ field = Reorm::FieldPath.new(:one, :two, :three)
34
34
  expect {
35
- field.value!({first: 1})
35
+ field.value!({one: 1})
36
36
  }.to raise_exception(Reorm::Error, "Unable to locate the #{field.name} (full path: #{field}) field for an instance of the Hash class.")
37
37
  end
38
38
  end
39
39
 
40
40
  describe "#exists?()" do
41
41
  subject {
42
- Reorm::FieldPath.new(:first, :second, :third)
42
+ Reorm::FieldPath.new(:one, :two, :three)
43
43
  }
44
44
 
45
45
  it "returns true if the field exists within the specified document" do
46
- expect(subject.exists?({first: {second: {third: 3}}})).to eq(true)
46
+ expect(subject.exists?({one: {two: {three: 3}}})).to eq(true)
47
47
  end
48
48
 
49
49
  it "returns false if the field does not exist within the specified document" do
50
- expect(subject.exists?({first: 1})).to eq(false)
50
+ expect(subject.exists?({one: 1})).to eq(false)
51
51
  end
52
52
  end
53
53
 
54
54
  describe "#to_s" do
55
55
  it "returns the correct value for a simple field path" do
56
- field = Reorm::FieldPath.new(:first)
57
- expect(field.to_s).to eq("first")
56
+ field = Reorm::FieldPath.new(:one)
57
+ expect(field.to_s).to eq("one")
58
58
  end
59
59
 
60
60
  it "returns the correct value for a more complicated field path" do
61
- field = Reorm::FieldPath.new(:first, :second, :third)
62
- expect(field.to_s).to eq("first -> second -> third")
61
+ field = Reorm::FieldPath.new(:one, :two, :three)
62
+ expect(field.to_s).to eq("one -> two -> three")
63
63
  end
64
64
  end
65
65
  end
@@ -22,9 +22,11 @@ end
22
22
 
23
23
  class SaveTestModel < Reorm::Model
24
24
  after_create :on_after_create
25
+ after_delete :on_after_delete
25
26
  after_save :on_after_save
26
27
  after_update :on_after_update
27
28
  before_create :on_before_create
29
+ before_delete :on_before_delete
28
30
  before_save :on_before_save
29
31
  before_update :on_before_update
30
32
 
@@ -42,6 +44,10 @@ class SaveTestModel < Reorm::Model
42
44
  @events << :after_create
43
45
  end
44
46
 
47
+ def on_after_delete
48
+ @events << :after_delete
49
+ end
50
+
45
51
  def on_after_save
46
52
  @events << :after_save
47
53
  end
@@ -54,6 +60,10 @@ class SaveTestModel < Reorm::Model
54
60
  @events << :before_create
55
61
  end
56
62
 
63
+ def on_before_delete
64
+ @events << :before_delete
65
+ end
66
+
57
67
  def on_before_save
58
68
  @events << :before_save
59
69
  end
@@ -232,6 +242,55 @@ describe Reorm::Model do
232
242
  end
233
243
  end
234
244
 
245
+ describe "#update()" do
246
+ subject {
247
+ SaveTestModel.new(one: 1, two: 2, three: 3)
248
+ }
249
+
250
+ it "updates the properties for the model object and calls save()" do
251
+ expect(subject).to receive(:save)
252
+ subject.update(one: "One", four: 4)
253
+ expect(subject.one).to eq("One")
254
+ expect(subject.two).to eq(2)
255
+ expect(subject.three).to eq(3)
256
+ expect(subject.four).to eq(4)
257
+ end
258
+
259
+ it "does not calls save if the no properties are specified" do
260
+ expect(subject).not_to receive(:save)
261
+ subject.update()
262
+ end
263
+ end
264
+
265
+ describe "#delete()" do
266
+ subject {
267
+ SaveTestModel.create(one: 1, two: 2, three: 3)
268
+ }
269
+
270
+ it "removes the record for the model instance deleted" do
271
+ id = subject.id
272
+ subject.delete
273
+ expect(subject.id).to be_nil
274
+ expect(SaveTestModel.filter(id: id).count).to eq(0)
275
+ end
276
+
277
+ it "does nothing if the model instance has not been saved" do
278
+ instance = SaveTestModel.new(one: 1, two: 2, three: 3)
279
+ expect {
280
+ instance.delete
281
+ }.not_to raise_exception
282
+ subject
283
+ expect(SaveTestModel.all.count).to eq(1)
284
+ end
285
+
286
+ it "fires the before and after delete events" do
287
+ instance = SaveTestModel.create(one: 1, two: 2)
288
+ instance.delete
289
+ expect(instance.events).to include(:before_delete)
290
+ expect(instance.events).to include(:after_delete)
291
+ end
292
+ end
293
+
235
294
  describe "#[]()" do
236
295
  subject {
237
296
  SaveTestModel.new(one: 1, two: "Two")
@@ -263,6 +322,73 @@ describe Reorm::Model do
263
322
  end
264
323
  end
265
324
 
325
+ describe "#has_property?()" do
326
+ subject {
327
+ SaveTestModel.new(one: 1, two: "Two")
328
+ }
329
+
330
+ it "returns true when a model possesses the named property" do
331
+ expect(subject.has_property?(:two)).to eq(true)
332
+ end
333
+
334
+ it "returns false when a model does not possess the named property" do
335
+ expect(subject.has_property?(:three)).to eq(false)
336
+ end
337
+ end
338
+
339
+ describe "#get_property()" do
340
+ subject {
341
+ SaveTestModel.new(one: 1, two: "Two")
342
+ }
343
+
344
+ it "returns the value of the specified property" do
345
+ expect(subject.get_property(:one)).to eq(1)
346
+ expect(subject.get_property(:two)).to eq("Two")
347
+ end
348
+
349
+ it "returns nil of a property does not exist" do
350
+ expect(subject.get_property(:three)).to be_nil
351
+ end
352
+ end
353
+
354
+ describe "#set_property()" do
355
+ subject {
356
+ SaveTestModel.new(one: 1, two: "Two")
357
+ }
358
+
359
+ it "updates the value of an existing property" do
360
+ subject.set_property(:two, 2)
361
+ expect(subject.two).to eq(2)
362
+ end
363
+
364
+ it "assigns a value to a property that did not previously exist" do
365
+ subject.set_property(:three, 3)
366
+ expect(subject.three).to eq(3)
367
+ end
368
+ end
369
+
370
+ describe "#set_proerties()" do
371
+ subject {
372
+ SaveTestModel.new(one: 1, two: "Two", three: 3)
373
+ }
374
+
375
+ it "updates existing properties" do
376
+ subject.set_properties(one: "One", two: 2, three: "Three")
377
+ expect(subject.one).to eq("One")
378
+ expect(subject.two).to eq(2)
379
+ expect(subject.three).to eq("Three")
380
+ end
381
+
382
+ it "assigns properties that had not been previously set" do
383
+ subject.set_properties(four: 4, five: "Five")
384
+ expect(subject.one).to eq(1)
385
+ expect(subject.two).to eq("Two")
386
+ expect(subject.three).to eq(3)
387
+ expect(subject.four).to eq(4)
388
+ expect(subject.five).to eq("Five")
389
+ end
390
+ end
391
+
266
392
  describe "#create()" do
267
393
  let(:standin) {
268
394
  SaveTestModel.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reorm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Wood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-08 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -157,7 +157,6 @@ files:
157
157
  - lib/reorm/validators/validator.rb
158
158
  - lib/reorm/version.rb
159
159
  - reorm.gemspec
160
- - spec/catwalk/modules/timestamped_spec.rb
161
160
  - spec/reorm/cursor_spec.rb
162
161
  - spec/reorm/field_path_spec.rb
163
162
  - spec/reorm/model_spec.rb
@@ -198,7 +197,6 @@ signing_key:
198
197
  specification_version: 4
199
198
  summary: A library for use with the RethinkDB application.
200
199
  test_files:
201
- - spec/catwalk/modules/timestamped_spec.rb
202
200
  - spec/reorm/cursor_spec.rb
203
201
  - spec/reorm/field_path_spec.rb
204
202
  - spec/reorm/model_spec.rb
@@ -1,17 +0,0 @@
1
- require "spec_helper"
2
-
3
- class TimestampedTestClass < Reorm::Model
4
- extend Reorm::Timestamped
5
- end
6
-
7
- describe Reorm::Timestamped do
8
- subject {
9
- TimestampedTestClass
10
- }
11
-
12
- it "set the timestamp fields when a record is created" do
13
- record = subject.create(label: "First")
14
- expect(record.created_at).not_to be_nil
15
- expect(record.updated_at).to be_nil
16
- end
17
- end