stricter-string-to-date 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 stricter-string-to-date.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jeff Dean
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.
@@ -0,0 +1,34 @@
1
+ # Stricter::String::To::Date
2
+
3
+ ActiveSupport 4.0 changed `String#to_date` to raise a more descriptive error for invalid dates.
4
+
5
+ It also makes date parsing more liberal than ActiveSupport 3.x.
6
+
7
+ ```ruby
8
+ # ActiveSupport 3.x
9
+ "asdf".to_date # => NoMethodError: undefined method `div' for nil:NilClass
10
+ "333".to_date # => NoMethodError: undefined method `div' for nil:NilClass
11
+
12
+ # ActiveSupport 4.x
13
+ "asdf".to_date # => ArgumentError: invalid date
14
+ "333".to_date # => Fri, 29 Nov 2013
15
+ ```
16
+
17
+ You can install this gem if you:
18
+
19
+ * Rely on the old 3.x behavior and want to upgrade to ActiveSupport 4.x without dealing with it yet
20
+ * Are on 3.x and want to see deprecation warnings about the changes that will come in 4.x
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ gem 'stricter-string-to-date'
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install stricter-string-to-date
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require 'stricter_string_to_date'
@@ -0,0 +1,28 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/date'
3
+ require 'active_support/core_ext/string/conversions'
4
+ require 'active_support/core_ext/object/blank'
5
+ require 'active_support/deprecation'
6
+
7
+ String.class_eval do
8
+ def to_date
9
+ unless blank?
10
+ begin
11
+ date_values = ::Date._parse(self, false).values_at(:year, :mon, :mday)
12
+ ::Date.new(*date_values)
13
+ rescue NoMethodError => e
14
+ if e.message == "undefined method `div' for nil:NilClass"
15
+ parsed_date = ::Date.parse(self, false) rescue nil
16
+ if parsed_date
17
+ message = %Q{"#{self}".to_date will become a valid date "#{parsed_date}"}
18
+ ActiveSupport::Deprecation.warn message
19
+ else
20
+ message = %Q{"NoMethodError: undefined method `div' for nil:NilClass" will raise "ArgumentError: Invalid Date" in "ActiveSupport: 4.0"}
21
+ ActiveSupport::Deprecation.warn message
22
+ end
23
+ end
24
+ raise e
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module StricterStringToDate
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'rspec'
2
+ require_relative '../lib/stricter_string_to_date'
3
+
4
+ describe String do
5
+
6
+ describe "#to_date" do
7
+
8
+ context "with a date that was invalid in ActiveSupport 3.2.13" do
9
+ it "should raise the same method that ActiveSupport 3.2.13" do
10
+ expect do
11
+ capture(:stderr) { "333".to_date }
12
+ end.to raise_error(NoMethodError, "undefined method `div' for nil:NilClass")
13
+ end
14
+ end
15
+
16
+ context "the date is valid in ActiveSupport 4.0" do
17
+ it "should render a deprecation warning about the date becoming valid" do
18
+ stderr_output = capture(:stderr) { expect { "333".to_date }.to raise_error }
19
+ stderr_output.should include(%Q{DEPRECATION WARNING: "333".to_date will become a valid date "#{Date.parse("333")}"})
20
+ end
21
+ end
22
+
23
+ context "the date is also invalid in ActiveSupport 4.0" do
24
+ it "should render a deprecation warning about the exception class change" do
25
+ stderr_output = capture(:stderr) { expect{ "asdf".to_date }.to raise_error }
26
+ stderr_output.should include(%Q{DEPRECATION WARNING: "NoMethodError: undefined method `div' for nil:NilClass" will raise "ArgumentError: Invalid Date" in "ActiveSupport: 4.0"})
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stricter_string_to_date/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stricter-string-to-date"
8
+ spec.version = StricterStringToDate::VERSION
9
+ spec.authors = ["Jeff Dean"]
10
+ spec.email = ["jeff@zilkey.com"]
11
+ spec.description = %q{Restores ActiveSupport 3.x String#to_date functionality}
12
+ spec.summary = %q{Restores ActiveSupport 3.x String#to_date functionality}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "activesupport", "> 3.0"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stricter-string-to-date
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Dean
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>'
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
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: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Restores ActiveSupport 3.x String#to_date functionality
79
+ email:
80
+ - jeff@zilkey.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/stricter-string-to-date.rb
91
+ - lib/stricter_string_to_date.rb
92
+ - lib/stricter_string_to_date/version.rb
93
+ - spec/string_spec.rb
94
+ - stricter-string-to-date.gemspec
95
+ homepage: ''
96
+ licenses:
97
+ - MIT
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.24
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Restores ActiveSupport 3.x String#to_date functionality
120
+ test_files:
121
+ - spec/string_spec.rb