mongoid-metastamp 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/Gemfile +2 -0
- data/README.md +31 -0
- data/Rakefile +9 -0
- data/lib/mongoid-metastamp.rb +1 -0
- data/lib/mongoid/core_ext.rb +26 -0
- data/lib/mongoid/metastamp.rb +11 -0
- data/lib/mongoid/metastamp/time.rb +48 -0
- data/lib/mongoid/metastamp/version.rb +5 -0
- data/mongoid-metastamp.gemspec +26 -0
- data/spec/metastamp_spec.rb +57 -0
- data/spec/models/event.rb +4 -0
- data/spec/spec_helper.rb +25 -0
- metadata +115 -0
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
Mongoid Metastamp
|
2
|
+
=========================
|
3
|
+
|
4
|
+
This gem provides Mongoid with enhanced meta-timestamps which allow querying by day, month, year, min, sec, or even by local vs. universal timezone.
|
5
|
+
|
6
|
+
What It Does
|
7
|
+
=========================
|
8
|
+
(Or why would I want to use this?)
|
9
|
+
|
10
|
+
Storing simple timestamps is all well and good if you only need to search inside a continuous range relative to one timezone.
|
11
|
+
Sometimes however, you might want to search for all events that occurred between 9 AM and 5 PM, but across multiple locations and timezones.
|
12
|
+
|
13
|
+
So you could query all possible events between some range and then run a conversion on each result to narrow it down, but that makes servers die and developers cry.
|
14
|
+
With mongoid-metastamp you get a custom timestamp field that performs conversions when you save, and then stores the metadata in a MongoDB friendly way for easy querying.
|
15
|
+
|
16
|
+
Storage
|
17
|
+
=========================
|
18
|
+
(What metadata should we store?)
|
19
|
+
|
20
|
+
* in_zone (Date)
|
21
|
+
* normalized (Date)
|
22
|
+
* year (Int)
|
23
|
+
* month (Int)
|
24
|
+
* day (Int)
|
25
|
+
* hour (Int)
|
26
|
+
* min (Int)
|
27
|
+
* sec (Int)
|
28
|
+
* time_zone (String)
|
29
|
+
|
30
|
+
|
31
|
+
(More to come)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "mongoid/metastamp"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mongoid #:nodoc
|
4
|
+
module Fields #:nodoc:
|
5
|
+
module Mappings #:nodoc:
|
6
|
+
|
7
|
+
def for(klass, foreign_key = false)
|
8
|
+
return Serializable::Object unless klass
|
9
|
+
if foreign_key
|
10
|
+
return "#{MODULE}::ForeignKeys::#{klass.to_s.demodulize}".constantize
|
11
|
+
end
|
12
|
+
begin
|
13
|
+
modules = "#{MODULE}::|BSON::|ActiveSupport::"
|
14
|
+
if match = klass.to_s.match(Regexp.new("^(#{ modules })?(\\w+)$"))
|
15
|
+
"#{MODULE}::#{match[2]}".constantize
|
16
|
+
else
|
17
|
+
klass.to_s.constantize
|
18
|
+
end
|
19
|
+
rescue NameError
|
20
|
+
klass
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mongoid #:nodoc:
|
4
|
+
module Metastamp
|
5
|
+
class Time
|
6
|
+
include Mongoid::Fields::Serializable
|
7
|
+
include Mongoid::Fields::Serializable::Timekeeping
|
8
|
+
|
9
|
+
def deserialize(object)
|
10
|
+
return nil if object.blank?
|
11
|
+
super(object[:in_zone])
|
12
|
+
end
|
13
|
+
|
14
|
+
def serialize(object)
|
15
|
+
time = super(object)
|
16
|
+
normalized_time = normalize_time(object)
|
17
|
+
{
|
18
|
+
in_zone: time,
|
19
|
+
normalized: normalized_time,
|
20
|
+
year: normalized_time.year,
|
21
|
+
month: normalized_time.month,
|
22
|
+
day: normalized_time.day,
|
23
|
+
hour: normalized_time.hour,
|
24
|
+
min: normalized_time.min,
|
25
|
+
sec: normalized_time.sec
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def normalize_time(object)
|
32
|
+
case object
|
33
|
+
when ::String
|
34
|
+
time = ::Time.parse(object)
|
35
|
+
when ::DateTime
|
36
|
+
time = ::Time.new(object.year, object.month, object.day, object.hour, object.min, object.sec)
|
37
|
+
when ::Date
|
38
|
+
time = ::Time.new(object.year, object.month, object.day)
|
39
|
+
when ::Array
|
40
|
+
time = ::Time.new(*object)
|
41
|
+
else
|
42
|
+
time = object
|
43
|
+
end
|
44
|
+
::Time.parse(time.iso8601.sub(/-(\d\d:\d\d)$/, "-00:00"))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "mongoid/metastamp/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mongoid-metastamp"
|
7
|
+
s.version = Mongoid::Metastamp::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Peter Gumeson"]
|
10
|
+
s.email = ["gumeson@gmail.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/mongoid-metastamp"
|
12
|
+
s.summary = %q{ Store and query more useful information about your Mongoid timestamps }
|
13
|
+
s.description = %q{ Provides enhanced meta-timestamps for mongoid that allow querying by day, month, year, min, sec, or by local vs. universal timezone. }
|
14
|
+
|
15
|
+
s.rubyforge_project = "mongoid-metastamp"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency("mongoid", "~> 2.1")
|
23
|
+
s.add_development_dependency("bson_ext", "~> 1.3")
|
24
|
+
s.add_development_dependency("rake", "~> 0.9")
|
25
|
+
s.add_development_dependency("rspec", "~> 2.6")
|
26
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Mongoid::Metastamp do
|
6
|
+
|
7
|
+
let :ten_am_utc do
|
8
|
+
"2011-10-05T10:00:00-00:00"
|
9
|
+
end
|
10
|
+
|
11
|
+
let :ten_am_eastern do
|
12
|
+
"2011-10-05T10:00:00-04:00"
|
13
|
+
end
|
14
|
+
|
15
|
+
let :ten_am_pacific do
|
16
|
+
"2011-10-05T10:00:00-07:00"
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "with custom field of type Mongoid::Metastamp::Time" do
|
20
|
+
|
21
|
+
context "named timestamp" do
|
22
|
+
|
23
|
+
context "when set to 10:00 AM PST" do
|
24
|
+
|
25
|
+
let :pst_event do
|
26
|
+
Event.create(timestamp: ten_am_pacific)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have a timestamp field" do
|
30
|
+
pst_event.should respond_to(:timestamp)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should deserialize the timestamp as 10 AM PST" do
|
34
|
+
pst_event.timestamp.should == Time.parse(ten_am_pacific)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be searchable by timestamp.in_zone" do
|
38
|
+
Event.where(
|
39
|
+
"timestamp.in_zone" => { '$gt' => Time.parse(ten_am_pacific) - 1.minute },
|
40
|
+
"timestamp.in_zone" => { '$lt' => Time.parse(ten_am_pacific) + 1.minute }
|
41
|
+
).should == [pst_event]
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should be searchable by timestamp.normalized" do
|
45
|
+
Event.where(
|
46
|
+
"timestamp.normalized" => { '$gt' => Time.parse(ten_am_utc) - 1.minute },
|
47
|
+
"timestamp.normalized" => { '$lt' => Time.parse(ten_am_utc) + 1.minute }
|
48
|
+
).should == [pst_event]
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
require "mongoid"
|
5
|
+
require "mongoid/core_ext"
|
6
|
+
|
7
|
+
require "mongoid/metastamp"
|
8
|
+
require "mongoid/metastamp/time"
|
9
|
+
|
10
|
+
require "rspec"
|
11
|
+
|
12
|
+
Mongoid.configure do |config|
|
13
|
+
config.master = Mongo::Connection.new.db("mongoid_metastamp_test")
|
14
|
+
config.use_utc = false
|
15
|
+
config.use_activesupport_time_zone = true
|
16
|
+
end
|
17
|
+
|
18
|
+
Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
|
19
|
+
|
20
|
+
RSpec.configure do |c|
|
21
|
+
c.before(:each) do
|
22
|
+
::Time.zone = "UTC"
|
23
|
+
Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:remove)
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-metastamp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter Gumeson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-10-08 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: mongoid
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "2.1"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bson_ext
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "1.3"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rake
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0.9"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "2.6"
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id004
|
60
|
+
description: " Provides enhanced meta-timestamps for mongoid that allow querying by day, month, year, min, sec, or by local vs. universal timezone. "
|
61
|
+
email:
|
62
|
+
- gumeson@gmail.com
|
63
|
+
executables: []
|
64
|
+
|
65
|
+
extensions: []
|
66
|
+
|
67
|
+
extra_rdoc_files: []
|
68
|
+
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .rspec
|
72
|
+
- Gemfile
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/mongoid-metastamp.rb
|
76
|
+
- lib/mongoid/core_ext.rb
|
77
|
+
- lib/mongoid/metastamp.rb
|
78
|
+
- lib/mongoid/metastamp/time.rb
|
79
|
+
- lib/mongoid/metastamp/version.rb
|
80
|
+
- mongoid-metastamp.gemspec
|
81
|
+
- spec/metastamp_spec.rb
|
82
|
+
- spec/models/event.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
has_rdoc: true
|
85
|
+
homepage: http://rubygems.org/gems/mongoid-metastamp
|
86
|
+
licenses: []
|
87
|
+
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: "0"
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: "0"
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project: mongoid-metastamp
|
108
|
+
rubygems_version: 1.6.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: Store and query more useful information about your Mongoid timestamps
|
112
|
+
test_files:
|
113
|
+
- spec/metastamp_spec.rb
|
114
|
+
- spec/models/event.rb
|
115
|
+
- spec/spec_helper.rb
|