american_date_parsing 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NGQxMjM3N2VjYzczYTFmOGNhMjUxOTQ4ZGZmZWMyMjg2OTQ5YzNhZg==
5
+ data.tar.gz: !binary |-
6
+ ZTRiY2FkYWFjYzVhOWQyY2EzNTdhMzAzMGE1NDFlNmFjMjdjNjVkYg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OWU0ODM0ZDE1Y2IxMjI0YmU1NTE4M2JiZTY2ZTZlZTlhMjZhZTc1MmU2Njkw
10
+ YzJlYmE1MThkMGIzYjhkYzg0NWRkYjA2ZDdiMWZlNjY0Yzk5MWZjMDlmYWIz
11
+ YmJkZjY1NzM4OTUxMTIyNDc1NTkxNTM4ZGZhMTE3NWI0YWIxMjY=
12
+ data.tar.gz: !binary |-
13
+ N2NiNzNiMzg5ZmVkOWJiODk1YTY4Njg3MTU3NGZlMTkxNDY5NjE5M2E3NDBl
14
+ MzVkYmYyZTYyNjRhYjNlZWFlZmNkNmQ3MTVjMTQ0OWM0OTUzYWJmMjFlMzlk
15
+ NDZiM2FjNTIwMjVjMjE4MGU2OGY1YzQwNDFjMDczZjQ5N2Q4OGY=
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 american_date_parsing.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ben Eddy
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,107 @@
1
+ # AmericanDateParsing
2
+
3
+ American-style (e.g 12/25/2014) date parsing for ActiveRecord and ActiveModel.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'american_date_parsing'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ Invoke `parse_as_americanized_date` in a model.
18
+
19
+ ```Ruby
20
+ class MyModel < ActiveRecord::Base
21
+ parse_as_americanized_date :activates_on, :expires_on
22
+ end
23
+
24
+ instance = MyModel.new
25
+
26
+ instance.activates_on = "12/25/2012"
27
+ instance.activates_on
28
+ # => Tue, 25 Dec 2012
29
+
30
+ instance.expires_on = Date.new(2012, 12, 25)
31
+ instance.expires_on
32
+ # => Tue, 25 Dec 2012
33
+ ```
34
+
35
+ ## Validation
36
+
37
+ Presence and format validations are added by passing options to `parse_as_americanized_date`. Note: make sure to `include ActiveModel::Validations` for `ActiveModel` subclasses.
38
+
39
+ ```Ruby
40
+ class MyModel < ActiveRecord::Base
41
+ parse_as_americanized_date :activates_on, validate: {
42
+ presence: true,
43
+ format: true
44
+ }
45
+ end
46
+
47
+ instance = MyModel.new
48
+
49
+ instance.valid?
50
+ instance.errors[:activates_on]
51
+ # => ["can't be blank"]
52
+
53
+ instance.activates_on = "1225-2012"
54
+ instance.valid?
55
+ instance.errors[:activates_on]
56
+ # => ["is invalid"]
57
+
58
+ instance.activates_on = "12-25-2012"
59
+ instance.valid?
60
+ instance.errors[:activates_on]
61
+ # => []
62
+ ```
63
+
64
+ ### Custom validation messages
65
+
66
+ Just as with standard validations, `:validate` accepts a `:message` option.
67
+
68
+ ```Ruby
69
+ class MyModel < ActiveRecord::Base
70
+ parse_as_americanized_date :activates_on, validate: {
71
+ presence: {message: "is required"},
72
+ format: {message: "needs to be formatted as MM/DD/YYYY"}
73
+ }
74
+ end
75
+
76
+ instance = MyModel.new
77
+
78
+ instance.valid?
79
+ instance.errors[:activates_on]
80
+ # => ["is required"]
81
+
82
+ instance.activates_on = "1225-2012"
83
+ instance.valid?
84
+ instance.errors[:activates_on]
85
+ # => ["needs to be formatted as MM/DD/YYYY"]
86
+ ```
87
+
88
+ Alternatively, custom messaging can be acheived via i18n.
89
+
90
+
91
+ ```YAML
92
+ en:
93
+ errors:
94
+ attributes:
95
+ activates_on:
96
+ blank: is required
97
+ invalid: needs to be formatted as MM/DD/YYYY
98
+ ```
99
+
100
+
101
+ ## Contributing
102
+
103
+ 1. Fork it
104
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
105
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
106
+ 4. Push to the branch (`git push origin my-new-feature`)
107
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'american_date_parsing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "american_date_parsing"
8
+ spec.version = AmericanDateParsing::VERSION
9
+ spec.authors = ["Ben Eddy"]
10
+ spec.email = ["bae@foraker.com"]
11
+ spec.description = %q{American date parsing for ActiveRecord and ActiveModel}
12
+ spec.summary = %q{American date parsing for ActiveRecord and ActiveModel}
13
+ spec.homepage = "https://github.com/foraker/american_date_parsing"
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_dependency "chronic"
22
+ spec.add_dependency "activemodel"
23
+ spec.add_dependency "activesupport"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "minitest"
28
+ end
@@ -0,0 +1,109 @@
1
+ require "active_model"
2
+ require "active_support/core_ext"
3
+ require "chronic"
4
+
5
+ module AmericanDateParsing
6
+ mattr_writer :delimiter, :date_format
7
+
8
+ DEFAULT_DELIMITER = "/|-"
9
+
10
+ def self.delimiter
11
+ DEFAULT_DELIMITER
12
+ end
13
+
14
+ def self.date_format
15
+ @date_format ||= %r{\d{1,2}#{delimiter}\d{1,2}#{delimiter}\d{2,4}}
16
+ end
17
+
18
+ def parse_as_americanized_date(*attributes)
19
+ options = attributes.extract_options!
20
+
21
+ if !defined?(ActiveRecord::Base) || !(self < ActiveRecord::Base)
22
+ send(:include, AmericanDateParsing::Accessors)
23
+ end
24
+
25
+ attributes.each do |attribute|
26
+ attr_accessor :"_raw_#{attribute}"
27
+
28
+ if options[:validate].present?
29
+ validates attribute, 'american_date_parsing/american_date' => options[:validate]
30
+ end
31
+
32
+ define_method "#{attribute}=" do |date_string|
33
+ parsed = if date_string.respond_to?(:strftime)
34
+ date_string.to_date
35
+ else
36
+ Chronic.parse(date_string.to_s).try(:to_date)
37
+ end
38
+
39
+ send(:"_raw_#{attribute}=", date_string)
40
+ write_attribute(attribute, parsed)
41
+ end
42
+ end
43
+ end
44
+
45
+ module Accessors
46
+ def write_attribute(attribute, value)
47
+ instance_variable_set("@#{attribute}", value)
48
+ end
49
+
50
+ def read_attribute(attribute)
51
+ instance_variable_get("@#{attribute}")
52
+ end
53
+ end
54
+
55
+ class AmericanDateValidator < ActiveModel::EachValidator
56
+ def validate_each(record, attribute, value)
57
+ Validation.new({
58
+ value: record.read_attribute(attribute),
59
+ raw: record.send(:"_raw_#{attribute}"),
60
+ options: options
61
+ }).errors.each do |error, opts|
62
+ opts = opts.respond_to?(:keys) ? opts : {}
63
+ record.errors.add(attribute, error, opts)
64
+ end
65
+ end
66
+
67
+ class Validation
68
+ def initialize(options)
69
+ @value = options[:value]
70
+ @raw = options[:raw]
71
+ @options = options[:options]
72
+ end
73
+
74
+ def errors
75
+ [format_error, presence_error].compact
76
+ end
77
+
78
+ private
79
+
80
+ attr_reader :value, :raw, :options
81
+
82
+ def presence_error
83
+ [:blank, options[:presence]] if validate_presence? && blank?
84
+ end
85
+
86
+ def format_error
87
+ [:invalid, options[:format]] if validate_format? && format_mismatch?
88
+ end
89
+
90
+ def blank?
91
+ value.blank? && raw.blank?
92
+ end
93
+
94
+ def format_mismatch?
95
+ raw.present? &&
96
+ value.blank? &&
97
+ raw !~ AmericanDateParsing.date_format
98
+ end
99
+
100
+ def validate_format?
101
+ options[:format]
102
+ end
103
+
104
+ def validate_presence?
105
+ options[:presence]
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module AmericanDateParsing
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,88 @@
1
+ require 'minitest'
2
+ require 'minitest/autorun'
3
+
4
+ require File.expand_path("../../lib/american_date_parsing", __FILE__)
5
+
6
+ describe AmericanDateParsing do
7
+ class TestModel
8
+ include ActiveModel
9
+ include ActiveModel::Validations
10
+ extend AmericanDateParsing
11
+
12
+ attr_accessor :date
13
+
14
+ parse_as_americanized_date :date, validate: {
15
+ format: true,
16
+ presence: true
17
+ }
18
+ end
19
+
20
+ def with_date(string)
21
+ instance = TestModel.new
22
+ instance.date = string
23
+ instance.date
24
+ end
25
+
26
+ def errors(string)
27
+ instance = TestModel.new
28
+ instance.date = string
29
+ instance.valid?
30
+ instance.errors[:date]
31
+ end
32
+
33
+ describe "validations" do
34
+ it "accepts a valid american date" do
35
+ errors("12/25/2012").wont_include('is invalid')
36
+ end
37
+
38
+ it "accepts hyphen delimiters" do
39
+ errors("12-25-2012").wont_include('is invalid')
40
+ end
41
+
42
+ it "accepts abbreviated delimiters" do
43
+ errors("12-25-2012").wont_include('is invalid')
44
+ end
45
+
46
+ it "rejects non-delimited dates" do
47
+ errors("12252012").must_include('is invalid')
48
+ end
49
+
50
+ it "rejects nonsense dates" do
51
+ errors("cats").must_include('is invalid')
52
+ end
53
+
54
+ it "rejects missing dates" do
55
+ errors("").must_include('can\'t be blank')
56
+ end
57
+ end
58
+
59
+ describe "setting" do
60
+ it "accepts American style dates" do
61
+ with_date("12/25/2012").must_equal Date.new(2012, 12, 25)
62
+ end
63
+
64
+ it "accepts hyphen delimiters" do
65
+ with_date("12-25-2012").must_equal Date.new(2012, 12, 25)
66
+ end
67
+
68
+ it "accepts abberviated years" do
69
+ with_date("12/25/12").must_equal Date.new(2012, 12, 25)
70
+ end
71
+
72
+ it "accepts dates" do
73
+ with_date(Date.today).must_equal Date.today
74
+ end
75
+
76
+ it "accepts times" do
77
+ with_date(Date.today.to_time).must_equal Date.today
78
+ end
79
+
80
+ it "accepts nil" do
81
+ with_date(nil).must_equal nil
82
+ end
83
+
84
+ it "sets nonsense dates to nil" do
85
+ with_date("cats").must_equal nil
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: american_date_parsing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Eddy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chronic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: American date parsing for ActiveRecord and ActiveModel
98
+ email:
99
+ - bae@foraker.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - american_date_parsing.gemspec
110
+ - lib/american_date_parsing.rb
111
+ - lib/american_date_parsing/version.rb
112
+ - test/american_date_parsing_spec.rb
113
+ homepage: https://github.com/foraker/american_date_parsing
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.0.3
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: American date parsing for ActiveRecord and ActiveModel
137
+ test_files:
138
+ - test/american_date_parsing_spec.rb
139
+ has_rdoc: