reorm 0.1.8 → 0.1.11

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: 0926ae773897fa1ab0198afe1c4577ccfdeea99c
4
- data.tar.gz: 2f6d59a5ff0e3bbc41daa49262d62a20007f0956
3
+ metadata.gz: 9fbf3a40fc80bdd3c3803014234f9fadcde74ad3
4
+ data.tar.gz: 38d870a011a1fd0872ef64f63f63b53bc70dd7c9
5
5
  SHA512:
6
- metadata.gz: 048f6fd28385581a438c97804d93fc4e2e384af5e2e6f5efcaf35f26a810a6fa74c4f03cf32c62c0836eae0f78201d86216e0244a91d6c4ed9bc17d69212dffb
7
- data.tar.gz: 4c1198b1673b3b7867c49caaeef97ed0a7dfd3c2b1e266223305e159973d887ec90cb05d6cc7db6810aecd9d5cd2d23e184664c3abe8706629e9011a20b53b47
6
+ metadata.gz: 3a4b190166c7882ebfa1a5c4c1d42ff54da07948b4b4c0930234f03d738d1482068974df822ad67c4167353df70a40b32f8ffff08dfeacf24180014aeb102920
7
+ data.tar.gz: 4036786a08d0ded1844089cfa56b7d4b6871f02bd4c4a999b481c76949803ac1b630ece858ab648c4ef97c951517e032cd7b19aab199b1f471b82fa56b302159
data/lib/reorm/cursor.rb CHANGED
@@ -60,7 +60,7 @@ module Reorm
60
60
  end
61
61
  data = @order_by.nil? ? @cursor.next : @cursor[@offset]
62
62
  @offset += 1
63
- model_class.new(data)
63
+ model_for(data)
64
64
  end
65
65
 
66
66
  def each(&block)
@@ -78,7 +78,7 @@ module Reorm
78
78
  model = nil
79
79
  if offset >= 0 && offset < count
80
80
  Reorm.connection do |connection|
81
- model = model_class.new(@query.nth(offset).run(connection))
81
+ model = model_for(@query.nth(offset).run(connection))
82
82
  end
83
83
  end
84
84
  model
@@ -100,7 +100,7 @@ module Reorm
100
100
  Cursor.new(model_class, @query.limit(size), @order_by)
101
101
  end
102
102
 
103
- def offset(index)
103
+ def offset(quantity)
104
104
  Cursor.new(model_class, @query.skip(quantity), @order_by)
105
105
  end
106
106
  alias :skip :offset
@@ -151,7 +151,7 @@ module Reorm
151
151
  def each_with_order_by
152
152
  Reorm.connection do |connection|
153
153
  @query.order_by(*@order_by).run(connection).each do |record|
154
- yield model_class.new(record)
154
+ yield model_for(record)
155
155
  end
156
156
  end
157
157
  end
@@ -161,12 +161,18 @@ module Reorm
161
161
  cursor = @query.run(connection)
162
162
  begin
163
163
  cursor.each do |record|
164
- yield model_class.new(record)
164
+ yield model_for(record)
165
165
  end
166
166
  ensure
167
167
  cursor.close
168
168
  end
169
169
  end
170
170
  end
171
+
172
+ def model_for(properties)
173
+ model = model_class.new
174
+ model.assign_properties(properties)
175
+ model
176
+ end
171
177
  end
172
178
  end
data/lib/reorm/model.rb CHANGED
@@ -123,6 +123,13 @@ module Reorm
123
123
  self
124
124
  end
125
125
 
126
+ def assign_properties(properties={})
127
+ properties.each do |key, value|
128
+ @properties[key.to_sym] = value
129
+ end
130
+ self
131
+ end
132
+
126
133
  def respond_to?(method_name, include_private=false)
127
134
  method_name.to_s[-1, 1] == "=" || @properties.include?(property_name(method_name)) || super
128
135
  end
@@ -143,6 +150,20 @@ module Reorm
143
150
  {}.merge(@properties)
144
151
  end
145
152
 
153
+ def self.get(id)
154
+ model = nil
155
+ Reorm.connection do |connection|
156
+ if table_exists?(table_name, connection)
157
+ properties = r.table(table_name).get(id).run(connection)
158
+ if !properties.nil?
159
+ model = self.new
160
+ model.assign_properties(properties)
161
+ end
162
+ end
163
+ end
164
+ model
165
+ end
166
+
146
167
  def self.create(properties={})
147
168
  object = self.new(properties)
148
169
  object.save
@@ -172,10 +193,17 @@ module Reorm
172
193
  end
173
194
 
174
195
  def ensure_table_exists(connection)
175
- tables = r.table_list.run(connection)
176
- if !tables.include?(table_name)
196
+ if !table_exists?(table_name, connection)
177
197
  r.table_create(table_name, primary_key: primary_key).run(connection)
178
198
  end
179
199
  end
200
+
201
+ def table_exists?(name, connection)
202
+ Model.table_exists?(name, connection)
203
+ end
204
+
205
+ def self.table_exists?(name, connection)
206
+ r.table_list.run(connection).include?(name)
207
+ end
180
208
  end
181
209
  end
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 = 8
4
+ BUILD_VERSION = 11
5
5
  VERSION = "#{MAJOR_VERSION}.#{MINOR_VERSION}.#{BUILD_VERSION}"
6
6
  end
@@ -221,4 +221,18 @@ describe Reorm::Cursor do
221
221
  expect(CursorTestModel.all.count).to eq(2)
222
222
  end
223
223
  end
224
+
225
+ describe "#offset()" do
226
+ it "moves the starting point for records retrieved" do
227
+ expect(subject.count).to eq(5)
228
+ expect(subject.offset(2).count).to eq(3)
229
+ end
230
+ end
231
+
232
+ describe "#limit()" do
233
+ it "restricts the number of records to be retrieved" do
234
+ expect(subject.count).to eq(5)
235
+ expect(subject.limit(2).count).to eq(2)
236
+ end
237
+ end
224
238
  end
@@ -389,6 +389,35 @@ describe Reorm::Model do
389
389
  end
390
390
  end
391
391
 
392
+ describe "#assign_properties()" do
393
+ subject {
394
+ SetterTestModel.new
395
+ }
396
+
397
+ it "assigns model properties but bypasses explicit assignment methods" do
398
+ subject.assign_properties(value: "Value.")
399
+ expect(subject.value).to eq("Value.")
400
+ expect(subject.called).to be_nil
401
+ end
402
+ end
403
+
404
+ describe "#get()" do
405
+ let(:model) {
406
+ SaveTestModel.create(one: 1)
407
+ }
408
+
409
+ it "returns a model instance that matches the key if one exists" do
410
+ object = SaveTestModel.get(model.id)
411
+ expect(object).not_to be_nil
412
+ expect(object.id).to eq(model.id)
413
+ expect(object.one).to eq(1)
414
+ end
415
+
416
+ it "returns nil if a match record could not be found" do
417
+ expect(SaveTestModel.get(SecureRandom.uuid)).to be_nil
418
+ end
419
+ end
420
+
392
421
  describe "#create()" do
393
422
  let(:standin) {
394
423
  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.8
4
+ version: 0.1.11
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-13 00:00:00.000000000 Z
11
+ date: 2015-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -192,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  requirements: []
194
194
  rubyforge_project:
195
- rubygems_version: 2.4.5
195
+ rubygems_version: 2.4.4
196
196
  signing_key:
197
197
  specification_version: 4
198
198
  summary: A library for use with the RethinkDB application.