mongoid_time_field 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -11
- data/Gemfile.lock +40 -44
- data/LICENSE.txt +3 -1
- data/README.rdoc +6 -10
- data/Rakefile +1 -49
- data/lib/mongoid_time_field.rb +5 -52
- data/lib/mongoid_time_field/class_methods.rb +24 -0
- data/lib/mongoid_time_field/time_field.rb +68 -0
- data/lib/mongoid_time_field/value.rb +77 -0
- data/lib/mongoid_time_field/version.rb +3 -0
- data/mongoid_time_field.gemspec +23 -66
- data/spec/spec_helper.rb +4 -4
- data/spec/support/dummy_duration.rb +7 -0
- data/spec/support/dummy_time.rb +2 -3
- data/spec/support/dummy_v2.rb +12 -0
- data/spec/support/mongoid.yml +8 -0
- data/spec/{time_spec.rb → v1_spec.rb} +15 -13
- data/spec/v2_spec.rb +175 -0
- metadata +80 -64
- data/.document +0 -5
- data/.rvmrc +0 -1
- data/VERSION +0 -1
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 675c1a0fb1ddddd399518a3984b3575f5203186e
|
4
|
+
data.tar.gz: 254095769b5f2c6cb8b67889d2e9e7aea3211c05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54b63521baaccff3b95eb6d7075d6ae58e0e342ea95c87d710a380a439c8adfa7af5983e1ce7fd15279bbb9692afc1a6b0fa0b2eef05f001984251541d0c4182
|
7
|
+
data.tar.gz: 93944c906019a1a619b3cc77fd35f592112f7b9645d0b46da9c1402d563e84c5c72da52b84eb41e865db67335f5d6277a9cf43962c018c8d12149e1bbf6205c0
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
time_field
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p195
|
data/Gemfile
CHANGED
@@ -1,12 +1,4 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gem
|
4
|
-
|
5
|
-
group :development do
|
6
|
-
gem "rspec", "~> 2.8.0"
|
7
|
-
gem "rdoc", "~> 3.12"
|
8
|
-
gem "bundler", "~> 1.0.0"
|
9
|
-
gem "jeweler", "~> 1.8.3"
|
10
|
-
gem "simplecov", '>= 0.4.0', :require => false
|
11
|
-
gem "database_cleaner"
|
12
|
-
end
|
3
|
+
# Specify your gem's dependencies in mongoid_time_field.gemspec
|
4
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,56 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mongoid_time_field (0.2.1)
|
5
|
+
mongoid (>= 3.0.0)
|
6
|
+
|
1
7
|
GEM
|
2
|
-
remote:
|
8
|
+
remote: https://rubygems.org/
|
3
9
|
specs:
|
4
|
-
activemodel (3.2.
|
5
|
-
activesupport (= 3.2.
|
10
|
+
activemodel (3.2.13)
|
11
|
+
activesupport (= 3.2.13)
|
6
12
|
builder (~> 3.0.0)
|
7
|
-
activesupport (3.2.
|
8
|
-
i18n (
|
13
|
+
activesupport (3.2.13)
|
14
|
+
i18n (= 0.6.1)
|
9
15
|
multi_json (~> 1.0)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
git (>= 1.2.5)
|
19
|
-
rake
|
20
|
-
rdoc
|
21
|
-
json (1.6.5)
|
22
|
-
mongo (1.6.1)
|
23
|
-
bson (~> 1.6.1)
|
24
|
-
mongoid (2.4.6)
|
25
|
-
activemodel (~> 3.1)
|
26
|
-
mongo (~> 1.3)
|
16
|
+
builder (3.0.4)
|
17
|
+
database_cleaner (1.0.1)
|
18
|
+
diff-lcs (1.2.4)
|
19
|
+
i18n (0.6.1)
|
20
|
+
mongoid (3.1.4)
|
21
|
+
activemodel (~> 3.2)
|
22
|
+
moped (~> 1.4)
|
23
|
+
origin (~> 1.0)
|
27
24
|
tzinfo (~> 0.3.22)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
rspec (2.
|
33
|
-
rspec-core (~> 2.
|
34
|
-
rspec-expectations (~> 2.
|
35
|
-
rspec-mocks (~> 2.
|
36
|
-
rspec-core (2.
|
37
|
-
rspec-expectations (2.
|
38
|
-
diff-lcs (
|
39
|
-
rspec-mocks (2.
|
40
|
-
simplecov (0.
|
25
|
+
moped (1.5.0)
|
26
|
+
multi_json (1.7.6)
|
27
|
+
origin (1.1.0)
|
28
|
+
rake (10.0.4)
|
29
|
+
rspec (2.13.0)
|
30
|
+
rspec-core (~> 2.13.0)
|
31
|
+
rspec-expectations (~> 2.13.0)
|
32
|
+
rspec-mocks (~> 2.13.0)
|
33
|
+
rspec-core (2.13.1)
|
34
|
+
rspec-expectations (2.13.0)
|
35
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
36
|
+
rspec-mocks (2.13.1)
|
37
|
+
simplecov (0.7.1)
|
41
38
|
multi_json (~> 1.0)
|
42
|
-
simplecov-html (~> 0.
|
43
|
-
simplecov-html (0.
|
44
|
-
tzinfo (0.3.
|
39
|
+
simplecov-html (~> 0.7.1)
|
40
|
+
simplecov-html (0.7.1)
|
41
|
+
tzinfo (0.3.37)
|
45
42
|
|
46
43
|
PLATFORMS
|
47
44
|
ruby
|
48
45
|
|
49
46
|
DEPENDENCIES
|
50
|
-
bundler
|
47
|
+
bundler
|
51
48
|
database_cleaner
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
simplecov (>= 0.4.0)
|
49
|
+
mongoid_time_field!
|
50
|
+
rake
|
51
|
+
rspec
|
52
|
+
simplecov
|
data/LICENSE.txt
CHANGED
data/README.rdoc
CHANGED
@@ -7,21 +7,18 @@ Simple gem to store minutes+seconds or hours+minutes in one integer field in mon
|
|
7
7
|
Include the gem in your Gemfile
|
8
8
|
|
9
9
|
gem 'mongoid_time_field'
|
10
|
-
|
10
|
+
|
11
11
|
== Usage
|
12
12
|
|
13
13
|
class DummyTime
|
14
14
|
include Mongoid::Document
|
15
|
-
include Mongoid::TimeField
|
16
|
-
|
17
|
-
field :description
|
18
|
-
|
19
|
-
time_field :open, :close
|
20
15
|
|
16
|
+
field :worktime, type: TimeField.new(format: 'HH:MM')
|
17
|
+
field :time_of_day, type: TimeField.new(format: 'HH:MM:SS')
|
18
|
+
field :duration, type: TimeField.new(format: 'mm:SS')
|
21
19
|
end
|
22
|
-
|
23
|
-
All time values are converted and stored in mongodb as seconds.
|
24
20
|
|
21
|
+
All time values are converted and stored in mongodb as seconds.
|
25
22
|
|
26
23
|
== Contributing to mongoid_time_field
|
27
24
|
|
@@ -35,6 +32,5 @@ All time values are converted and stored in mongodb as seconds.
|
|
35
32
|
|
36
33
|
== Copyright
|
37
34
|
|
38
|
-
Copyright (c) 2012 glebtv. See LICENSE.txt for
|
39
|
-
further details.
|
35
|
+
Copyright (c) 2012-2013 glebtv. See LICENSE.txt for further details.
|
40
36
|
|
data/Rakefile
CHANGED
@@ -1,49 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'bundler'
|
5
|
-
begin
|
6
|
-
Bundler.setup(:default, :development)
|
7
|
-
rescue Bundler::BundlerError => e
|
8
|
-
$stderr.puts e.message
|
9
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
-
exit e.status_code
|
11
|
-
end
|
12
|
-
require 'rake'
|
13
|
-
|
14
|
-
require 'jeweler'
|
15
|
-
Jeweler::Tasks.new do |gem|
|
16
|
-
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
-
gem.name = "mongoid_time_field"
|
18
|
-
gem.homepage = "http://github.com/glebtv/mongoid_time_field"
|
19
|
-
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{hh:mm or mm:ss time field for mongoid}
|
21
|
-
gem.description = %Q{hh:mm or mm:ss time field for mongoid}
|
22
|
-
gem.email = "glebtv@gmail.com"
|
23
|
-
gem.authors = ["glebtv"]
|
24
|
-
# dependencies defined in Gemfile
|
25
|
-
end
|
26
|
-
Jeweler::RubygemsDotOrgTasks.new
|
27
|
-
|
28
|
-
require 'rspec/core'
|
29
|
-
require 'rspec/core/rake_task'
|
30
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
-
end
|
33
|
-
|
34
|
-
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
-
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
-
spec.rcov = true
|
37
|
-
end
|
38
|
-
|
39
|
-
task :default => :spec
|
40
|
-
|
41
|
-
require 'rdoc/task'
|
42
|
-
Rake::RDocTask.new do |rdoc|
|
43
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
-
|
45
|
-
rdoc.rdoc_dir = 'rdoc'
|
46
|
-
rdoc.title = "mongoid_time_field #{version}"
|
47
|
-
rdoc.rdoc_files.include('README*')
|
48
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
-
end
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mongoid_time_field.rb
CHANGED
@@ -1,57 +1,10 @@
|
|
1
|
+
require "mongoid_time_field/version"
|
2
|
+
require "mongoid_time_field/time_field"
|
3
|
+
|
1
4
|
module Mongoid
|
2
5
|
module TimeField
|
3
6
|
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
|
6
|
-
[columns].flatten.each do |name|
|
7
|
-
attr = name.to_sym
|
8
|
-
name_minutes = (name.to_s + '_minutes').to_sym
|
9
|
-
|
10
|
-
field attr, type: Integer
|
11
|
-
|
12
|
-
define_method(name) do
|
13
|
-
minutes = read_attribute(attr)
|
14
|
-
if minutes.nil?
|
15
|
-
nil
|
16
|
-
else
|
17
|
-
hours = (minutes / 60).to_i
|
18
|
-
sprintf("%d:%02d", hours, (minutes - 60 * hours))
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
define_method("#{name}=") do |value|
|
23
|
-
if value.blank?
|
24
|
-
write_attribute(attr, nil)
|
25
|
-
nil
|
26
|
-
else
|
27
|
-
hours, minutes = value.split(/:/)
|
28
|
-
val = minutes.to_i + (hours.to_i * 60)
|
29
|
-
write_attribute(attr, val)
|
30
|
-
value
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
define_method(name_minutes) do
|
35
|
-
minutes = read_attribute(attr)
|
36
|
-
if minutes.nil?
|
37
|
-
nil
|
38
|
-
else
|
39
|
-
minutes
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
define_method("#{name_minutes}=") do |value|
|
44
|
-
if value.blank?
|
45
|
-
write_attribute(attr, nil)
|
46
|
-
nil
|
47
|
-
else
|
48
|
-
write_attribute(attr, value.to_i)
|
49
|
-
value.to_i
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
7
|
+
autoload :ClassMethods, 'mongoid_time_field/class_methods'
|
8
|
+
autoload :Value, 'mongoid_time_field/value'
|
56
9
|
end
|
57
10
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Mongoid::TimeField
|
2
|
+
module ClassMethods
|
3
|
+
# @deprecated
|
4
|
+
def time_field(*columns)
|
5
|
+
[columns].flatten.each do |name|
|
6
|
+
attr = name.to_sym
|
7
|
+
name_minutes = (name.to_s + '_minutes').to_sym
|
8
|
+
|
9
|
+
field attr, type: TimeField.new(format: 'mm:SS')
|
10
|
+
|
11
|
+
# we treat minutes as seconds for compatibility with v0.1.0
|
12
|
+
# TODO: Remove this later
|
13
|
+
define_method(name_minutes) do
|
14
|
+
v = send(attr)
|
15
|
+
v.nil? ? nil : v.seconds
|
16
|
+
end
|
17
|
+
|
18
|
+
define_method("#{name_minutes}=") do |value|
|
19
|
+
send(name.to_s + '=', Mongoid::TimeField::Value.new(value))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
class TimeField
|
2
|
+
def initialize(options = {})
|
3
|
+
@options = options
|
4
|
+
|
5
|
+
@options[:format] = 'mm:SS' unless options[:format]
|
6
|
+
@options[:regexp] = build_regexp(options[:format]) unless options[:regexp]
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_regexp(format)
|
10
|
+
s = '^' + Regexp.escape(format) + '$'
|
11
|
+
s.gsub!(':', ':?')
|
12
|
+
s.gsub!('-', '-?')
|
13
|
+
s.gsub!('_', '_?')
|
14
|
+
|
15
|
+
s.gsub!('HH', '(?<h>\d*?)')
|
16
|
+
s.gsub!('hh', '(?<h>\d*?)')
|
17
|
+
s.gsub!('MM', '(?<m>\d*)')
|
18
|
+
s.gsub!('mm', '(?<m>\d*)')
|
19
|
+
s.gsub!('SS', '(?<s>\d*)')
|
20
|
+
Regexp.new(s)
|
21
|
+
end
|
22
|
+
|
23
|
+
def parse(value)
|
24
|
+
if value.blank?
|
25
|
+
nil
|
26
|
+
else
|
27
|
+
match = @options[:regexp].match(value)
|
28
|
+
seconds = 0
|
29
|
+
names = match.names
|
30
|
+
|
31
|
+
seconds += match['s'].to_i if names.include?('s')
|
32
|
+
seconds += match['m'].to_i * 60 if names.include?('m')
|
33
|
+
seconds += match['h'].to_i * 3600 if names.include?('h')
|
34
|
+
|
35
|
+
Mongoid::TimeField::Value.new(seconds, @options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Get the object as it was stored in the database, and instantiate
|
40
|
+
# this custom class from it.
|
41
|
+
def demongoize(object)
|
42
|
+
if object.nil?
|
43
|
+
nil
|
44
|
+
else
|
45
|
+
Mongoid::TimeField::Value.new(object, @options)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Takes any possible object and converts it to how it would be
|
50
|
+
# stored in the database.
|
51
|
+
def mongoize(object)
|
52
|
+
case object
|
53
|
+
when Mongoid::TimeField::Value then object.mongoize
|
54
|
+
when String then parse(object).mongoize
|
55
|
+
else object
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Converts the object that was supplied to a criteria and converts it
|
60
|
+
# into a database friendly form.
|
61
|
+
def evolve(object)
|
62
|
+
case object
|
63
|
+
when String then parse(object).mongoize
|
64
|
+
when Mongoid::TimeField::Value then object.mongoize
|
65
|
+
else object
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Mongoid::TimeField
|
2
|
+
class Value
|
3
|
+
attr_accessor :seconds
|
4
|
+
|
5
|
+
def initialize(seconds, options = {})
|
6
|
+
if seconds.blank?
|
7
|
+
@seconds = nil
|
8
|
+
else
|
9
|
+
@seconds = seconds
|
10
|
+
end
|
11
|
+
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
# @seconds.nil? ? nil : Time.at(@seconds).utc.strftime(@options[:strftime])
|
17
|
+
|
18
|
+
if @seconds.nil?
|
19
|
+
nil
|
20
|
+
else
|
21
|
+
format = @options[:format]
|
22
|
+
|
23
|
+
fm, ss = @seconds.divmod(60)
|
24
|
+
hh, mm = fm.divmod(60)
|
25
|
+
|
26
|
+
if format.match(/hh/i).nil?
|
27
|
+
replaces = {
|
28
|
+
'mm' => fm,
|
29
|
+
'MM' => fm.to_s.rjust(2, '0'),
|
30
|
+
'SS' => ss.to_s.rjust(2, '0'),
|
31
|
+
}
|
32
|
+
format.gsub(/(mm|MM|SS)/) do |match|
|
33
|
+
replaces[match]
|
34
|
+
end
|
35
|
+
else
|
36
|
+
replaces = {
|
37
|
+
'hh' => hh,
|
38
|
+
'HH' => hh.to_s.rjust(2, '0'),
|
39
|
+
'mm' => mm,
|
40
|
+
'MM' => mm.to_s.rjust(2, '0'),
|
41
|
+
'SS' => ss.to_s.rjust(2, '0'),
|
42
|
+
}
|
43
|
+
format.gsub(/(hh|HH|mm|MM|SS)/) do |match|
|
44
|
+
replaces[match]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
alias_method :to_str, :to_s
|
50
|
+
|
51
|
+
def minutes
|
52
|
+
@seconds / 60
|
53
|
+
end
|
54
|
+
|
55
|
+
def mongoize
|
56
|
+
@seconds
|
57
|
+
end
|
58
|
+
|
59
|
+
def inspect
|
60
|
+
'"' + to_s + '"'
|
61
|
+
end
|
62
|
+
|
63
|
+
def coerce(something)
|
64
|
+
[self, something]
|
65
|
+
end
|
66
|
+
|
67
|
+
def ==(something)
|
68
|
+
if seconds === something
|
69
|
+
true
|
70
|
+
elsif to_s === something
|
71
|
+
true
|
72
|
+
else
|
73
|
+
false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/mongoid_time_field.gemspec
CHANGED
@@ -1,71 +1,28 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid_time_field/version'
|
5
5
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid_time_field"
|
8
|
+
spec.version = MongoidTimeField::VERSION
|
9
|
+
spec.authors = ["glebtv"]
|
10
|
+
spec.email = ["glebtv@gmail.com"]
|
11
|
+
spec.description = %q{Time field (stored as integer with no date) for Mongoid}
|
12
|
+
spec.summary = %q{Time field for Mongoid}
|
13
|
+
spec.homepage = "http://github.com/glebtv/mongoid_time_field"
|
14
|
+
spec.license = "MIT"
|
9
15
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
s.email = "glebtv@gmail.com"
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE.txt",
|
17
|
-
"README.rdoc"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".document",
|
21
|
-
".rspec",
|
22
|
-
".rvmrc",
|
23
|
-
"Gemfile",
|
24
|
-
"Gemfile.lock",
|
25
|
-
"LICENSE.txt",
|
26
|
-
"README.rdoc",
|
27
|
-
"Rakefile",
|
28
|
-
"VERSION",
|
29
|
-
"lib/mongoid_time_field.rb",
|
30
|
-
"mongoid_time_field.gemspec",
|
31
|
-
"spec/spec_helper.rb",
|
32
|
-
"spec/support/dummy_time.rb",
|
33
|
-
"spec/time_spec.rb"
|
34
|
-
]
|
35
|
-
s.homepage = "http://github.com/glebtv/mongoid_time_field"
|
36
|
-
s.licenses = ["MIT"]
|
37
|
-
s.require_paths = ["lib"]
|
38
|
-
s.rubygems_version = "1.8.15"
|
39
|
-
s.summary = "hh:mm or mm:ss time field for mongoid"
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
40
20
|
|
41
|
-
|
42
|
-
s.specification_version = 3
|
21
|
+
spec.add_runtime_dependency 'mongoid', [">= 3.0.0"]
|
43
22
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
50
|
-
s.add_development_dependency(%q<simplecov>, [">= 0.4.0"])
|
51
|
-
s.add_development_dependency(%q<database_cleaner>, [">= 0"])
|
52
|
-
else
|
53
|
-
s.add_dependency(%q<mongoid>, [">= 0"])
|
54
|
-
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
55
|
-
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
56
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
-
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
58
|
-
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
59
|
-
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
60
|
-
end
|
61
|
-
else
|
62
|
-
s.add_dependency(%q<mongoid>, [">= 0"])
|
63
|
-
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
64
|
-
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
65
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
66
|
-
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
67
|
-
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
68
|
-
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
69
|
-
end
|
23
|
+
spec.add_development_dependency "bundler"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency 'rspec'
|
26
|
+
spec.add_development_dependency 'simplecov'
|
27
|
+
spec.add_development_dependency 'database_cleaner'
|
70
28
|
end
|
71
|
-
|
data/spec/spec_helper.rb
CHANGED
@@ -14,10 +14,10 @@ require 'mongoid_time_field'
|
|
14
14
|
# in ./support/ and its subdirectories.
|
15
15
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
Mongoid.
|
20
|
-
|
17
|
+
Mongoid.configure do |config|
|
18
|
+
ENV["MONGOID_ENV"] = "test"
|
19
|
+
Mongoid.load!("spec/support/mongoid.yml")
|
20
|
+
end
|
21
21
|
|
22
22
|
DatabaseCleaner.orm = "mongoid"
|
23
23
|
|
data/spec/support/dummy_time.rb
CHANGED
@@ -0,0 +1,12 @@
|
|
1
|
+
class DummyV2
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
field :description
|
5
|
+
|
6
|
+
field :open, type: TimeField.new(format: "hh:MM")
|
7
|
+
field :close, type: TimeField.new(format: 'hh:MM')
|
8
|
+
|
9
|
+
field :worktime, type: TimeField.new(format: 'HH:MM')
|
10
|
+
field :time_of_day, type: TimeField.new(format: 'HH:MM:SS')
|
11
|
+
field :duration, type: TimeField.new(format: 'mm:SS')
|
12
|
+
end
|
@@ -1,22 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Mongoid::TimeField do
|
4
|
-
|
4
|
+
|
5
5
|
describe 'when persisting a document with a TimeField datatype' do
|
6
|
-
|
6
|
+
|
7
7
|
it 'should be persisted normally' do
|
8
8
|
dummy = DummyTime.new
|
9
9
|
dummy.open = '10:00'
|
10
10
|
dummy.save.should eq true
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
it 'should fix bad time values' do
|
14
14
|
dummy = DummyTime.new
|
15
15
|
dummy.open = '10:61'
|
16
16
|
dummy.save.should eq true
|
17
|
+
|
18
|
+
dummy = DummyTime.first
|
17
19
|
dummy.open.should eq '11:01'
|
18
20
|
end
|
19
|
-
|
21
|
+
|
20
22
|
it 'should handle blank values set as time' do
|
21
23
|
dummy = DummyTime.new
|
22
24
|
dummy.open = ''
|
@@ -32,19 +34,19 @@ describe Mongoid::TimeField do
|
|
32
34
|
dummy.open.should be_nil
|
33
35
|
dummy.open_minutes.should be_nil
|
34
36
|
end
|
35
|
-
|
37
|
+
|
36
38
|
end
|
37
|
-
|
39
|
+
|
38
40
|
describe 'when accessing a document from the datastore with a TimeField datatype' do
|
39
41
|
before(:each) do
|
40
42
|
DummyTime.create(:description => "Test", :open => '12:34')
|
41
43
|
end
|
42
|
-
|
44
|
+
|
43
45
|
it 'should have a time field value that matches the TimeField value that was initially persisted' do
|
44
46
|
dummy = DummyTime.first
|
45
47
|
dummy.open.should eq '12:34'
|
46
48
|
end
|
47
|
-
|
49
|
+
|
48
50
|
it 'should have a minutes field value equal to what was initially persisted as time' do
|
49
51
|
dummy = DummyTime.first
|
50
52
|
dummy.open_minutes.should eq 754
|
@@ -55,24 +57,24 @@ describe Mongoid::TimeField do
|
|
55
57
|
before(:each) do
|
56
58
|
DummyTime.create(:description => "Test", :open_minutes => 754)
|
57
59
|
end
|
58
|
-
|
60
|
+
|
59
61
|
it 'should have a time field value that matches the TimeField value that was initially persisted' do
|
60
62
|
dummy = DummyTime.first
|
61
63
|
dummy.open.should eq '12:34'
|
62
64
|
end
|
63
|
-
|
65
|
+
|
64
66
|
it 'should have a minutes field value equal to what was initially persisted as time' do
|
65
67
|
dummy = DummyTime.first
|
66
68
|
dummy.open_minutes.should eq 754
|
67
69
|
end
|
68
70
|
end
|
69
71
|
|
70
|
-
|
72
|
+
|
71
73
|
describe 'when accessing a document from the datastore with a TimeField datatype and empty value' do
|
72
74
|
before(:each) do
|
73
75
|
DummyTime.create(:description => "Test")
|
74
76
|
end
|
75
|
-
|
77
|
+
|
76
78
|
it 'should have a TimeField value of 0' do
|
77
79
|
dummy = DummyTime.first
|
78
80
|
dummy.open.should be_nil
|
@@ -83,7 +85,7 @@ describe Mongoid::TimeField do
|
|
83
85
|
before(:each) do
|
84
86
|
DummyTime.create(description: "Test", open: '1:23', close: '2:33')
|
85
87
|
end
|
86
|
-
|
88
|
+
|
87
89
|
it 'should have correct TimeField value for field 1' do
|
88
90
|
dummy = DummyTime.first
|
89
91
|
dummy.open.should eq '1:23'
|
data/spec/v2_spec.rb
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::TimeField do
|
4
|
+
describe 'compatibility with v0.1.0' do
|
5
|
+
it 'should store minutes as seconds' do
|
6
|
+
dummy = DummyTime.new
|
7
|
+
dummy.open = '10:00'
|
8
|
+
dummy.save.should eq true
|
9
|
+
dummy.open_minutes.should eq 600
|
10
|
+
|
11
|
+
dummy = DummyTime.first
|
12
|
+
dummy.read_attribute(:open).should eq 600
|
13
|
+
dummy.open_minutes.should eq 600
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'datatype' do
|
18
|
+
|
19
|
+
it 'is persisted normally' do
|
20
|
+
dummy = DummyV2.new
|
21
|
+
dummy.open = '10:00'
|
22
|
+
dummy.save.should eq true
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'allows querying' do
|
26
|
+
DummyV2.create!(open: '10:00')
|
27
|
+
second = DummyV2.create!(open: '10:05')
|
28
|
+
last = DummyV2.create!(open: '11:00')
|
29
|
+
DummyV2.where(open: {'$gt' => 10 * 3600 + 5 * 60 }).count.should eq 1
|
30
|
+
DummyV2.where(open: {'$gt' => '10:05'}).count.should eq 1
|
31
|
+
DummyV2.where(open: '10:05').first.id.should eq second.id
|
32
|
+
DummyV2.where(open: '11:00').first.id.should eq last.id
|
33
|
+
DummyV2.where(open: last.open).first.id.should eq last.id
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'has proper inspect' do
|
37
|
+
dummy = DummyV2.new
|
38
|
+
dummy.open = '10:00'
|
39
|
+
dummy.open.inspect.should eq '"10:00"'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'allows setting seconds' do
|
43
|
+
dummy = DummyV2.new
|
44
|
+
dummy.open = 20 * 60
|
45
|
+
dummy.open.should eq '0:20'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'has proper comparsion and coercion for strings and integers' do
|
49
|
+
dummy = DummyV2.new
|
50
|
+
dummy.open = '10:00'
|
51
|
+
(dummy.open == '10:00').should be_true
|
52
|
+
(dummy.open == (10*60*60)).should be_true
|
53
|
+
(dummy.open == (10*60*60 + 1)).should be_false
|
54
|
+
('10:00' == dummy.open).should be_true
|
55
|
+
((10*60*60) == dummy.open).should be_true
|
56
|
+
((10*60*60 + 1) == dummy.open).should be_false
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'fixes bad time values' do
|
60
|
+
dummy = DummyV2.new
|
61
|
+
dummy.open = '10:61'
|
62
|
+
dummy.save.should eq true
|
63
|
+
dummy.open.should eq '11:01'
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'handles blank values set as time' do
|
67
|
+
dummy = DummyV2.new
|
68
|
+
dummy.open = ''
|
69
|
+
dummy.save.should eq true
|
70
|
+
dummy.open.should be_nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe 'loading from DB' do
|
75
|
+
before(:each) do
|
76
|
+
DummyV2.create(:description => "Test", :open => '12:34')
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'has same value' do
|
80
|
+
dummy = DummyV2.first
|
81
|
+
dummy.open.should eq '12:34'
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'has proper minutes and seconds' do
|
85
|
+
dummy = DummyV2.first
|
86
|
+
dummy.open.minutes.should eq 754
|
87
|
+
dummy.open.seconds.should eq (754*60)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'empty value' do
|
92
|
+
before(:each) do
|
93
|
+
DummyV2.create(:description => "Test")
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should have a TimeField value of 0' do
|
97
|
+
dummy = DummyV2.first
|
98
|
+
dummy.open.should be_nil
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'multiple' do
|
103
|
+
before(:each) do
|
104
|
+
DummyV2.create(description: "Test", open: '1:23', close: '2:33')
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should have correct TimeField value for field 1' do
|
108
|
+
dummy = DummyV2.first
|
109
|
+
dummy.open.should eq '1:23'
|
110
|
+
end
|
111
|
+
it 'should have correct TimeField value for field 2' do
|
112
|
+
dummy = DummyV2.first
|
113
|
+
dummy.close.should eq '2:33'
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe 'formats' do
|
118
|
+
it 'works with HH:MM' do
|
119
|
+
dummy = DummyV2.new
|
120
|
+
dummy.worktime = '1:00'
|
121
|
+
dummy.save.should eq true
|
122
|
+
dummy.worktime.should eq '01:00'
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'works with HH:MM:SS' do
|
126
|
+
dummy = DummyV2.new
|
127
|
+
dummy.time_of_day = '1:00'
|
128
|
+
dummy.save.should eq true
|
129
|
+
dummy.time_of_day.should eq '00:01:00'
|
130
|
+
dummy.time_of_day.seconds.should eq 60
|
131
|
+
dummy.time_of_day.minutes.should eq 1
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'works with mm:SS' do
|
135
|
+
dummy = DummyV2.new
|
136
|
+
dummy.duration = '1:00'
|
137
|
+
dummy.save.should eq true
|
138
|
+
dummy.duration.should eq '1:00'
|
139
|
+
dummy.duration.seconds.should eq 60
|
140
|
+
dummy.duration.minutes.should eq 1
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'mm:SS handles more minutes' do
|
144
|
+
dummy = DummyV2.new
|
145
|
+
dummy.duration = '120:00'
|
146
|
+
dummy.save.should eq true
|
147
|
+
dummy.duration.should eq '120:00'
|
148
|
+
dummy.duration.seconds.should eq (120 * 60)
|
149
|
+
dummy.duration.minutes.should eq 120
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'HH:MM handles more hours' do
|
153
|
+
dummy = DummyV2.new
|
154
|
+
dummy.worktime = '121:00'
|
155
|
+
dummy.save.should eq true
|
156
|
+
dummy.worktime.should eq '121:00'
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'HH:MM:SS handles more hours' do
|
160
|
+
dummy = DummyV2.new
|
161
|
+
dummy.time_of_day = '120:00:00'
|
162
|
+
dummy.save.should eq true
|
163
|
+
dummy.time_of_day.should eq '120:00:00'
|
164
|
+
dummy.time_of_day.minutes.should eq (120 * 60)
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'HH:MM:SS normalizes' do
|
168
|
+
dummy = DummyV2.new
|
169
|
+
dummy.time_of_day = '1:70:00'
|
170
|
+
dummy.save.should eq true
|
171
|
+
dummy.time_of_day.should eq '02:10:00'
|
172
|
+
dummy.time_of_day.minutes.should eq (130)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
metadata
CHANGED
@@ -1,141 +1,157 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_time_field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- glebtv
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-06-07 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: mongoid
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
19
|
+
version: 3.0.0
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: rspec
|
27
|
-
requirement: &75429980 !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
29
23
|
requirements:
|
30
|
-
- -
|
24
|
+
- - '>='
|
31
25
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
33
|
-
type: :development
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *75429980
|
26
|
+
version: 3.0.0
|
36
27
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement:
|
39
|
-
none: false
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
40
30
|
requirements:
|
41
|
-
- -
|
31
|
+
- - '>='
|
42
32
|
- !ruby/object:Gem::Version
|
43
|
-
version: '
|
33
|
+
version: '0'
|
44
34
|
type: :development
|
45
35
|
prerelease: false
|
46
|
-
version_requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement:
|
50
|
-
none: false
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
51
44
|
requirements:
|
52
|
-
- -
|
45
|
+
- - '>='
|
53
46
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
47
|
+
version: '0'
|
55
48
|
type: :development
|
56
49
|
prerelease: false
|
57
|
-
version_requirements:
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
58
55
|
- !ruby/object:Gem::Dependency
|
59
|
-
name:
|
60
|
-
requirement:
|
61
|
-
none: false
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
62
58
|
requirements:
|
63
|
-
- -
|
59
|
+
- - '>='
|
64
60
|
- !ruby/object:Gem::Version
|
65
|
-
version:
|
61
|
+
version: '0'
|
66
62
|
type: :development
|
67
63
|
prerelease: false
|
68
|
-
version_requirements:
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
|
-
requirement:
|
72
|
-
none: false
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
73
72
|
requirements:
|
74
|
-
- -
|
73
|
+
- - '>='
|
75
74
|
- !ruby/object:Gem::Version
|
76
|
-
version: 0
|
75
|
+
version: '0'
|
77
76
|
type: :development
|
78
77
|
prerelease: false
|
79
|
-
version_requirements:
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
80
83
|
- !ruby/object:Gem::Dependency
|
81
84
|
name: database_cleaner
|
82
|
-
requirement:
|
83
|
-
none: false
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
84
86
|
requirements:
|
85
|
-
- -
|
87
|
+
- - '>='
|
86
88
|
- !ruby/object:Gem::Version
|
87
89
|
version: '0'
|
88
90
|
type: :development
|
89
91
|
prerelease: false
|
90
|
-
version_requirements:
|
91
|
-
|
92
|
-
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Time field (stored as integer with no date) for Mongoid
|
98
|
+
email:
|
99
|
+
- glebtv@gmail.com
|
93
100
|
executables: []
|
94
101
|
extensions: []
|
95
|
-
extra_rdoc_files:
|
96
|
-
- LICENSE.txt
|
97
|
-
- README.rdoc
|
102
|
+
extra_rdoc_files: []
|
98
103
|
files:
|
99
|
-
- .
|
104
|
+
- .gitignore
|
100
105
|
- .rspec
|
101
|
-
- .
|
106
|
+
- .ruby-gemset
|
107
|
+
- .ruby-version
|
102
108
|
- Gemfile
|
103
109
|
- Gemfile.lock
|
104
110
|
- LICENSE.txt
|
105
111
|
- README.rdoc
|
106
112
|
- Rakefile
|
107
|
-
- VERSION
|
108
113
|
- lib/mongoid_time_field.rb
|
114
|
+
- lib/mongoid_time_field/class_methods.rb
|
115
|
+
- lib/mongoid_time_field/time_field.rb
|
116
|
+
- lib/mongoid_time_field/value.rb
|
117
|
+
- lib/mongoid_time_field/version.rb
|
109
118
|
- mongoid_time_field.gemspec
|
110
119
|
- spec/spec_helper.rb
|
120
|
+
- spec/support/dummy_duration.rb
|
111
121
|
- spec/support/dummy_time.rb
|
112
|
-
- spec/
|
122
|
+
- spec/support/dummy_v2.rb
|
123
|
+
- spec/support/mongoid.yml
|
124
|
+
- spec/v1_spec.rb
|
125
|
+
- spec/v2_spec.rb
|
113
126
|
homepage: http://github.com/glebtv/mongoid_time_field
|
114
127
|
licenses:
|
115
128
|
- MIT
|
129
|
+
metadata: {}
|
116
130
|
post_install_message:
|
117
131
|
rdoc_options: []
|
118
132
|
require_paths:
|
119
133
|
- lib
|
120
134
|
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
135
|
requirements:
|
123
|
-
- -
|
136
|
+
- - '>='
|
124
137
|
- !ruby/object:Gem::Version
|
125
138
|
version: '0'
|
126
|
-
segments:
|
127
|
-
- 0
|
128
|
-
hash: -523699249
|
129
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
-
none: false
|
131
140
|
requirements:
|
132
|
-
- -
|
141
|
+
- - '>='
|
133
142
|
- !ruby/object:Gem::Version
|
134
143
|
version: '0'
|
135
144
|
requirements: []
|
136
145
|
rubyforge_project:
|
137
|
-
rubygems_version:
|
146
|
+
rubygems_version: 2.0.3
|
138
147
|
signing_key:
|
139
|
-
specification_version:
|
140
|
-
summary:
|
141
|
-
test_files:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Time field for Mongoid
|
150
|
+
test_files:
|
151
|
+
- spec/spec_helper.rb
|
152
|
+
- spec/support/dummy_duration.rb
|
153
|
+
- spec/support/dummy_time.rb
|
154
|
+
- spec/support/dummy_v2.rb
|
155
|
+
- spec/support/mongoid.yml
|
156
|
+
- spec/v1_spec.rb
|
157
|
+
- spec/v2_spec.rb
|
data/.document
DELETED
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use ruby-1.9.3-p125@timefield --create
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.1.0
|