mordor 0.2.9 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -161,20 +161,16 @@ module Mordor
161
161
  Collection.new(self, perform_collection_find(query, options))
162
162
  end
163
163
 
164
- def find_by_day(day, options = {})
165
- case day
166
- when DateTime
167
- start = day.to_date.to_time
168
- end_of_day = (day.to_date + 1).to_time
169
- when Date
170
- start = day.to_time
171
- end_of_day = (day + 1).to_time
172
- when Time
173
- start = day.to_datetime.to_date.to_time
174
- end_of_day = (day.to_date + 1).to_datetime.to_date.to_time
164
+ def find_by_day(value, options = {})
165
+ if value.is_a?(Hash)
166
+ raise ArgumentError.new(":value missing from complex query hash") unless value.keys.include?(:value)
167
+ day = value.delete(:value)
168
+ query = value.merge(day_to_query(day))
169
+ else
170
+ query = day_to_query(value)
175
171
  end
176
- hash = {:at => {'$gte' => start, '$lt' => end_of_day}}
177
- cursor = perform_collection_find({:at => {'$gte' => start, '$lt' => end_of_day}}, options)
172
+
173
+ cursor = perform_collection_find(query, options)
178
174
  Collection.new(self, cursor)
179
175
  end
180
176
 
@@ -246,6 +242,29 @@ module Mordor
246
242
  def index_types
247
243
  @index_types ||= {}
248
244
  end
245
+
246
+ def day_to_range(day)
247
+ case day
248
+ when DateTime
249
+ start = day.to_date.to_time
250
+ end_of_day = (day.to_date + 1).to_time
251
+ when Date
252
+ start = day.to_time
253
+ end_of_day = (day + 1).to_time
254
+ when Time
255
+ start = day.to_datetime.to_date.to_time
256
+ end_of_day = (day.to_datetime + 1).to_date.to_time
257
+ end
258
+ [start, end_of_day]
259
+ end
260
+
261
+ def date_range_to_query(range)
262
+ {:at => {:$gte => range.first, :$lt => range.last}}
263
+ end
264
+
265
+ def day_to_query(day)
266
+ date_range_to_query( day_to_range(day) )
267
+ end
249
268
  end
250
269
  end
251
270
  end
data/mordor.gemspec CHANGED
@@ -2,8 +2,8 @@ Gem::Specification.new do |s|
2
2
  s.name = "mordor"
3
3
 
4
4
  # Do not set the version and date field manually, this is done by the release script
5
- s.version = "0.2.9"
6
- s.date = "2012-01-04"
5
+ s.version = "0.2.10"
6
+ s.date = "2012-01-19"
7
7
 
8
8
  s.summary = "mordor"
9
9
  s.description = <<-eos
@@ -9,7 +9,8 @@ describe "with respect to resources" do
9
9
  attribute :first, :index => true
10
10
  attribute :second, :index => true, :index_type => Mongo::ASCENDING
11
11
  attribute :third, :finder_method => :find_by_third_attribute
12
- attribute :at, :timestamp => true
12
+ attribute :at
13
+ attribute :created_at, :timestamp => true
13
14
 
14
15
  # Put this in here again to ensure the original method is still here
15
16
  class_eval do
@@ -50,7 +51,7 @@ describe "with respect to resources" do
50
51
 
51
52
  it "should be possible to designate an attribute as a timestamp" do
52
53
  TestResource.timestamped_attribute.should_not be_nil
53
- TestResource.timestamped_attribute.should == :at
54
+ TestResource.timestamped_attribute.should == :created_at
54
55
  end
55
56
 
56
57
  it "should only be possible to have one attribute as a timestamp" do
@@ -103,7 +104,7 @@ describe "with respect to resources" do
103
104
  end
104
105
  end
105
106
 
106
- it "should correctly replace BSON::Timestampls" do
107
+ it "should correctly replace BSON::Timestamps" do
107
108
  options = {
108
109
  "option" => BSON::Timestamp.new(324244, 12)
109
110
  }
@@ -117,12 +118,82 @@ describe "with respect to resources" do
117
118
  it "should correctly respond to to_hash" do
118
119
  resource = TestResource.new({:first => "first", :second => "second", :third => "third"})
119
120
  hash = resource.to_hash
120
- hash.size.should == 4
121
+ hash.size.should == 5
121
122
  hash[:first].should == "first"
122
123
  hash[:second].should == "second"
123
124
  hash[:third].should == "third"
125
+ hash[:at].should == ""
124
126
  end
127
+ end
128
+
129
+ context "with respect to times and ranges" do
130
+ context "when DateTime days are given" do
131
+ it "should return a correct range" do
132
+ day = DateTime.civil(2012, 1, 19, 10, 0)
133
+ range = TestResource.send(:day_to_range, day)
134
+ range.first.should == DateTime.civil(2012, 1, 19).to_time.gmtime
135
+ range.last.should == DateTime.civil(2012, 1, 20).to_time.gmtime
136
+ end
137
+
138
+ it "should return an Array of 2 Time objects" do
139
+ day = DateTime.civil(2012, 1, 19, 10, 0)
140
+ range = TestResource.send(:day_to_range, day)
141
+ range.first.should be_a Time
142
+ range.last.should be_a Time
143
+ end
144
+ end
145
+
146
+ context "when Date days are given" do
147
+ it "should return a correct range" do
148
+ day = Date.parse("2012-1-19")
149
+ range = TestResource.send(:day_to_range, day)
150
+ range.first.should == Date.parse("2012-1-19").to_time.gmtime
151
+ range.last.should == Date.parse("2012-1-20").to_time.gmtime
152
+ end
153
+
154
+ it "should return an Array of 2 Time objects" do
155
+ day = Date.parse("2012-1-19")
156
+ range = TestResource.send(:day_to_range, day)
157
+ range.first.should be_a Time
158
+ range.last.should be_a Time
159
+ end
160
+ end
161
+
162
+ context "when Time days are given" do
163
+ it "should return a correct range" do
164
+ day = DateTime.civil(2012, 1, 19, 10, 0).to_time
165
+ range = TestResource.send(:day_to_range, day)
166
+ range.first.should == DateTime.civil(2012, 1, 19).to_time.gmtime
167
+ range.last.should == DateTime.civil(2012, 1, 20).to_time.gmtime
168
+ end
169
+
170
+ it "should return an Array of 2 Time objects" do
171
+ day = DateTime.civil(2012, 1, 19, 10, 0).to_time
172
+ range = TestResource.send(:day_to_range, day)
173
+ range.first.should be_a Time
174
+ range.last.should be_a Time
175
+ end
176
+ end
177
+
178
+ context "when ranges are changed to queries" do
179
+ before :all do
180
+ @range = TestResource.send(:day_to_range, DateTime.civil(2012, 1, 19))
181
+ @query = TestResource.send(:date_range_to_query, @range)
182
+ end
183
+
184
+ it "should scope the query to the 'at' attribute" do
185
+ @query.size.should == 1
186
+ @query[:at].should be_a Hash
187
+ end
125
188
 
189
+ it "should use the first of the range for the greater equal part" do
190
+ @query[:at][:$gte].should == @range.first
191
+ end
192
+
193
+ it "should use the last of the range for the smaller than part" do
194
+ @query[:at][:$lt].should == @range.last
195
+ end
196
+ end
126
197
  end
127
198
 
128
199
  context "with respect to indices" do
@@ -309,7 +380,7 @@ describe "with respect to resources" do
309
380
  describe "with respect to passing extra query parameters to finder methods" do
310
381
  before :each do
311
382
  5.times do |i|
312
- TestResource.create({:first => "first", :second => "second-#{i}", :third => "third-#{i}"})
383
+ TestResource.create({:first => "first", :second => "second-#{i}", :third => "third-#{i}", :at => (Date.today).to_time})
313
384
  end
314
385
  end
315
386
 
@@ -320,6 +391,30 @@ describe "with respect to resources" do
320
391
  lambda{ TestResource.find_by_first({:second => "second-2"})}.should raise_error
321
392
  end
322
393
 
394
+ it "should be possible to add extra query clauses to the find_by_day method" do
395
+ collection = TestResource.find_by_day(Date.today)
396
+ collection.size.should == 5
397
+
398
+ collection = TestResource.find_by_day({:value => Date.today, :second => "second-1"})
399
+ collection.size.should == 1
400
+ resource = collection.first
401
+ resource.first.should == "first"
402
+ resource.at.should == Date.today.to_time
403
+ end
404
+
405
+ it "should be possible to add more complex query clauses to the find_by_day method" do
406
+ collection = TestResource.find_by_day(Date.today)
407
+ collection.size.should == 5
408
+
409
+ collection = TestResource.find_by_day({:value => Date.today, :second => {:$in => ["second-1", "second-2"]}})
410
+ collection.size.should == 2
411
+ collection.each do |res|
412
+ res.at.should == Date.today.to_time
413
+ ["second-1", "second-2"].should include res.second
414
+ end
415
+
416
+ end
417
+
323
418
  it "should be possible to add extra query clauses to a finder method" do
324
419
  collection = TestResource.find_by_first("first")
325
420
  collection.size.should == 5
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mordor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 9
10
- version: 0.2.9
9
+ - 10
10
+ version: 0.2.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jan-Willem Koelewijn
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-01-04 00:00:00 +01:00
19
+ date: 2012-01-19 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency