mongoid_time_field 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 537b202c74ae99d9c2fe7564506bfc3acfced49d
4
- data.tar.gz: ef56ebe5b5854312e4c4b663a49e5332c53bbe8d
3
+ metadata.gz: 6b650d7bcdd423e0ebffe2899b18dbd171d4cc80
4
+ data.tar.gz: 97248e826c2dff4f57d3f8da3ecf053d3e425ead
5
5
  SHA512:
6
- metadata.gz: 0f88679289a058e07f67ac5c1afc54c4590f7772916584372337ff55688827bd19d9f10bd01b204203e71498a1899e6ef6e57610658391070b8781b3d6bb52a3
7
- data.tar.gz: 53f5e2d89037f2d43b7785975da46acaa7989238d245f166f5b9af130f6321cbc59b57401e4aca7eab3e709379282e2277ef3f009088ede5de30ccf7dbd07645
6
+ metadata.gz: ce9ca402375d3a586947fe46ffcb94761270efe95bf622f1cb539f446c727f20c07a92fd345f3c86546b55133bd2f56a7da7533019654b28a9626c543dd747c3
7
+ data.tar.gz: 1ce4f7fd8a957637379c9160ccdd0c03e32c0ffc9af7eddd6095ce805ab69f40033f2347b5c522b2d45b1750bf8db3274480cbff4ead98689c0c66a732a5ee96
@@ -1 +1 @@
1
- 2.0.0-p195
1
+ 2.0.0-p353
File without changes
@@ -9,6 +9,12 @@ module Mongoid
9
9
  end
10
10
  end
11
11
 
12
+ if Object.const_defined?("TimeValidator")
13
+ puts "[WARN] Mongoid Time Field: TimeValidator is already defined, not loading ours"
14
+ else
15
+ require "mongoid_time_field/validator"
16
+ end
17
+
12
18
  if Object.const_defined?("RailsAdmin")
13
19
  require 'rails_admin/adapters/mongoid'
14
20
  require 'rails_admin/config/fields/types/text'
@@ -0,0 +1,14 @@
1
+ class TimeValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ unless options[:less_than].blank?
4
+ unless value < Mongoid::TimeField::Value.new(options[:less_than], value.format)
5
+ record.errors.add attribute, (options[:message] || "должно быть меньше #{options[:less_than]}")
6
+ end
7
+ end
8
+ unless options[:greater_than].blank?
9
+ unless value > Mongoid::TimeField::Value.new(options[:greater_than], value.format)
10
+ record.errors.add attribute, (options[:message] || "должно быть больше #{options[:greater_than]}")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -18,6 +18,10 @@ module Mongoid::TimeField
18
18
  seconds.__bson_dump__(io, key)
19
19
  end
20
20
 
21
+ def format
22
+ @options[:format].dup
23
+ end
24
+
21
25
  def to_s
22
26
  if @seconds.nil?
23
27
  nil
@@ -73,6 +77,16 @@ module Mongoid::TimeField
73
77
  '"' + to_s + '"'
74
78
  end
75
79
 
80
+ def >(x)
81
+ raise ArgumentError, 'Argument is not Mongoid::TimeField::Value' unless x.is_a? Mongoid::TimeField::Value
82
+ @seconds > x.seconds
83
+ end
84
+
85
+ def <(x)
86
+ raise ArgumentError, 'Argument is not Mongoid::TimeField::Value' unless x.is_a? Mongoid::TimeField::Value
87
+ @seconds < x.seconds
88
+ end
89
+
76
90
  def coerce(something)
77
91
  [self, something]
78
92
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidTimeField
2
- VERSION = "0.2.9"
2
+ VERSION = "0.3.0"
3
3
  end
File without changes
@@ -0,0 +1,6 @@
1
+ class DummyHhmm
2
+ include Mongoid::Document
3
+
4
+ field :t, type: TimeField.new(format: 'HH:MM')
5
+ validates :t, time: { less_than: 24*60*60, greater_than: 0 }
6
+ end
File without changes
File without changes
@@ -260,4 +260,31 @@ describe Mongoid::TimeField do
260
260
  dummy.save.should eq true
261
261
  end
262
262
  end
263
+
264
+ describe 'HH:MM' do
265
+ it 'stores properly' do
266
+ h = DummyHhmm.create!(t: '12:30')
267
+ h.t.should eq '12:30'
268
+ h.t.to_i.should eq ( (12 * 60) + 30) * 60
269
+ h.reload
270
+ h.t.should eq '12:30'
271
+ h.t.to_i.should eq ( (12 * 60) + 30) * 60
272
+ end
273
+
274
+ it 'validates' do
275
+ h = DummyHhmm.new(t: '24:30')
276
+ h.valid?.should be_false
277
+ h = DummyHhmm.new(t: '23:30')
278
+ h.valid?.should be_true
279
+ end
280
+ end
281
+
282
+ describe 'Value' do
283
+ it 'does compare' do
284
+ opt = {format: 'mm:SS'}
285
+ Mongoid::TimeField::Value.new(120, opt).to_s.should eq '2:00'
286
+ (Mongoid::TimeField::Value.new(120, opt) > Mongoid::TimeField::Value.new(119, opt)).should be_true
287
+ end
288
+
289
+ end
263
290
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_time_field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebtv
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-16 00:00:00.000000000 Z
11
+ date: 2013-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -113,11 +113,13 @@ files:
113
113
  - lib/mongoid_time_field.rb
114
114
  - lib/mongoid_time_field/class_methods.rb
115
115
  - lib/mongoid_time_field/time_field.rb
116
+ - lib/mongoid_time_field/validator.rb
116
117
  - lib/mongoid_time_field/value.rb
117
118
  - lib/mongoid_time_field/version.rb
118
119
  - mongoid_time_field.gemspec
119
120
  - spec/spec_helper.rb
120
121
  - spec/support/dummy_duration.rb
122
+ - spec/support/dummy_hhmm.rb
121
123
  - spec/support/dummy_time.rb
122
124
  - spec/support/dummy_v2.rb
123
125
  - spec/support/dummy_validate.rb
@@ -144,13 +146,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
146
  version: '0'
145
147
  requirements: []
146
148
  rubyforge_project:
147
- rubygems_version: 2.0.3
149
+ rubygems_version: 2.1.11
148
150
  signing_key:
149
151
  specification_version: 4
150
152
  summary: Time field for Mongoid
151
153
  test_files:
152
154
  - spec/spec_helper.rb
153
155
  - spec/support/dummy_duration.rb
156
+ - spec/support/dummy_hhmm.rb
154
157
  - spec/support/dummy_time.rb
155
158
  - spec/support/dummy_v2.rb
156
159
  - spec/support/dummy_validate.rb