whi-cassie 1.0.0 → 1.0.1

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: b5b36ebf78a28b63c80478d20c672bef7a411250
4
- data.tar.gz: ffaeaaebd95342e98c91d04840934cfbd2540bb4
3
+ metadata.gz: 03ff68291018701bf3bbd15e8dd2d018f4ef1b65
4
+ data.tar.gz: 4924128158a74937bf8a307f4208417eec9e2ce0
5
5
  SHA512:
6
- metadata.gz: 81b226510b09bfddf2f9fcd07910a97deecacb668964676ebc1393637807173c551e84a173706dee4994473858fd1a03ea00292601ed79712be514dcae1a75fd
7
- data.tar.gz: 48a2d80f9737f34b6850be20eff36f179e81a751558b58e9dcff80b6a54627dd5874ec299ab095679689318fe67824b18afb9b2b94d751c46162d5fd2269fc82
6
+ metadata.gz: e76d10e1818452fbcea764a54cfdb67bdfa76ca95743264abb3e8e195fa180af7f8cbd7993042544522aa629ef4f7dff3318230f68a28e623d50b77a13ccc95f
7
+ data.tar.gz: faec19ce6868879cf5c9e8d85ddacfdb8f394d466d314b666735a5b65c3d863ae9eea5be3e91deb96b92d9458a04dd3c608870e611c7e4a95e37d33b52aecbda
data/HISTORY.txt CHANGED
@@ -1,3 +1,7 @@
1
1
  1.0.0
2
2
 
3
3
  Intial Release
4
+
5
+ 1.0.1
6
+
7
+ Allow finding the offset to the row from a range.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/lib/cassie/model.rb CHANGED
@@ -288,13 +288,16 @@ module Cassie::Model
288
288
  #
289
289
  # The order argument can be used to specify an order for the ordering key (:asc or :desc).
290
290
  # It will default to the natural order of the last ordering key as defined by the ordering_key method.
291
- def offset_to_id(key, offset, order: nil, batch_size: 1000)
291
+ #
292
+ # The min and max can be used to limit the offset calculation to a range of values (exclusive).
293
+ def offset_to_id(key, offset, order: nil, batch_size: 1000, min: nil, max: nil)
292
294
  ordering_key = primary_key.last
293
295
  cluster_order = _ordering_keys[ordering_key] || :asc
294
296
  order ||= cluster_order
295
297
  order_cql = "#{ordering_key} #{order}" unless order == cluster_order
296
298
 
297
- from = nil
299
+ from = (order == :desc ? max : min)
300
+ to = (order == :desc ? min : max)
298
301
  loop do
299
302
  limit = (offset > batch_size ? batch_size : offset + 1)
300
303
  conditions_cql = []
@@ -303,12 +306,16 @@ module Cassie::Model
303
306
  conditions_cql << "#{ordering_key} #{order == :desc ? '<' : '>'} ?"
304
307
  conditions << from
305
308
  end
309
+ if to
310
+ conditions_cql << "#{ordering_key} #{order == :desc ? '>' : '<'} ?"
311
+ conditions << to
312
+ end
306
313
  key.each do |name, value|
307
314
  conditions_cql << "#{column_name(name)} = ?"
308
315
  conditions << value
309
316
  end
310
317
  conditions.unshift(conditions_cql.join(" AND "))
311
-
318
+
312
319
  results = find_all(:select => [ordering_key], :where => conditions, :limit => limit, :order => order_cql)
313
320
  last_row = results.last if results.size == limit
314
321
  last_id = last_row.send(ordering_key) if last_row
@@ -97,7 +97,7 @@ describe Cassie::Model do
97
97
  end
98
98
 
99
99
  it "won't find all records with a blank where clause" do
100
- expect{ Cassie::Thing.find_all(where: {}) }.to raise_error
100
+ expect{ Cassie::Thing.find_all(where: {}) }.to raise_error(ArgumentError)
101
101
  Cassie::Thing.find_all(where: :all).size.should == 3
102
102
  end
103
103
  end
@@ -116,6 +116,8 @@ describe Cassie::Model do
116
116
  Cassie::Thing.offset_to_id({:owner => 1}, 3, batch_size: 1).should == 2
117
117
  Cassie::Thing.offset_to_id({:owner => 1}, 4, batch_size: 1).should == nil
118
118
  Cassie::Thing.offset_to_id({:owner => 1}, 4, batch_size: 100).should == nil
119
+ Cassie::Thing.offset_to_id({:owner => 1}, 1, batch_size: 1, min: 3).should == 4
120
+ Cassie::Thing.offset_to_id({:owner => 1}, 1, order: :desc, batch_size: 1, max: 5).should == 3
119
121
  end
120
122
  end
121
123
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whi-cassie
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - We Heart It
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-24 00:00:00.000000000 Z
12
+ date: 2016-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cassandra-driver