date_parity 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in date_parity.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Scott Stewart
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # DateParity
2
+
3
+ Consistent date formatting per request; creating parity between date.to\_s and
4
+ string.to\_date.
5
+
6
+ Provides a Date.format syntax and usage, similiar to Time.zone in rails. Can be
7
+ set per request (i.e. per user).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'date_parity'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install date_parity
22
+
23
+ ## Usage
24
+
25
+ In Rails controller before\_filter:
26
+ Date.format = '%m/%d/%Y'
27
+ or
28
+ Date.format = current\_user.date\_format
29
+
30
+ In any view assuming @resourc\_created\_on is a date::
31
+ @resource.start\_on
32
+ will render: 12/31/2012
33
+
34
+ And then you get parity when a form with this field is posted.
35
+ Rails will attempt to convert the posted string value of "12/31/2012"
36
+ using "12/31/2012".to\_date in the respective model before validation.
37
+ Under normal circumstances, @resource.start\_on will end up being nil
38
+ because "12/31/2012".to\_date is invalid for Ruby Date.\_parse.
39
+
40
+ When date\_parity is required, Date will use the format string stored on Date.formt
41
+ to correctly parse "12/31/2012". This is accomplished by overriding String.to\_date
42
+ to use : Date.strptime(self, Date.format).to\_s(:db)
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
51
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+
9
+ task :default => :test
10
+
11
+ Rake::TestTask.new do |t|
12
+ t.libs << 'test/lib'
13
+ t.pattern = 'test/*_test.rb'
14
+ end
15
+
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/date_parity/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "date_parity"
6
+ gem.version = DateParity::VERSION
7
+
8
+ gem.author = 'Scott Stewart'
9
+ gem.email = 'scottpstewart@gamil.com'
10
+ gem.description = 'Consistent date formatting done simple'
11
+ gem.summary = 'Consistent date formatting done simple'
12
+ gem.homepage = ""
13
+
14
+ gem.add_dependency 'activesupport', '>= 3.2'
15
+
16
+ gem.add_development_dependency 'rake'
17
+ gem.add_development_dependency 'minitest'
18
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ gem.files = `git ls-files`.split($\)
20
+ end
@@ -0,0 +1,12 @@
1
+ class Date
2
+ class << self
3
+ def format
4
+ Thread.current[:date_format] || Date::DATE_FORMATS[:db] || "%Y-%m-%d"
5
+ end
6
+ def format=(format)
7
+ Thread.current[:date_format] = format
8
+ end
9
+ end
10
+ end
11
+
12
+
@@ -0,0 +1,13 @@
1
+ class String
2
+
3
+ def to_date_from_formatted_string
4
+ return nil if self.blank?
5
+ Date.strptime(self, Date.format)
6
+ rescue
7
+ to_rails_date
8
+ end
9
+
10
+ alias_method :to_rails_date, :to_date
11
+ alias_method :to_date, :to_date_from_formatted_string
12
+
13
+ end
@@ -0,0 +1,3 @@
1
+ module DateParity
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'date'
2
+ require 'active_support/core_ext/date/conversions'
3
+ require 'active_support/core_ext/string/conversions'
4
+
5
+ require "date_parity/version"
6
+ require 'date_parity/core_ext/string'
7
+ require 'date_parity/core_ext/date'
8
+
9
+ # ActiveSupport provides an implementation of Date.to_s that uses
10
+ # Date::DATE_FORMATS[:default] to format date the string by default.
11
+ # We create a default DATE_FORMAT here that uses a proc that respects
12
+ # our custom Date.format value, which can be set per thread.
13
+ Date::DATE_FORMATS[:default] = lambda { |date| date.strftime(Date.format)}
14
+
15
+
@@ -0,0 +1,65 @@
1
+ require 'date_parity/test_helper'
2
+
3
+ class DateParityTest < MiniTest::Unit::TestCase
4
+
5
+ def test_date_to_string_respects_format
6
+ date = Date.parse("2012-08-12")
7
+
8
+ Date.format = "%m/%d/%Y"
9
+ assert_equal "08/12/2012", date.to_s
10
+
11
+ Date.format = "%d/%m/%Y"
12
+ assert_equal "12/08/2012", date.to_s
13
+
14
+ Date.format = '%Y-%m-%d'
15
+ assert_equal "2012-08-12", date.to_s
16
+
17
+ Date.format = '%d.%m.%Y'
18
+ assert_equal "12.08.2012", date.to_s
19
+
20
+ Date.format = '%Y.%m.%d'
21
+ assert_equal "2012.08.12", date.to_s
22
+
23
+ Date.format = '%Y/%m/%d'
24
+ assert_equal "2012/08/12", date.to_s
25
+
26
+ end
27
+
28
+ def test_string_to_date_respects_format
29
+ Date.format = "%m/%d/%Y"
30
+ assert_equal Date.parse("2012-08-12"), "08/12/2012".to_date
31
+
32
+ Date.format = "%d/%m/%Y"
33
+ assert_equal Date.parse("2012-08-12"), "12/08/2012".to_date
34
+
35
+ Date.format = '%Y-%m-%d'
36
+ assert_equal Date.parse("2012-08-12"), "2012-08-12".to_date
37
+
38
+ Date.format = '%d.%m.%Y'
39
+ assert_equal Date.parse("2012-08-12"), "12.08.2012".to_date
40
+
41
+ Date.format = '%Y.%m.%d'
42
+ assert_equal Date.parse("2012-08-12"), "2012.08.12".to_date
43
+
44
+ Date.format = '%Y/%m/%d'
45
+ assert_equal Date.parse("2012-08-12"), "2012/08/12".to_date
46
+ end
47
+
48
+ def test_invalid_date_still_raises_argument_error
49
+ # Assert that the normal ArgumentError that gets
50
+ # raised in Rails when parsing an invalid string
51
+ # to_date still gets raised
52
+ assert_raises ArgumentError, 'invalid date' do
53
+ "12/31/2012".to_date
54
+ end
55
+ end
56
+
57
+ def full_date_parity
58
+ Date.format = "%m/%d/%Y"
59
+ date = Date.parse("2012-12-31")
60
+ assert date, date.to_s.to_date
61
+
62
+ assert "12/31/2012", "12/31/2012".to_date.to_s
63
+ end
64
+
65
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'date_parity'
4
+
5
+ require 'minitest/unit'
6
+ MiniTest::Unit.autorun
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_parity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott Stewart
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70139748526520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70139748526520
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70139748526100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70139748526100
36
+ - !ruby/object:Gem::Dependency
37
+ name: minitest
38
+ requirement: &70139748525640 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70139748525640
47
+ description: Consistent date formatting done simple
48
+ email: scottpstewart@gamil.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - date_parity.gemspec
59
+ - lib/date_parity.rb
60
+ - lib/date_parity/core_ext/date.rb
61
+ - lib/date_parity/core_ext/string.rb
62
+ - lib/date_parity/version.rb
63
+ - test/date_parity_test.rb
64
+ - test/lib/date_parity/test_helper.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: -3237689646668649695
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: -3237689646668649695
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.11
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Consistent date formatting done simple
95
+ test_files:
96
+ - test/date_parity_test.rb
97
+ - test/lib/date_parity/test_helper.rb