mongoid-scroll 0.3.0 → 0.3.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: 614ee5395d9475c74547769cefbfd7c531b09d45
4
- data.tar.gz: 9a61c61ce360c352a4b69fd770eca27f1ee9a6c7
3
+ metadata.gz: b18caa6adcba10267216ab4c06890232de3ec0cd
4
+ data.tar.gz: b05d520e679bb1d49806470546a546624f0574bf
5
5
  SHA512:
6
- metadata.gz: 05e6f26d88ad8515bb3d0871bed4ea5386d3fb801300394ffedbdb4529833b2fb19ed73da362267e35cba02fba6b11c74e4d8ca4bfecb93cb389bc2408967c84
7
- data.tar.gz: 3511a0273d3c198987a5812cfb137083216373e6602d03cbfe828ad0fa8c65d4fc577c20598bb1a8239c480d4f891d2179fa89e382b942347e01a063c4bd18a1
6
+ metadata.gz: 7c4c3bbe7deeb838e893f3c19de0160b64b9e4ee66d7c12dbca872181e56c51416c0355d87acb3dd5a33db0d504b11f396c4a59559c2592bf78c74437e9fba66
7
+ data.tar.gz: b5a55c6f00c90ae8eed3f3ee3153d07b17159d8c7129b4e5dab3d4719a9d6147804ba20cd00e2c6b8825638f3d77102dafd3556557b6deb92664c79cd4002911
data/.rubocop.yml CHANGED
@@ -1,64 +1,8 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
1
3
  AllCops:
2
4
  Exclude:
3
5
  - vendor/**/*
4
6
  - gemfiles/vendor/**/*
5
7
 
6
- LineLength:
7
- Enabled: false
8
-
9
- MethodLength:
10
- Enabled: false
11
-
12
- ClassLength:
13
- Enabled: false
14
-
15
- Documentation:
16
- # don't require classes to be documented
17
- Enabled: false
18
-
19
- CollectionMethods:
20
- # don't prefer map to collect, recuce to inject
21
- Enabled: false
22
-
23
- Encoding:
24
- # no need to always specify encoding
25
- Enabled: false
26
-
27
- Void:
28
- # == operator used in void context in specs
29
- Enabled: false
30
-
31
- SignalException:
32
- # prefer raise to fail
33
- EnforcedStyle: only_raise
34
-
35
- RaiseArgs:
36
- # don't care for what kind of raise
37
- Enabled: false
38
-
39
- PerlBackrefs:
40
- # TODO: regular expression matching with $1, $2, etc.
41
- Enabled: false
42
-
43
- BlockNesting:
44
- # TODO: fix too much nesting
45
- Max: 4
46
-
47
- Lambda:
48
- # TODO: replace all lambda with -> or Proc
49
- Enabled: false
50
-
51
- Blocks:
52
- # allow multi-line blocks like expect { }
53
- Enabled: false
54
-
55
- WordArray:
56
- # %w vs. [ '', ... ]
57
- Enabled: false
58
-
59
- CyclomaticComplexity:
60
- Enabled: false
61
-
62
- FileName:
63
- Enabled: false
64
8
 
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,42 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-07-07 10:17:35 -0400 using RuboCop version 0.32.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 4
9
+ Metrics/AbcSize:
10
+ Max: 70
11
+
12
+ # Offense count: 5
13
+ Metrics/CyclomaticComplexity:
14
+ Max: 11
15
+
16
+ # Offense count: 78
17
+ # Configuration parameters: AllowURI, URISchemes.
18
+ Metrics/LineLength:
19
+ Max: 170
20
+
21
+ # Offense count: 4
22
+ # Configuration parameters: CountComments.
23
+ Metrics/MethodLength:
24
+ Max: 26
25
+
26
+ # Offense count: 3
27
+ Metrics/PerceivedComplexity:
28
+ Max: 12
29
+
30
+ # Offense count: 12
31
+ Style/Documentation:
32
+ Enabled: false
33
+
34
+ # Offense count: 1
35
+ # Configuration parameters: Exclude.
36
+ Style/FileName:
37
+ Enabled: false
38
+
39
+ # Offense count: 6
40
+ # Configuration parameters: SupportedStyles.
41
+ Style/RaiseArgs:
42
+ EnforcedStyle: compact
data/.travis.yml CHANGED
@@ -11,6 +11,7 @@ rvm:
11
11
  env:
12
12
  - MONGOID_VERSION=3
13
13
  - MONGOID_VERSION=4
14
+ - MONGOID_VERSION=5.0.0.beta
14
15
 
15
16
  language: ruby
16
17
 
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ 0.3.1 (7/27/2015)
2
+ ------------
3
+
4
+ * Compatibility with Mongoid 5.x beta - [@dblock](https://github.com/dblock).
5
+ * [#4](https://github.com/dblock/mongoid-scroll/pull/4): Fix: support chaining `$or` criteria - [@sweir27](https://github.com/sweir27).
6
+ * [#5](https://github.com/dblock/mongoid-scroll/pull/5): Fix: embeddable objects now returned in pagination - [@sweir27](https://github.com/sweir27).
7
+
1
8
  0.3.0 (1/7/2014)
2
9
  ----------------
3
10
 
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'http://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
@@ -12,9 +12,10 @@ else
12
12
  end
13
13
 
14
14
  group :development, :test do
15
- gem "rake"
16
- gem "bundler"
17
- gem "rspec", "~> 2.9"
18
- gem "faker"
19
- gem "rubocop", "0.24.0"
15
+ gem 'rake'
16
+ gem 'bundler'
17
+ gem 'rspec', '~> 3.0'
18
+ gem 'rspec-its'
19
+ gem 'faker'
20
+ gem 'rubocop', '0.32.1'
20
21
  end
data/README.md CHANGED
@@ -1,8 +1,19 @@
1
- Mongoid::Scroll [![Build Status](https://travis-ci.org/dblock/mongoid-scroll.png?branch=master)](https://travis-ci.org/dblock/mongoid-scroll)
1
+ Mongoid::Scroll
2
2
  ===============
3
3
 
4
+ [![Gem Version](http://img.shields.io/gem/v/mongoid-scroll.svg)](http://badge.fury.io/rb/mongoid-scroll)
5
+ [![Build Status](http://img.shields.io/travis/dblock/mongoid-scroll.svg)](https://travis-ci.org/dblock/mongoid-scroll)
6
+ [![Dependency Status](https://gemnasium.com/dblock/mongoid-scroll.svg)](https://gemnasium.com/dblock/mongoid-scroll)
7
+ [![Code Climate](https://codeclimate.com/github/dblock/mongoid-scroll.svg)](https://codeclimate.com/github/dblock/mongoid-scroll)
8
+
9
+
4
10
  Mongoid extension that enables infinite scrolling for `Mongoid::Criteria` and `Moped::Query`.
5
11
 
12
+ Compatibility
13
+ -------------
14
+
15
+ This gem supports Mongoid 3, 4 and 5 beta.
16
+
6
17
  Demo
7
18
  ----
8
19
 
@@ -8,8 +8,8 @@ require 'mongoid/scroll/mongoid'
8
8
  require 'mongoid/scroll/errors'
9
9
  require 'mongoid/scroll/cursor'
10
10
 
11
- require 'moped/scrollable'
11
+ require 'moped/scrollable' if Object.const_defined?(:Moped)
12
12
  require 'mongoid/criterion/scrollable'
13
13
 
14
- Moped::Query.send(:include, Moped::Scrollable)
14
+ Moped::Query.send(:include, Moped::Scrollable) if Object.const_defined?(:Moped)
15
15
  Mongoid::Criteria.send(:include, Mongoid::Criterion::Scrollable)
@@ -5,7 +5,7 @@ module Mongoid
5
5
  criteria = self
6
6
  # we don't support scrolling over a criteria with multiple fields
7
7
  if criteria.options[:sort] && criteria.options[:sort].keys.size != 1
8
- raise Mongoid::Scroll::Errors::MultipleSortFieldsError.new(sort: criteria.options[:sort])
8
+ fail Mongoid::Scroll::Errors::MultipleSortFieldsError.new(sort: criteria.options[:sort])
9
9
  elsif !criteria.options.key?(:sort) || criteria.options[:sort].empty?
10
10
  # introduce a default sort order if there's none
11
11
  criteria = criteria.asc(:_id)
@@ -19,7 +19,9 @@ module Mongoid
19
19
  cursor = cursor.is_a?(Mongoid::Scroll::Cursor) ? cursor : Mongoid::Scroll::Cursor.new(cursor, cursor_options)
20
20
  # scroll
21
21
  if block_given?
22
- criteria.where(cursor.criteria).order_by(_id: scroll_direction).each do |record|
22
+ cursor_criteria = criteria.dup
23
+ cursor_criteria.selector = { '$and' => [criteria.selector, cursor.criteria] }
24
+ cursor_criteria.order_by(_id: scroll_direction).each do |record|
23
25
  yield record, Mongoid::Scroll::Cursor.from_record(record, cursor_options)
24
26
  end
25
27
  else
@@ -37,7 +37,7 @@ module Mongoid
37
37
  return unless value
38
38
  parts = value.split(':')
39
39
  unless parts.length >= 2
40
- raise Mongoid::Scroll::Errors::InvalidCursorError.new(cursor: value)
40
+ fail Mongoid::Scroll::Errors::InvalidCursorError.new(cursor: value)
41
41
  end
42
42
  id = parts[-1]
43
43
  value = parts[0...-1].join(':')
@@ -56,7 +56,7 @@ module Mongoid
56
56
  elsif options && (field = options[:field])
57
57
  [field.type.to_s, field.name.to_s]
58
58
  else
59
- raise ArgumentError.new 'Missing options[:field_name] and/or options[:field_type].'
59
+ fail ArgumentError.new 'Missing options[:field_name] and/or options[:field_type].'
60
60
  end
61
61
  end
62
62
 
@@ -70,7 +70,7 @@ module Mongoid
70
70
  when 'Float' then value.to_f
71
71
  when 'Integer' then value.to_i
72
72
  else
73
- raise Mongoid::Scroll::Errors::UnsupportedFieldTypeError.new(field: field_name, type: field_type)
73
+ fail Mongoid::Scroll::Errors::UnsupportedFieldTypeError.new(field: field_name, type: field_type)
74
74
  end
75
75
  end
76
76
 
@@ -79,11 +79,11 @@ module Mongoid
79
79
  when 'BSON::ObjectId', 'Moped::BSON::ObjectId' then value
80
80
  when 'String' then value.to_s
81
81
  when 'Date' then Time.utc(value.year, value.month, value.day).to_i
82
- when 'DateTime', 'Time' then value.to_i
82
+ when 'DateTime', 'Time' then value.utc.to_i
83
83
  when 'Float' then value.to_f
84
84
  when 'Integer' then value.to_i
85
85
  else
86
- raise Mongoid::Scroll::Errors::UnsupportedFieldTypeError.new(field: field_name, type: field_type)
86
+ fail Mongoid::Scroll::Errors::UnsupportedFieldTypeError.new(field: field_name, type: field_type)
87
87
  end
88
88
  end
89
89
  end
@@ -1,7 +1,7 @@
1
1
  module Mongoid
2
2
  module Scroll
3
3
  def self.mongoid3?
4
- ::Mongoid.const_defined? :Observer
4
+ Mongoid::VERSION =~ /^3\./
5
5
  end
6
6
  end
7
7
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Scroll
3
- VERSION = '0.3.0'
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
@@ -11,7 +11,7 @@ module Moped
11
11
  query.operation.limit = operation.limit
12
12
  # we don't support scrolling over a criteria with multiple fields
13
13
  if query.operation.selector['$orderby'] && query.operation.selector['$orderby'].keys.size != 1
14
- raise Mongoid::Scroll::Errors::MultipleSortFieldsError.new(sort: query.operation.selector['$orderby'])
14
+ fail Mongoid::Scroll::Errors::MultipleSortFieldsError.new(sort: query.operation.selector['$orderby'])
15
15
  elsif !query.operation.selector.key?('$orderby') || query.operation.selector['$orderby'].empty?
16
16
  # introduce a default sort order if there's none
17
17
  query.sort(_id: 1)
@@ -1,18 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::Criteria do
4
- context 'scrollable' do
5
- subject do
6
- Feed::Item
7
- end
8
- it ':scroll' do
9
- subject.should.respond_to? :scroll
10
- end
11
- end
12
4
  context 'with multiple sort fields' do
13
5
  subject do
14
6
  Feed::Item.desc(:name).asc(:value)
15
7
  end
8
+ it ':scroll' do
9
+ expect(subject).to respond_to(:scroll)
10
+ end
16
11
  it 'raises Mongoid::Scroll::Errors::MultipleSortFieldsError' do
17
12
  expect { subject.scroll }.to raise_error Mongoid::Scroll::Errors::MultipleSortFieldsError,
18
13
  /You're attempting to scroll over data with a sort order that includes multiple fields: name, value./
@@ -23,7 +18,7 @@ describe Mongoid::Criteria do
23
18
  Feed::Item.all
24
19
  end
25
20
  it 'adds a default sort by _id' do
26
- subject.scroll.options[:sort].should == { '_id' => 1 }
21
+ expect(subject.scroll.options[:sort]).to eq('_id' => 1)
27
22
  end
28
23
  end
29
24
  context 'with data' do
@@ -32,7 +27,7 @@ describe Mongoid::Criteria do
32
27
  Feed::Item.create!(
33
28
  a_string: i.to_s,
34
29
  a_integer: i,
35
- a_datetime: DateTime.new(2013, i + 1, 21, 1, 42, 3),
30
+ a_datetime: DateTime.new(2013, i + 1, 21, 1, 42, 3, 'UTC'),
36
31
  a_date: Date.new(2013, i + 1, 21),
37
32
  a_time: Time.at(Time.now.to_i + i)
38
33
  )
@@ -44,8 +39,8 @@ describe Mongoid::Criteria do
44
39
  Feed::Item.all.scroll do |record, _next_cursor|
45
40
  records << record
46
41
  end
47
- records.size.should == 10
48
- records.should eq Feed::Item.all.to_a
42
+ expect(records.size).to eq 10
43
+ expect(records).to eq Feed::Item.all.to_a
49
44
  end
50
45
  end
51
46
  { a_string: String, a_integer: Integer, a_date: Date, a_datetime: DateTime }.each_pair do |field_name, field_type|
@@ -55,8 +50,8 @@ describe Mongoid::Criteria do
55
50
  Feed::Item.asc(field_name).scroll do |record, _next_cursor|
56
51
  records << record
57
52
  end
58
- records.size.should == 10
59
- records.should eq Feed::Item.all.to_a
53
+ expect(records.size).to eq 10
54
+ expect(records).to eq Feed::Item.all.to_a
60
55
  end
61
56
  it 'scrolls all with a break' do
62
57
  records = []
@@ -65,27 +60,28 @@ describe Mongoid::Criteria do
65
60
  records << record
66
61
  cursor = next_cursor
67
62
  end
68
- records.size.should == 5
63
+ expect(records.size).to eq 5
69
64
  Feed::Item.asc(field_name).scroll(cursor) do |record, next_cursor|
70
65
  records << record
71
66
  cursor = next_cursor
72
67
  end
73
- records.size.should == 10
74
- records.should eq Feed::Item.all.to_a
68
+ expect(records.size).to eq 10
69
+ expect(records).to eq Feed::Item.all.to_a
75
70
  end
76
71
  it 'scrolls in descending order' do
77
72
  records = []
78
73
  Feed::Item.desc(field_name).limit(3).scroll do |record, _next_cursor|
79
74
  records << record
80
75
  end
81
- records.size.should == 3
82
- records.should eq Feed::Item.desc(field_name).limit(3).to_a
76
+ expect(records.size).to eq 3
77
+ expect(records).to eq Feed::Item.desc(field_name).limit(3).to_a
83
78
  end
84
79
  it 'map' do
85
- record = Feed::Item.desc(field_name).limit(3).scroll.map { |r, _| r }.last
86
- cursor = Mongoid::Scroll::Cursor.from_record(record, field_type: field_type, field_name: field_name)
87
- cursor.should_not be_nil
88
- cursor.to_s.split(':').should == [
80
+ record = Feed::Item.desc(field_name).limit(3).scroll.map { |r| r }.last
81
+ expect(record).to_not be nil
82
+ cursor = Mongoid::Scroll::Cursor.from_record(record, field_type: field_type, field_name: field_name)
83
+ expect(cursor).to_not be nil
84
+ expect(cursor.to_s.split(':')).to eq [
89
85
  Mongoid::Scroll::Cursor.transform_field_value(field_type, field_name, record.send(field_name)).to_s,
90
86
  record.id.to_s
91
87
  ]
@@ -93,6 +89,63 @@ describe Mongoid::Criteria do
93
89
  end
94
90
  end
95
91
  end
92
+ context 'with logic in initial criteria' do
93
+ before :each do
94
+ 3.times do |i|
95
+ Feed::Item.create!(
96
+ name: "Feed Item #{i}",
97
+ a_string: i.to_s,
98
+ a_integer: i,
99
+ a_datetime: DateTime.new(2015, i + 1, 21, 1, 42, 3, 'UTC'),
100
+ a_date: Date.new(2016, i + 1, 21),
101
+ a_time: Time.new(2015, i + 1, 22, 1, 2, 3)
102
+ )
103
+ end
104
+ Feed::Item.create!(
105
+ name: 'Feed Item 3',
106
+ a_string: '3',
107
+ a_integer: 3,
108
+ a_datetime: DateTime.new(2015, 3, 2, 1, 2, 3),
109
+ a_date: Date.new(2012, 2, 3),
110
+ a_time: Time.new(2014, 2, 2, 1, 2, 3)
111
+ )
112
+ end
113
+ it 'respects original criteria with OR logic' do
114
+ criteria = Feed::Item.where(
115
+ '$or' => [{ :a_time.gt => Time.new(2015, 7, 22, 1, 2, 3) }, { :a_time.lte => Time.new(2015, 7, 22, 1, 2, 3), :a_date.gte => Date.new(2015, 7, 30) }]
116
+ ).asc(:a_time)
117
+ records = []
118
+ cursor = nil
119
+ criteria.limit(2).scroll do |record, next_cursor|
120
+ records << record
121
+ cursor = next_cursor
122
+ end
123
+ expect(records.size).to eq 2
124
+ expect(records.map(&:name)).to eq ['Feed Item 0', 'Feed Item 1']
125
+ records = []
126
+ criteria.limit(2).scroll(cursor) do |record, next_cursor|
127
+ records << record
128
+ cursor = next_cursor
129
+ end
130
+ expect(records.size).to eq 1
131
+ expect(records.map(&:name)).to eq ['Feed Item 2']
132
+ end
133
+ end
134
+ context 'with embeddable objects' do
135
+ before do
136
+ @item = Feed::Item.create! a_integer: 1, name: 'item'
137
+ @embedded_item = Feed::EmbeddedItem.create! name: 'embedded', item: @item
138
+ end
139
+ it 'respects embedded queries' do
140
+ records = []
141
+ criteria = @item.embedded_items.limit(2)
142
+ criteria.scroll do |record, _next_cursor|
143
+ records << record
144
+ end
145
+ expect(records.size).to eq 1
146
+ expect(records.map(&:name)).to eq ['embedded']
147
+ end
148
+ end
96
149
  context 'with overlapping data' do
97
150
  before :each do
98
151
  3.times { Feed::Item.create! a_integer: 5 }
@@ -102,7 +155,7 @@ describe Mongoid::Criteria do
102
155
  # natural order isn't necessarily going to be the same as _id order
103
156
  # if a document is updated and grows in size, it may need to be relocated and
104
157
  # thus cause the natural order to change
105
- Feed::Item.order_by('$natural' => 1).to_a.should_not eq Feed::Item.order_by(_id: 1).to_a
158
+ expect(Feed::Item.order_by('$natural' => 1).to_a).to_not eq(Feed::Item.order_by(_id: 1).to_a)
106
159
  end
107
160
  [{ a_integer: 1 }, { a_integer: -1 }].each do |sort_order|
108
161
  it "scrolls by #{sort_order}" do
@@ -112,12 +165,12 @@ describe Mongoid::Criteria do
112
165
  records << record
113
166
  cursor = next_cursor
114
167
  end
115
- records.size.should == 2
168
+ expect(records.size).to eq 2
116
169
  Feed::Item.order_by(sort_order).scroll(cursor) do |record, _next_cursor|
117
170
  records << record
118
171
  end
119
- records.size.should == 3
120
- records.should eq Feed::Item.all.sort(_id: sort_order[:a_integer]).to_a
172
+ expect(records.size).to eq 3
173
+ expect(records).to eq Feed::Item.all.sort(_id: sort_order[:a_integer]).to_a
121
174
  end
122
175
  end
123
176
  end
@@ -22,12 +22,12 @@ describe Mongoid::Scroll::Cursor do
22
22
  end
23
23
  its(:value) { should eq feed_item.a_string }
24
24
  its(:tiebreak_id) { should eq feed_item.id }
25
- its(:criteria) {
25
+ its(:criteria) do
26
26
  should eq('$or' => [
27
27
  { 'a_string' => { '$gt' => feed_item.a_string } },
28
28
  { 'a_string' => feed_item.a_string, :_id => { '$gt' => feed_item.id } }
29
29
  ])
30
- }
30
+ end
31
31
  end
32
32
  context 'an integer field cursor' do
33
33
  let(:feed_item) { Feed::Item.create!(a_integer: 10) }
@@ -36,27 +36,27 @@ describe Mongoid::Scroll::Cursor do
36
36
  end
37
37
  its(:value) { should eq feed_item.a_integer }
38
38
  its(:tiebreak_id) { should eq feed_item.id }
39
- its(:criteria) {
39
+ its(:criteria) do
40
40
  should eq('$or' => [
41
41
  { 'a_integer' => { '$gt' => feed_item.a_integer } },
42
42
  { 'a_integer' => feed_item.a_integer, :_id => { '$gt' => feed_item.id } }
43
43
  ])
44
- }
44
+ end
45
45
  end
46
46
  context 'a date/time field cursor' do
47
- let(:feed_item) { Feed::Item.create!(a_datetime: DateTime.new(2013, 12, 21, 1, 42, 3)) }
47
+ let(:feed_item) { Feed::Item.create!(a_datetime: DateTime.new(2013, 12, 21, 1, 42, 3, 'UTC')) }
48
48
  subject do
49
- Mongoid::Scroll::Cursor.new "#{feed_item.a_datetime.to_i}:#{feed_item.id}", field_name: 'a_datetime', field_type: DateTime
49
+ Mongoid::Scroll::Cursor.new "#{feed_item.a_datetime.utc.to_i}:#{feed_item.id}", field_name: 'a_datetime', field_type: DateTime
50
50
  end
51
51
  its(:value) { should eq feed_item.a_datetime }
52
52
  its(:tiebreak_id) { should eq feed_item.id }
53
- its(:to_s) { should eq "#{feed_item.a_datetime.to_i}:#{feed_item.id}" }
54
- its(:criteria) {
53
+ its(:to_s) { should eq "#{feed_item.a_datetime.utc.to_i}:#{feed_item.id}" }
54
+ its(:criteria) do
55
55
  should eq('$or' => [
56
56
  { 'a_datetime' => { '$gt' => feed_item.a_datetime } },
57
57
  { 'a_datetime' => feed_item.a_datetime, :_id => { '$gt' => feed_item.id } }
58
58
  ])
59
- }
59
+ end
60
60
  end
61
61
  context 'a date field cursor' do
62
62
  let(:feed_item) { Feed::Item.create!(a_date: Date.new(2013, 12, 21)) }
@@ -66,12 +66,12 @@ describe Mongoid::Scroll::Cursor do
66
66
  its(:value) { should eq feed_item.a_date }
67
67
  its(:tiebreak_id) { should eq feed_item.id }
68
68
  its(:to_s) { should eq "#{feed_item.a_date.to_datetime.to_i}:#{feed_item.id}" }
69
- its(:criteria) {
69
+ its(:criteria) do
70
70
  should eq('$or' => [
71
71
  { 'a_date' => { '$gt' => feed_item.a_date.to_datetime } },
72
72
  { 'a_date' => feed_item.a_date.to_datetime, :_id => { '$gt' => feed_item.id } }
73
73
  ])
74
- }
74
+ end
75
75
  end
76
76
  context 'a time field cursor' do
77
77
  let(:feed_item) { Feed::Item.create!(a_time: Time.new(2013, 12, 21, 1, 2, 3)) }
@@ -81,12 +81,12 @@ describe Mongoid::Scroll::Cursor do
81
81
  its(:value) { should eq feed_item.a_time }
82
82
  its(:tiebreak_id) { should eq feed_item.id }
83
83
  its(:to_s) { should eq "#{feed_item.a_time.to_i}:#{feed_item.id}" }
84
- its(:criteria) {
84
+ its(:criteria) do
85
85
  should eq('$or' => [
86
86
  { 'a_time' => { '$gt' => feed_item.a_time } },
87
87
  { 'a_time' => feed_item.a_time, :_id => { '$gt' => feed_item.id } }
88
88
  ])
89
- }
89
+ end
90
90
  end
91
91
  context 'a time field cursor with a field option' do
92
92
  let(:feed_item) { Feed::Item.create!(a_time: Time.new(2013, 12, 21, 1, 2, 3)) }
@@ -96,26 +96,26 @@ describe Mongoid::Scroll::Cursor do
96
96
  its(:value) { should eq feed_item.a_time }
97
97
  its(:tiebreak_id) { should eq feed_item.id }
98
98
  its(:to_s) { should eq "#{feed_item.a_time.to_i}:#{feed_item.id}" }
99
- its(:criteria) {
99
+ its(:criteria) do
100
100
  should eq('$or' => [
101
101
  { 'a_time' => { '$gt' => feed_item.a_time } },
102
102
  { 'a_time' => feed_item.a_time, :_id => { '$gt' => feed_item.id } }
103
103
  ])
104
- }
104
+ end
105
105
  end
106
106
  context 'an array field cursor' do
107
- let(:feed_item) { Feed::Item.create!(a_array: ['x', 'y']) }
107
+ let(:feed_item) { Feed::Item.create!(a_array: %w(x y)) }
108
108
  it 'is not supported' do
109
- expect {
109
+ expect do
110
110
  Mongoid::Scroll::Cursor.from_record feed_item, field_name: 'a_array', field_type: Array
111
- }.to raise_error Mongoid::Scroll::Errors::UnsupportedFieldTypeError, /The type of the field 'a_array' is not supported: Array./
111
+ end.to raise_error Mongoid::Scroll::Errors::UnsupportedFieldTypeError, /The type of the field 'a_array' is not supported: Array./
112
112
  end
113
113
  end
114
114
  context 'an invalid field cursor' do
115
115
  it 'raises ArgumentError' do
116
- expect {
116
+ expect do
117
117
  Mongoid::Scroll::Cursor.new 'invalid:whatever', {}
118
- }.to raise_error ArgumentError
118
+ end.to raise_error ArgumentError
119
119
  end
120
120
  end
121
121
  end
@@ -6,7 +6,7 @@ describe Moped::Query do
6
6
  Mongoid.default_session['feed_items'].find
7
7
  end
8
8
  it ':scroll' do
9
- subject.should.respond_to? :scroll
9
+ expect(subject).to respond_to(:scroll)
10
10
  end
11
11
  end
12
12
  context 'with multiple sort fields' do
@@ -23,7 +23,7 @@ describe Moped::Query do
23
23
  Mongoid.default_session['feed_items'].find
24
24
  end
25
25
  it 'adds a default sort by _id' do
26
- subject.scroll.operation.selector['$orderby'].should == { _id: 1 }
26
+ expect(subject.scroll.operation.selector['$orderby']).to eq(_id: 1)
27
27
  end
28
28
  end
29
29
  context 'with data' do
@@ -32,7 +32,7 @@ describe Moped::Query do
32
32
  Mongoid.default_session['feed_items'].insert(
33
33
  a_string: i.to_s,
34
34
  a_integer: i,
35
- a_datetime: DateTime.mongoize(DateTime.new(2013, i + 1, 21, 1, 42, 3)),
35
+ a_datetime: DateTime.mongoize(DateTime.new(2013, i + 1, 21, 1, 42, 3, 'UTC')),
36
36
  a_date: Date.mongoize(Date.new(2013, i + 1, 21)),
37
37
  a_time: Time.mongoize(Time.at(Time.now.to_i + i))
38
38
  )
@@ -44,50 +44,48 @@ describe Moped::Query do
44
44
  Mongoid.default_session['feed_items'].find.scroll do |record, _next_cursor|
45
45
  records << record
46
46
  end
47
- records.size.should == 10
48
- records.should eq Mongoid.default_session['feed_items'].find.to_a
47
+ expect(records.size).to eq 10
48
+ expect(records).to eq Mongoid.default_session['feed_items'].find.to_a
49
49
  end
50
50
  end
51
51
  { a_string: String, a_integer: Integer, a_date: Date, a_datetime: DateTime }.each_pair do |field_name, field_type|
52
52
  context field_type do
53
53
  it 'scrolls all with a block' do
54
54
  records = []
55
- Mongoid.default_session['feed_items'].find.sort(field_name => 1).scroll(nil, field_type: field_type) do |record, _next_cursor|
55
+ Mongoid.default_session['feed_items'].find.sort(field_name => 1).scroll(nil, field_type: field_type) do |record, _next_cursor|
56
56
  records << record
57
57
  end
58
- records.size.should == 10
59
- records.should eq Mongoid.default_session['feed_items'].find.to_a
58
+ expect(records.size).to eq 10
59
+ expect(records).to eq Mongoid.default_session['feed_items'].find.to_a
60
60
  end
61
61
  it 'scrolls all with a break' do
62
62
  records = []
63
63
  cursor = nil
64
- Mongoid.default_session['feed_items'].find.sort(field_name => 1).limit(5).scroll(nil, field_type: field_type) do |record, next_cursor|
64
+ Mongoid.default_session['feed_items'].find.sort(field_name => 1).limit(5).scroll(nil, field_type: field_type) do |record, next_cursor|
65
65
  records << record
66
66
  cursor = next_cursor
67
67
  end
68
- records.size.should == 5
69
- Mongoid.default_session['feed_items'].find.sort(field_name => 1).scroll(cursor, field_type: field_type) do |record, next_cursor|
68
+ expect(records.size).to eq 5
69
+ Mongoid.default_session['feed_items'].find.sort(field_name => 1).scroll(cursor, field_type: field_type) do |record, next_cursor|
70
70
  records << record
71
71
  cursor = next_cursor
72
72
  end
73
- records.size.should == 10
74
- records.should eq Mongoid.default_session['feed_items'].find.to_a
73
+ expect(records.size).to eq 10
74
+ expect(records).to eq Mongoid.default_session['feed_items'].find.to_a
75
75
  end
76
76
  it 'scrolls in descending order' do
77
77
  records = []
78
- Mongoid.default_session['feed_items'].find.sort(field_name => -1).limit(3).scroll(nil, field_type: field_type, field_name: field_name) do |record, _next_cursor|
78
+ Mongoid.default_session['feed_items'].find.sort(field_name => -1).limit(3).scroll(nil, field_type: field_type, field_name: field_name) do |record, _next_cursor|
79
79
  records << record
80
80
  end
81
- records.size.should == 3
82
- records.should eq Mongoid.default_session['feed_items'].find.sort(field_name => -1).limit(3).to_a
81
+ expect(records.size).to eq 3
82
+ expect(records).to eq Mongoid.default_session['feed_items'].find.sort(field_name => -1).limit(3).to_a
83
83
  end
84
84
  it 'map' do
85
- record = Mongoid.default_session['feed_items'].find.limit(3).scroll(nil, field_type: field_type, field_name: field_name).map {
86
- |r, _| r
87
- }.last
88
- cursor = Mongoid::Scroll::Cursor.from_record(record, field_type: field_type, field_name: field_name)
89
- cursor.should_not be_nil
90
- cursor.to_s.split(':').should == [
85
+ record = Mongoid.default_session['feed_items'].find.limit(3).scroll(nil, field_type: field_type, field_name: field_name).map { |r| r }.last
86
+ cursor = Mongoid::Scroll::Cursor.from_record(record, field_type: field_type, field_name: field_name)
87
+ expect(cursor).to_not be nil
88
+ expect(cursor.to_s.split(':')).to eq [
91
89
  Mongoid::Scroll::Cursor.transform_field_value(field_type, field_name, record[field_name.to_s]).to_s,
92
90
  record['_id'].to_s
93
91
  ]
@@ -104,7 +102,7 @@ describe Moped::Query do
104
102
  # natural order isn't necessarily going to be the same as _id order
105
103
  # if a document is updated and grows in size, it may need to be relocated and
106
104
  # thus cause the natural order to change
107
- Feed::Item.order_by('$natural' => 1).to_a.should_not eq Feed::Item.order_by(_id: 1).to_a
105
+ expect(Feed::Item.order_by('$natural' => 1).to_a).to_not eq Feed::Item.order_by(_id: 1).to_a
108
106
  end
109
107
  [{ a_integer: 1 }, { a_integer: -1 }].each do |sort_order|
110
108
  it "scrolls by #{sort_order}" do
@@ -114,13 +112,13 @@ describe Moped::Query do
114
112
  records << record
115
113
  cursor = next_cursor
116
114
  end
117
- records.size.should == 2
115
+ expect(records.size).to eq 2
118
116
  Mongoid.default_session['feed_items'].find.sort(sort_order).scroll(cursor) do |record, _next_cursor|
119
117
  records << record
120
118
  end
121
- records.size.should == 3
122
- records.should eq Mongoid.default_session['feed_items'].find.sort(_id: sort_order[:a_integer]).to_a
119
+ expect(records.size).to eq 3
120
+ expect(records).to eq Mongoid.default_session['feed_items'].find.sort(_id: sort_order[:a_integer]).to_a
123
121
  end
124
122
  end
125
123
  end
126
- end
124
+ end if Object.const_defined?(:Moped)
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
 
4
4
  require 'rubygems'
5
5
  require 'rspec'
6
+ require 'rspec/its'
6
7
  require 'mongoid-scroll'
7
8
 
8
9
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |f|
@@ -0,0 +1,9 @@
1
+ module Feed
2
+ class EmbeddedItem
3
+ include Mongoid::Document
4
+
5
+ field :name, type: String
6
+
7
+ embedded_in :item, inverse_of: :embedded_items
8
+ end
9
+ end
@@ -9,5 +9,7 @@ module Feed
9
9
  field :a_date, type: Date
10
10
  field :a_time, type: Time
11
11
  field :a_array, type: Array
12
+
13
+ embeds_many :embedded_items
12
14
  end
13
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-scroll
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-02 00:00:00.000000000 Z
12
+ date: 2015-07-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -48,6 +48,7 @@ files:
48
48
  - .gitignore
49
49
  - .rspec
50
50
  - .rubocop.yml
51
+ - .rubocop_todo.yml
51
52
  - .travis.yml
52
53
  - CHANGELOG.md
53
54
  - Gemfile
@@ -76,6 +77,7 @@ files:
76
77
  - spec/mongoid/scroll_spec.rb
77
78
  - spec/moped/query_spec.rb
78
79
  - spec/spec_helper.rb
80
+ - spec/support/feed/embedded_item.rb
79
81
  - spec/support/feed/item.rb
80
82
  homepage: http://github.com/dblock/mongoid-scroll
81
83
  licenses:
@@ -97,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
99
  version: 1.3.6
98
100
  requirements: []
99
101
  rubyforge_project:
100
- rubygems_version: 2.1.11
102
+ rubygems_version: 2.2.2
101
103
  signing_key:
102
104
  specification_version: 4
103
105
  summary: Mongoid extensions to enable infinite scroll.