time_of_day_attr 0.0.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab0ac090f636ee04c329736bd7f775f40fd65ba4
4
- data.tar.gz: d7feee73d836a73b72f857b474b0489bea6a9e5c
3
+ metadata.gz: eada9ab6b9945f028e72aa4335e1bcfbf6227238
4
+ data.tar.gz: f5f0071ce565c37812395a7bd055c8d2b223ed0d
5
5
  SHA512:
6
- metadata.gz: 1110a3aec230b82a7483d0cc31d410e61a7f9c294a44f22b7d34bf1463fd5ae36e9ca78932672ca0dd623f72b49359efb5aaadeed1eec64c4a8775edc8d47079
7
- data.tar.gz: 9867243a77fbc695403ddee5b8c8e55d49ea14df4ae5bb9967fd327e2860ad3201ba64c4840c4861f82ec6f3db19e3e5e6ca68d1790310c231f4c94fae0a1e1d
6
+ metadata.gz: f9ce89c3c480c445091ae538c7426cf417d70a7d1bcb2c02a17d4c861af6d946a593e91262567eec414319cf8185268ede8c14a32bf8b3881ceabaddf533ce59
7
+ data.tar.gz: 18cd51daf9d5c0f90f86c6f6b3c6ee5987738aa6a8b54d56a9a7d1c6af859715744c65685c0add7c6ac5c0ff157de624b996baf37ae763be8019eb310785cff2
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
- # time_of_day_attr
1
+ # Time of day attributes for your Rails model
2
2
  [![Gem Version](https://badge.fury.io/rb/time_of_day_attr.png)](http://badge.fury.io/rb/time_of_day_attr) [![Code Climate](https://codeclimate.com/github/clemenst/time_of_day_attr.png)](https://codeclimate.com/github/clemenst/time_of_day_attr) [![License](https://img.shields.io/npm/l/express.svg?style=flat)](http://clemenst.mit-license.org)
3
3
 
4
- Convert time of day to seconds since midnight and back.
4
+ This ruby gem converts time of day to seconds since midnight and back. The seconds value is stored in the database and can be used for calculations and validations.
5
5
 
6
6
  ## Installation
7
7
 
@@ -43,7 +43,7 @@ TimeOfDayAttr.l(business_hour.opening, omit_minutes_at_full_hour: true)
43
43
 
44
44
  ### Formats
45
45
 
46
- The standard formats for conversation are 'default' and 'hour'.
46
+ The standard formats for conversion are 'default' and 'hour'.
47
47
  ```yml
48
48
  en:
49
49
  time_of_day:
@@ -60,7 +60,7 @@ en:
60
60
  custom: '%H-%M'
61
61
  ```
62
62
 
63
- Pass the formats you want for conversation:
63
+ Pass the formats you want for conversion:
64
64
  ```ruby
65
65
  class BusinessHour < ActiveRecord::Base
66
66
  time_of_day_attr :opening, formats: [:custom]
@@ -13,15 +13,25 @@ module TimeOfDayAttr
13
13
  format = options[:format] || :default
14
14
  format = translate_format(format) if format.is_a?(Symbol)
15
15
  time = Time.strptime(value, format).change(month: 1, day: 1)
16
- time.seconds_since_midnight.to_i
16
+ seconds = time.seconds_since_midnight.to_i
17
+ if seconds.zero? && value.starts_with?('24')
18
+ 24.hours.to_i
19
+ else
20
+ seconds
21
+ end
17
22
  end
18
23
 
19
24
  def localize(value, options = {})
20
25
  return value unless value.respond_to?(:seconds)
21
26
  format = options[:format] || :default
22
27
  format = translate_format(format) if format.is_a?(Symbol)
28
+ # Switch to beginning of year to prevent wrong time on the day of time change
29
+ # see https://en.wikipedia.org/wiki/Daylight_saving_time
23
30
  time = Time.now.beginning_of_year.at_midnight + value.seconds
24
31
  time_of_day = time.strftime(format)
32
+ if 24.hours.to_i == value
33
+ time_of_day.gsub!(' 0', '24')
34
+ end
25
35
  if options[:omit_minutes_at_full_hour]
26
36
  if time_of_day.end_with?('00')
27
37
  time_of_day = time_of_day[0...-3]
@@ -13,7 +13,11 @@ module TimeOfDayAttr
13
13
  delocalized_values = options[:formats].map do |format|
14
14
  begin
15
15
  TimeOfDayAttr.delocalize(value, format: format)
16
- rescue ArgumentError
16
+ rescue ArgumentError => e
17
+ if 'argument out of range' == e.message
18
+ super(nil)
19
+ return
20
+ end
17
21
  nil
18
22
  end
19
23
  end
@@ -1,3 +1,3 @@
1
1
  module TimeOfDayAttr
2
- VERSION = '0.0.8'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -14,14 +14,12 @@ class TimeOfDayAttrTest < ActiveSupport::TestCase
14
14
  test 'localize time of day' do
15
15
  assert_equal ' 9:00', TimeOfDayAttr.localize(32400)
16
16
  assert_equal '14:45', TimeOfDayAttr.localize(53100)
17
- assert_equal ' 0:00', TimeOfDayAttr.localize(86400)
18
17
  end
19
18
 
20
19
  test 'localize time of day and omit minutes at full hour' do
21
20
  assert_equal ' 9', TimeOfDayAttr.localize(32400, omit_minutes_at_full_hour: true)
22
21
  assert_equal '14', TimeOfDayAttr.localize(50400, omit_minutes_at_full_hour: true)
23
22
  assert_equal '14:45', TimeOfDayAttr.localize(53100, omit_minutes_at_full_hour: true)
24
- assert_equal ' 0', TimeOfDayAttr.localize(86400, omit_minutes_at_full_hour: true)
25
23
  end
26
24
 
27
25
  test 'localizing nil should return nil' do
@@ -54,5 +52,21 @@ class TimeOfDayAttrTest < ActiveSupport::TestCase
54
52
  assert_equal 55800, business_hour.opening
55
53
  business_hour.opening = nil
56
54
  assert_nil business_hour.opening
55
+ business_hour.opening = '25:00'
56
+ assert_nil business_hour.opening
57
+ business_hour.opening = '24:30'
58
+ assert_nil business_hour.opening
59
+ end
60
+
61
+
62
+ test '24 should be usable' do
63
+ business_hour = BusinessHour.new(opening: '0', closing: '24')
64
+ assert_equal 0, business_hour.opening
65
+ assert_equal 86400, business_hour.closing
66
+ assert_equal ' 0:00', TimeOfDayAttr.localize(business_hour.opening)
67
+ assert_equal '24:00', TimeOfDayAttr.localize(business_hour.closing)
68
+ assert_equal ' 0', TimeOfDayAttr.localize(business_hour.opening, omit_minutes_at_full_hour: true)
69
+ assert_equal '24', TimeOfDayAttr.localize(business_hour.closing, omit_minutes_at_full_hour: true)
57
70
  end
71
+
58
72
  end
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.8
4
+ version: 1.0.0
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-03-29 00:00:00.000000000 Z
11
+ date: 2015-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,10 +38,9 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Adds time_of_day_attr to your models. Will try to convert time of day
42
- to seconds since midnight when a string was set. Localized formats for conversation
43
- can be added to your translation files. TimeOfDayAttr.localize converts seconds
44
- since midnight back to time of day.
41
+ description: This ruby gem converts time of day to seconds since midnight and back.
42
+ The seconds value is stored in the database and can be used for calculations and
43
+ validations.
45
44
  email:
46
45
  - clemens_t@web.de
47
46
  executables: []
@@ -79,12 +78,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
78
  version: '0'
80
79
  requirements: []
81
80
  rubyforge_project:
82
- rubygems_version: 2.2.2
81
+ rubygems_version: 2.4.5.1
83
82
  signing_key:
84
83
  specification_version: 4
85
- summary: Convert time of day to seconds since midnight and back.
84
+ summary: Time of day attributes for your Rails model
86
85
  test_files:
86
+ - test/time_of_day_attr_test.rb
87
87
  - test/models/business_hour.rb
88
- - test/schema.rb
89
88
  - test/test_helper.rb
90
- - test/time_of_day_attr_test.rb
89
+ - test/schema.rb