time_of_day_attr 0.0.1

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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = TimeOfDayAttr
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'TimeOfDay'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ de:
2
+ time_of_day:
3
+ formats:
4
+ default: '%k.%M'
5
+ hour: '%k'
@@ -0,0 +1,5 @@
1
+ en:
2
+ time_of_day:
3
+ formats:
4
+ default: '%k:%M'
5
+ hour: '%k'
@@ -0,0 +1,3 @@
1
+ module TimeOfDayAttr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,53 @@
1
+ module TimeOfDayAttr
2
+
3
+ class << self
4
+
5
+ def delocalize(value, options = {})
6
+ format = options[:format] || :default
7
+ format = translate_format(format) if format.is_a?(Symbol)
8
+ time = Time.strptime(value, format)
9
+ time.seconds_since_midnight.to_i
10
+ end
11
+
12
+ def localize(value, options = {})
13
+ format = options[:format] || :default
14
+ format = translate_format(format) if format.is_a?(Symbol)
15
+ time = Time.now.at_midnight + value.seconds
16
+ time_of_day = time.strftime(format)
17
+ if options[:omit_minutes_at_full_hour]
18
+ if time_of_day.end_with?('00')
19
+ time_of_day = time_of_day[0...-3]
20
+ end
21
+ end
22
+ time_of_day
23
+ end
24
+ alias :l :localize
25
+
26
+ def translate_format(format)
27
+ I18n.translate("time_of_day.formats.#{format}")
28
+ end
29
+ end
30
+
31
+ extend ActiveSupport::Concern
32
+
33
+ included do
34
+ end
35
+
36
+ module ClassMethods
37
+ def time_of_day_attr *attrs
38
+ options = attrs.extract_options!
39
+ formats = options[:formats] || [:default, :hour]
40
+ attrs.each do |attr|
41
+ define_method("#{attr}=") do |value|
42
+ if value.is_a?(String)
43
+ delocalized_values = formats.map { |format| TimeOfDayAttr.delocalize(value, format: format) rescue nil }.compact
44
+ value = delocalized_values.first || send(attr)
45
+ end
46
+ super(value)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ ActiveRecord::Base.send :include, TimeOfDayAttr
@@ -0,0 +1,3 @@
1
+ class BusinessHour < ActiveRecord::Base
2
+ time_of_day_attr :opening, :closing
3
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Migration.verbose = false
2
+
3
+ ActiveRecord::Schema.define do
4
+
5
+ create_table :business_hours, :force => true do |t|
6
+ t.integer :opening
7
+ t.string :closing
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require 'active_record'
5
+ require 'test/unit'
6
+ require 'time_of_day_attr'
7
+
8
+ files = Dir[File.join(File.dirname(__FILE__), '../config/locales/*.yml')]
9
+ I18n.load_path.concat(files)
10
+
11
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
12
+
13
+ require File.expand_path('../schema', __FILE__)
14
+
15
+ Dir["#{File.dirname(__FILE__)}/models/**/*.rb"].each { |f| require f }
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class TimeOfDayAttrTest < ActiveSupport::TestCase
4
+
5
+ test 'delocalize time of day' do
6
+ assert_equal 36000, TimeOfDayAttr.delocalize('10:00')
7
+ assert_equal 55800, TimeOfDayAttr.delocalize('15:30')
8
+ assert_equal 61140, TimeOfDayAttr.delocalize('16:59')
9
+ assert_equal 32400, TimeOfDayAttr.delocalize('9:00')
10
+ assert_equal 32400, TimeOfDayAttr.delocalize('09:00')
11
+ assert_equal 32400, TimeOfDayAttr.delocalize('9', format: :hour)
12
+ end
13
+
14
+ test 'localize time of day' do
15
+ assert_equal ' 9:00', TimeOfDayAttr.localize(32400)
16
+ assert_equal '14:45', TimeOfDayAttr.localize(53100)
17
+ assert_equal ' 0:00', TimeOfDayAttr.localize(86400)
18
+ end
19
+
20
+ test 'localize time of day and omit minutes at full hour' do
21
+ assert_equal ' 9', TimeOfDayAttr.localize(32400, omit_minutes_at_full_hour: true)
22
+ assert_equal '14', TimeOfDayAttr.localize(50400, omit_minutes_at_full_hour: true)
23
+ 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
+ end
26
+
27
+ test 'time of day attr setter should delocalize value' do
28
+ business_hour = BusinessHour.new(opening: '9:00', closing: '17:00')
29
+ assert_equal 32400, business_hour.opening
30
+ assert_equal 61200, business_hour.closing
31
+ end
32
+
33
+ test 'time of day attr setter should delocalize hour formatted value' do
34
+ business_hour = BusinessHour.new(opening: '9', closing: '17')
35
+ assert_equal 32400, business_hour.opening
36
+ assert_equal 61200, business_hour.closing
37
+ end
38
+
39
+ test 'time of day attr setter should ignore invalid formats' do
40
+ business_hour = BusinessHour.new(opening: 'Nine', closing: 'Five')
41
+ assert_nil business_hour.opening
42
+ assert_nil business_hour.closing
43
+ business_hour.opening = '9'
44
+ assert_equal 32400, business_hour.opening
45
+ business_hour.opening = 'Nine'
46
+ assert_equal 32400, business_hour.opening
47
+ business_hour.opening = '9:30'
48
+ assert_equal 34200, business_hour.opening
49
+ business_hour.opening = 55800
50
+ assert_equal 55800, business_hour.opening
51
+ business_hour.opening = nil
52
+ assert_nil business_hour.opening
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_of_day_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Clemens Teichmann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.14
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.14
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Convert time of day to seconds since midnight and back. Formats can be
47
+ defined in your translation files. To enable autoconverting use time_of_day_attr
48
+ in your models.
49
+ email:
50
+ - clemens_t@web.de
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - config/locales/time_of_day.en.yml
56
+ - config/locales/time_of_day.de.yml
57
+ - lib/time_of_day_attr.rb
58
+ - lib/time_of_day_attr/version.rb
59
+ - MIT-LICENSE
60
+ - Rakefile
61
+ - README.rdoc
62
+ - test/models/business_hour.rb
63
+ - test/schema.rb
64
+ - test/test_helper.rb
65
+ - test/time_of_day_attr_test.rb
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.23
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Store time of day as seconds since midnight
90
+ test_files:
91
+ - test/models/business_hour.rb
92
+ - test/schema.rb
93
+ - test/test_helper.rb
94
+ - test/time_of_day_attr_test.rb