interpret_date 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b03eb126b4b4f727346a4d23a576a2015adba70d
4
+ data.tar.gz: 933639b6e8e3db12d063238426140d98a1eb2f4b
5
+ SHA512:
6
+ metadata.gz: fe687df6723b8592ff8620e960e864b8cb52196c95272f81352d58446c84434b0a3011957de19c32ad51b37f38bc9e74f4d6de24b07e4d565a5b0faf7e46f26d
7
+ data.tar.gz: d8d47e84dd941bc979ab43f16a391c84ec714193333a2eb8e76fe233f4f7b427c22e93be9187a952d255ff94703b16e1b03b0e33313b66d6bcea0d0e72f04e50
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in interpret_date.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Keith Thompson
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
+ # InterpretDate
2
+
3
+ [![Build
4
+ status](https://travis-ci.org/sqm/interpret_date.svg?branch=master)](https://travis-ci.org/sqm/interpret_date)
5
+
6
+ Module to provide helper methods for interpreting American formatted date strings.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'interpret_date'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install interpret_date
21
+
22
+ ## Usage
23
+
24
+ To utilize `interpret_date`, mix in the `InterpretDate` module into a
25
+ class that needs to interpret American formatted dates into ruby Date
26
+ objects and pass the date string into the `interpret_date` or
27
+ `interpret_dob_date`.
28
+
29
+ Example (setting attributes for an example `ActiveRecord` model):
30
+
31
+ ```ruby
32
+ class Parcel < ActiveRecord::Base
33
+ include InterpretDate
34
+
35
+ def shipping_date=(value)
36
+ super(interpret_date(value))
37
+ end
38
+
39
+ def birthdate=(value)
40
+ super(interpret_dob_date(value))
41
+ end
42
+ end
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/sqm/interpret_date/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ require 'irb'
10
+ require 'irb/completion'
11
+ require 'interpret_date'
12
+ ARGV.clear
13
+ IRB.start
14
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'interpret_date/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "interpret_date"
8
+ spec.version = InterpretDate::VERSION
9
+ spec.authors = ["Squaremouth"]
10
+ spec.email = ["developers@squaremouth.com"]
11
+ spec.summary = %q{Mixin for easily parsing American formatted date strings into Date objects.}
12
+ spec.description = %q{Module to provide helper methods for interpreting American formatted date strings.}
13
+ spec.homepage = "https://github.com/sqm/interpret_date"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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 "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ end
@@ -0,0 +1,3 @@
1
+ module InterpretDate
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,78 @@
1
+ require "interpret_date/version"
2
+
3
+ module InterpretDate
4
+ def interpret_date(date_input, buffer = 10)
5
+ # the number of years previous turn of the century + buffer number of years
6
+ @century_divider = Date.today.year + buffer - current_century
7
+ convert_date(date_input)
8
+ end
9
+
10
+ def interpret_dob_date(date_input)
11
+ # the number of years since previous turn of the century
12
+ @century_divider = Date.today.year - current_century
13
+ convert_date(date_input)
14
+ end
15
+
16
+ private
17
+
18
+ def current_century
19
+ "#{Date.today.to_s[0..1]}00".to_i
20
+ end
21
+
22
+ def contains_only_six_digits(value)
23
+ /^\d{6}$/ =~ value
24
+ end
25
+
26
+ def convert_date(date_input)
27
+ # Only run the interpret_date code if we have a non-nil string,
28
+ # otherwise, use #to_date to strip any time or timezone info
29
+ return date_input.to_date unless date_input.is_a?(String) || date_input.nil?
30
+
31
+ if contains_only_six_digits(date_input)
32
+ # The date_input only contains digits, and must
33
+ # be in the form MMDDYY
34
+ month = date_input[0,2].to_i
35
+ day = date_input[2,2].to_i
36
+ year = date_input[4,2].to_i
37
+ elsif slash_based_date(date_input)
38
+ # Break the string apart using the delimiters '-', '/', and '.', making each element an integer
39
+ month, day, year = date_input.split(/[-\.\/]+/).collect { |element| element.to_i }
40
+ else
41
+ return nil
42
+ end
43
+
44
+ # Do not allow a three-digit year
45
+ return nil if year.to_s.size == 3
46
+
47
+ # Modify two-digit years
48
+ if year < 100
49
+ # @century_divider represents the year where we
50
+ # divide 2 digit years from the last century or the current one
51
+ if year > @century_divider
52
+ year += current_century - 100
53
+ else
54
+ year += current_century
55
+ end
56
+ end
57
+
58
+ begin
59
+ Date.new(year, month, day)
60
+ rescue
61
+ nil
62
+ end
63
+ end
64
+
65
+ # -----------------------------------------
66
+ # Does the input string match this pattern
67
+ # and associated variations?
68
+ # * m/d/yy
69
+ # * mm/dd/yy
70
+ # * mm/dd/yyyy
71
+ # * m-d-yy
72
+ # * mm-dd-yy
73
+ # * mm-dd-yyyy
74
+ # -----------------------------------------
75
+ def slash_based_date(value)
76
+ /^\d{1,2}([-\.\/]+)\d{1,2}\1\d{2,4}$/ =~ value
77
+ end
78
+ end
@@ -0,0 +1,70 @@
1
+ RSpec.describe InterpretDate do
2
+ include InterpretDate
3
+
4
+ describe "#interpret_date" do
5
+ let(:test_date) { Date.new(1990, 2, 6) }
6
+
7
+ {
8
+ "mmddyy" => "020690",
9
+ "m/d/yy" => "2/6/90",
10
+ "m/d/yyyy" => "2/6/1990",
11
+ "mm/dd/yy" => "02/06/90",
12
+ "mm/dd/yyyy" => "02/06/1990",
13
+ "m-d-yy" => "2-6-90",
14
+ "m-d-yyyy" => "2-6-1990",
15
+ "mm-dd-yy" => "02-06-90",
16
+ "mm-dd-yyyy" => "02-06-1990",
17
+ "m.d.yy" => "2.6.90",
18
+ "m.d.yyyy" => "2.6.1990",
19
+ "mm.dd.yy" => "02.06.90",
20
+ "mm.dd.yyyy" => "02.06.1990",
21
+ }.each do |format, value|
22
+ it "interprets dates in the form of #{format} properly" do
23
+ expect(interpret_date(value)).to eq(test_date)
24
+ end
25
+ end
26
+
27
+ context "with two digit year" do
28
+ it "interprets the years based on the century break + default 10 year buffer" do
29
+ buffer = 10
30
+ century_break = (Date.today.year + buffer).to_s[2..3].to_i
31
+ expect(interpret_date("01/01/#{century_break}")).to eq(Date.new(2000 + century_break, 1, 1))
32
+ century_break += 1
33
+ expect(interpret_date("01/01/#{century_break}")).to eq(Date.new(1900 + century_break, 1, 1))
34
+ end
35
+
36
+ it "interprets the years based on the century break + assigned buffer" do
37
+ buffer = 1
38
+ century_break = (Date.today.year + buffer).to_s[2..3].to_i
39
+ expect(interpret_date("01/01/#{century_break}", buffer)).to eq(Date.new(2000 + century_break, 1, 1))
40
+ century_break += 1
41
+ expect(interpret_date("01/01/#{century_break}", buffer)).to eq(Date.new(1900 + century_break, 1, 1))
42
+ end
43
+ end
44
+
45
+ it "returns the date if passed a Time object" do
46
+ expect(interpret_date(Time.now)).to eq(Date.today)
47
+ end
48
+
49
+ it "returns the date if passed a Date object" do
50
+ expect(interpret_date(Date.today)).to eq(Date.today)
51
+ end
52
+
53
+ it "returns nil if the input passed in cannot be parsed into a date" do
54
+ expect(interpret_date("Kevin Bacon")).to eq(nil)
55
+ end
56
+ end
57
+
58
+ describe "#interpret_dob_date" do
59
+ context "with two digit year" do
60
+ it "interprets the years based on the century break" do
61
+ century_break = (Date.today.year).to_s[2..3].to_i
62
+ # Person with dob of 01/01/15 would yield 01/01/2015
63
+ expect(interpret_dob_date("01/01/#{century_break}")).to eq(Date.new(2000 + century_break, 1, 1))
64
+ century_break += 1
65
+ # Person with dob of 01/01/16 would yield 01/01/1916 since 2016 hasn't occured yet.
66
+ expect(interpret_dob_date("01/01/#{century_break}")).to eq(Date.new(1900 + century_break, 1, 1))
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,86 @@
1
+ require "bundler/setup"
2
+ Bundler.setup
3
+
4
+ require "interpret_date"
5
+ # This file was generated by the `rspec --init` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
8
+ # this file to always be loaded, without a need to explicitly require it in any
9
+ # files.
10
+ #
11
+ # Given that it is always loaded, you are encouraged to keep this file as
12
+ # light-weight as possible. Requiring heavyweight dependencies from this file
13
+ # will add to the boot time of your test suite on EVERY test run, even for an
14
+ # individual file that may not need all of that loaded. Instead, consider making
15
+ # a separate helper file that requires the additional dependencies and performs
16
+ # the additional setup, and require it from the spec files that actually need
17
+ # it.
18
+ #
19
+ # The `.rspec` file also contains a few flags that are not defaults but that
20
+ # users commonly want.
21
+ #
22
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23
+ RSpec.configure do |config|
24
+ # rspec-expectations config goes here. You can use an alternate
25
+ # assertion/expectation library such as wrong or the stdlib/minitest
26
+ # assertions if you prefer.
27
+ config.expect_with :rspec do |expectations|
28
+ # This option will default to `true` in RSpec 4. It makes the `description`
29
+ # and `failure_message` of custom matchers include text for helper methods
30
+ # defined using `chain`, e.g.:
31
+ # be_bigger_than(2).and_smaller_than(4).description
32
+ # # => "be bigger than 2 and smaller than 4"
33
+ # ...rather than:
34
+ # # => "be bigger than 2"
35
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36
+ end
37
+
38
+ # rspec-mocks config goes here. You can use an alternate test double
39
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
40
+ config.mock_with :rspec do |mocks|
41
+ # Prevents you from mocking or stubbing a method that does not exist on
42
+ # a real object. This is generally recommended, and will default to
43
+ # `true` in RSpec 4.
44
+ mocks.verify_partial_doubles = true
45
+ end
46
+
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is
55
+ # recommended. For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Run specs in random order to surface order dependencies. If you find an
76
+ # order dependency and want to debug it, you can fix the order by providing
77
+ # the seed, which is printed after each run.
78
+ # --seed 1234
79
+ config.order = :random
80
+
81
+ # Seed global randomization in this process using the `--seed` CLI option.
82
+ # Setting this allows you to use `--seed` to deterministically reproduce
83
+ # test failures related to randomization by passing the same `--seed` value
84
+ # as the one that triggered the failure.
85
+ Kernel.srand config.seed
86
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interpret_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Squaremouth
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: Module to provide helper methods for interpreting American formatted
56
+ date strings.
57
+ email:
58
+ - developers@squaremouth.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - interpret_date.gemspec
71
+ - lib/interpret_date.rb
72
+ - lib/interpret_date/version.rb
73
+ - spec/interpret_date_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: https://github.com/sqm/interpret_date
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Mixin for easily parsing American formatted date strings into Date objects.
99
+ test_files:
100
+ - spec/interpret_date_spec.rb
101
+ - spec/spec_helper.rb