riak-record 0.4.1 → 0.5.0

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: b00a705964f29ab77910a325b38f3bebb60043bd
4
- data.tar.gz: b66150ea829d5c1c5d789a1f4e44c876217f05b5
3
+ metadata.gz: f9fc3f21c82333d4407dafb67bbb07f7541cae9a
4
+ data.tar.gz: d09d48df6dd73ba14739d052554e6cbde3aab138
5
5
  SHA512:
6
- metadata.gz: c8cc3bdb8ab3e991d0414311d557db8155eb5f7a990d140990cc52daab2c9af729e16dfcc06fdea157ef32846622754881c5c36cd35dc89d4855d0b151cfde7a
7
- data.tar.gz: b77edf9f28a069979e5c314fe8431088badf93a2acda46d8f1272ed7df4abc6f8ed4b2eb05e26dd0d5ea84826f656ada0a16eeee98460a01f7e43e496df6f065
6
+ metadata.gz: 6824a1da62874f8531ae438ff5584d130352cf10ba1c66eb555868ab9b90d3aff6f0a15a3a60dffd6954a2e99bdbe7294e43b9430c8a79fbf01ca1e92c3adc16
7
+ data.tar.gz: 1cd486fbfeda0b2fdedd06314c0045d733218acc91de0f1be09bd98329bc8f2c62fe237e430be0154256e9a959cd2c9076d608df8e1990703af49e69d2db1b13
data/README.md CHANGED
@@ -27,6 +27,9 @@ end
27
27
 
28
28
  Post.client #> instance of Riak::Client
29
29
  Post.bucket #> instance of Riak::Bucket named "staging:posts"
30
+ Post.all #> [] uses the special $bucket secondary index
31
+ Post.count #> 0 also uses the special $bucket index
32
+ records, next_page = Post.page(2, 100) #> returns records 101-200 using the $bucket index.
30
33
 
31
34
  Post.find(["my-first-post","a-farewell-to-blogging"]) #> Array of Posts returned
32
35
  post = Post.find("my-first-post") #> Instance of Post
@@ -60,6 +63,7 @@ RiakRecord::Finder provides find objects by indexes. Results are loaded in batch
60
63
 
61
64
  ```ruby
62
65
  finder = Post.where(:category => 'ruby') #> Instance of RiakRecord::Finder
66
+ finder.page(1) # returns the first 100 records. Note it has to stream in previous pages
63
67
  finder.count #> 1
64
68
  finder.any? #> true
65
69
  finder.any?{|o| o.category == 'php'} #> false
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.5.0
@@ -108,6 +108,26 @@ module RiakRecord
108
108
  @bucket ||= client.bucket(bucket_name)
109
109
  end
110
110
 
111
+ def self.finder
112
+ RiakRecord::Finder.new(self, :bucket => bucket_name)
113
+ end
114
+
115
+ def self.all
116
+ finder.all
117
+ end
118
+
119
+ def self.count
120
+ finder.count
121
+ end
122
+
123
+ def self.first(n = 1)
124
+ finder.first(n)
125
+ end
126
+
127
+ def self.page(page_number = 1, page_size = 100)
128
+ finder.page(page_number, page_size)
129
+ end
130
+
111
131
  def self.data_attributes(*attributes)
112
132
  attributes.map(&:to_sym).each do |method_name|
113
133
  define_method(method_name) do
@@ -149,7 +169,7 @@ module RiakRecord
149
169
  end
150
170
 
151
171
  def self.index_names
152
- @index_names ||= {}
172
+ @index_names ||= { :bucket => '$bucket' }
153
173
  end
154
174
 
155
175
  def self.where(options)
@@ -90,6 +90,25 @@ module RiakRecord
90
90
  results
91
91
  end
92
92
 
93
+ def page(page_number = 1, page_size = 100)
94
+ current_page = 1
95
+ page_number = page_number.to_i
96
+ page_number = 1 if page_number < 1
97
+
98
+ querier = Riak::SecondaryIndex.new(@bucket, @index, @value, :max_results => page_size)
99
+ while current_page < page_number
100
+ if querier.has_next_page?
101
+ querier = querier.next_page
102
+ current_page = current_page + 1
103
+ else
104
+ return [], false # no results, no next page
105
+ end
106
+ end
107
+
108
+ results = querier.values.compact.map{ |robject| @finder_class.new(robject) }
109
+ return results, querier.has_next_page?
110
+ end
111
+
93
112
  private
94
113
 
95
114
  def load_started?
@@ -120,7 +139,7 @@ module RiakRecord
120
139
  @querier = Riak::SecondaryIndex.new(@bucket, @index, @value, :max_results => @page_size)
121
140
  end
122
141
  @load_complete = !@querier.has_next_page?
123
- new_objects = @querier.values.map{ |robject| @finder_class.new(robject) }
142
+ new_objects = @querier.values.compact.map{ |robject| @finder_class.new(robject) }
124
143
  @loaded_objects.concat(new_objects)
125
144
  new_objects
126
145
  end
data/riak-record.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: riak-record 0.4.1 ruby lib
5
+ # stub: riak-record 0.5.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "riak-record"
9
- s.version = "0.4.1"
9
+ s.version = "0.5.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Robert Graff"]
14
- s.date = "2014-10-03"
14
+ s.date = "2014-10-15"
15
15
  s.description = "RiakRecord is a thin and immature wrapper around riak-ruby-client. It creates a bucket for\n each class, provides a simple finder, and creates attribute reader."
16
16
  s.email = "robert_graff@yahoo.com"
17
17
  s.extra_rdoc_files = [
@@ -34,7 +34,6 @@ Gem::Specification.new do |s|
34
34
  "lib/riak_record/base.rb",
35
35
  "lib/riak_record/callbacks.rb",
36
36
  "lib/riak_record/finder.rb",
37
- "riak-record-0.1.0.gem",
38
37
  "riak-record.gemspec",
39
38
  "spec/riak_record/associations_spec.rb",
40
39
  "spec/riak_record/base_spec.rb",
@@ -234,6 +234,30 @@ describe RiakRecord::Base do
234
234
  end
235
235
  end
236
236
 
237
+ describe "finder" do
238
+ it "should return a finder for the bucket" do
239
+ expect( ExampleB.finder ).to be_an_instance_of(RiakRecord::Finder)
240
+ end
241
+
242
+ describe "uses of class finder" do
243
+ let!(:record1) { ExampleB.new("1").save }
244
+ let!(:record2) { ExampleB.new("2").save }
245
+
246
+ describe "all" do
247
+ it "should return all the objects in the bucket" do
248
+ expect(ExampleB.all).to eq [record1, record2]
249
+ end
250
+ end
251
+
252
+ describe "count" do
253
+ it "should count all objects in the bucket" do
254
+ sleep(2) # wait for riak to collect ghosts
255
+ expect(ExampleB.count).to eq(2)
256
+ end
257
+ end
258
+ end
259
+ end
260
+
237
261
  describe "data_attributes" do
238
262
  let(:riak_object) { Riak::RObject.new("obj").tap{|r| r.data = data } }
239
263
  let(:data) { {'attribute1' => "1"} }
@@ -62,6 +62,27 @@ describe RiakRecord::Finder do
62
62
  end
63
63
  end
64
64
 
65
+ describe "page" do
66
+ it "should return a page of results" do
67
+ results, next_page = pop_finder.page(1,10)
68
+ expect(results.map(&:id).sort).to eq(pop_finder.first(10).map(&:id).sort)
69
+ expect(next_page).to eq(true)
70
+ end
71
+
72
+ it "should return an empty result when the page is to big" do
73
+ results, next_page = pop_finder.page(100,10)
74
+ expect(results).to be_empty
75
+ expect(next_page).to eq(false)
76
+ end
77
+
78
+ it "should return results and false on last page" do
79
+ results, next_page = pop_finder.page(2, 100)
80
+ expect(results).to_not be_empty
81
+ expect(next_page).to eq(false)
82
+ end
83
+
84
+ end
85
+
65
86
  describe "enumberable methods" do
66
87
  it "should yield once per block" do
67
88
  expect( pop_finder.all?{|o| o.category == ['pop']} ).to eq(true)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riak-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Graff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-03 00:00:00.000000000 Z
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: riak-client
@@ -161,7 +161,6 @@ files:
161
161
  - lib/riak_record/base.rb
162
162
  - lib/riak_record/callbacks.rb
163
163
  - lib/riak_record/finder.rb
164
- - riak-record-0.1.0.gem
165
164
  - riak-record.gemspec
166
165
  - spec/riak_record/associations_spec.rb
167
166
  - spec/riak_record/base_spec.rb
Binary file