time_of_day_attr 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/time_of_day_attr.rb +6 -4
- data/lib/time_of_day_attr/active_record_ext.rb +11 -4
- data/lib/time_of_day_attr/version.rb +1 -1
- data/test/schema.rb +1 -1
- data/test/time_of_day_attr_test.rb +8 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 918454e7a1335742ab83a0756f6a736976f997a1
|
4
|
+
data.tar.gz: 5c8561a4f1d40431c167c1b75eed948bf56e1043
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e27f14a0adf5295b5d0681963d932e40ae79f1999e4055e8ae497bd63be7809c084d37e3489b51a0319774136105ce4ba05206829502a2b7d0fa11ff1aaea871
|
7
|
+
data.tar.gz: 967e0f9f3f3e5ef2f459112fdb3652dbea0eed12e96d506b381fc08a5b9d3e70e86b0d01cf271bfd3518cead2e8beebf3c792fb1a2a08cb4903b9a9d44630969
|
data/lib/time_of_day_attr.rb
CHANGED
@@ -6,13 +6,13 @@ files = Dir[File.join(File.dirname(__FILE__), '../config/locales/*.yml')]
|
|
6
6
|
I18n.load_path.concat(files)
|
7
7
|
|
8
8
|
module TimeOfDayAttr
|
9
|
-
|
9
|
+
|
10
10
|
class << self
|
11
11
|
|
12
12
|
def delocalize(value, options = {})
|
13
13
|
format = options[:format] || :default
|
14
14
|
format = translate_format(format) if format.is_a?(Symbol)
|
15
|
-
time = Time.strptime(value, format)
|
15
|
+
time = Time.strptime(value, format).change(month: 1, day: 1)
|
16
16
|
time.seconds_since_midnight.to_i
|
17
17
|
end
|
18
18
|
|
@@ -20,17 +20,19 @@ module TimeOfDayAttr
|
|
20
20
|
return value unless value.respond_to?(:seconds)
|
21
21
|
format = options[:format] || :default
|
22
22
|
format = translate_format(format) if format.is_a?(Symbol)
|
23
|
-
time = Time.now.at_midnight + value.seconds
|
23
|
+
time = Time.now.beginning_of_year.at_midnight + value.seconds
|
24
24
|
time_of_day = time.strftime(format)
|
25
25
|
if options[:omit_minutes_at_full_hour]
|
26
26
|
if time_of_day.end_with?('00')
|
27
|
-
time_of_day = time_of_day[0...-3]
|
27
|
+
time_of_day = time_of_day[0...-3]
|
28
28
|
end
|
29
29
|
end
|
30
30
|
time_of_day
|
31
31
|
end
|
32
32
|
alias :l :localize
|
33
33
|
|
34
|
+
private
|
35
|
+
|
34
36
|
def translate_format(format)
|
35
37
|
I18n.translate("time_of_day.formats.#{format}")
|
36
38
|
end
|
@@ -4,16 +4,23 @@ module TimeOfDayAttr
|
|
4
4
|
|
5
5
|
module ClassMethods
|
6
6
|
|
7
|
-
def time_of_day_attr
|
7
|
+
def time_of_day_attr(*attrs)
|
8
8
|
options = attrs.extract_options!
|
9
9
|
options[:formats] ||= [:default, :hour]
|
10
10
|
attrs.each do |attr|
|
11
11
|
define_method("#{attr}=") do |value|
|
12
12
|
if value.is_a?(String)
|
13
|
-
delocalized_values = options[:formats].map
|
14
|
-
|
13
|
+
delocalized_values = options[:formats].map do |format|
|
14
|
+
begin
|
15
|
+
TimeOfDayAttr.delocalize(value, format: format)
|
16
|
+
rescue ArgumentError
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
delocalized_value = delocalized_values.compact.first
|
21
|
+
raise(ArgumentError, "invalid time of day #{value} for formats #{options[:formats].join(', ')}") unless delocalized_value
|
15
22
|
end
|
16
|
-
super(value)
|
23
|
+
super(delocalized_value || value)
|
17
24
|
end
|
18
25
|
end
|
19
26
|
end
|
data/test/schema.rb
CHANGED
@@ -40,13 +40,18 @@ class TimeOfDayAttrTest < ActiveSupport::TestCase
|
|
40
40
|
assert_equal 61200, business_hour.closing
|
41
41
|
end
|
42
42
|
|
43
|
-
test 'time of day attr setter should
|
44
|
-
|
43
|
+
test 'time of day attr setter should raise invalid formats' do
|
44
|
+
assert_raise ArgumentError do
|
45
|
+
business_hour = BusinessHour.new(opening: 'Nine', closing: 'Five')
|
46
|
+
end
|
47
|
+
business_hour = BusinessHour.new
|
45
48
|
assert_nil business_hour.opening
|
46
49
|
assert_nil business_hour.closing
|
47
50
|
business_hour.opening = '9'
|
48
51
|
assert_equal 32400, business_hour.opening
|
49
|
-
|
52
|
+
assert_raise ArgumentError do
|
53
|
+
business_hour.opening = 'Nine'
|
54
|
+
end
|
50
55
|
assert_equal 32400, business_hour.opening
|
51
56
|
business_hour.opening = '9:30'
|
52
57
|
assert_equal 34200, business_hour.opening
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_of_day_attr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clemens Teichmann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|