accountant_date 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec"
10
+ gem "rdoc"
11
+ gem "bundler"
12
+ gem "jeweler"
13
+ gem "simplecov"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.7)
12
+ multi_json (1.6.0)
13
+ rake (10.0.3)
14
+ rdoc (3.12.1)
15
+ json (~> 1.4)
16
+ rspec (2.12.0)
17
+ rspec-core (~> 2.12.0)
18
+ rspec-expectations (~> 2.12.0)
19
+ rspec-mocks (~> 2.12.0)
20
+ rspec-core (2.12.2)
21
+ rspec-expectations (2.12.1)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.12.2)
24
+ simplecov (0.7.1)
25
+ multi_json (~> 1.0)
26
+ simplecov-html (~> 0.7.1)
27
+ simplecov-html (0.7.1)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler
34
+ jeweler
35
+ rdoc
36
+ rspec
37
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Ryan Winograd
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,47 @@
1
+ = accountant_date
2
+
3
+ accountant_date monkey-patches ruby's Date class to add parsing functionality
4
+ useful for parsing dates entered by accountants.
5
+
6
+ This gem is based on american style dates (e.g., mm/dd/yy) and is compatible
7
+ with (american_date[https://github.com/jeremyevans/ruby-american_date]).
8
+
9
+ Accountants often deal with financial periods (mm/yy) and they also hate typing
10
+ extra characters (e.g., '/') since they enter so many dates. This gem enables
11
+ ruby to parse the following formats:
12
+
13
+ * [m]{1,2}/yy
14
+ * [m]{1,2}yy
15
+ * [m]{1,2}ddyy
16
+
17
+ In other words, to parse 1 Feb 2001, the following date entries could be parsed:
18
+
19
+ * 201
20
+ * 0201
21
+ * 02/01
22
+
23
+ Or to get 3 Feb 2001:
24
+
25
+ * 20301
26
+ * 020301
27
+ * 2/3/01
28
+
29
+ == Thanks
30
+
31
+ A big thanks to Jeremy Evans for the inspiration provided by his american_date
32
+ gem: https://github.com/jeremyevans/ruby-american_date.
33
+
34
+ == Contributing to accountant_date
35
+
36
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
37
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
38
+ * Fork the project.
39
+ * Start a feature/bugfix branch.
40
+ * Commit and push until you are happy with your contribution.
41
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
42
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
43
+
44
+ == Copyright
45
+
46
+ Copyright (c) 2013 Ryan Winograd. See LICENSE.txt for
47
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "accountant_date"
18
+ gem.homepage = "http://github.com/rylwin/accountant_date"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Accounting-style date parsing}
21
+ gem.description = %Q{Accountants deals with dates that are simply months (e.g., 01/13). accountant_date parses these mm/yy style dates and allows for other shorthands.}
22
+ gem.email = "ryan@thewinograds.com"
23
+ gem.authors = ["Ryan Winograd"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "accountant_date #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "accountant_date"
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Winograd"]
12
+ s.date = "2013-02-14"
13
+ s.description = "Accountants deals with dates that are simply months (e.g., 01/13). accountant_date parses these mm/yy style dates and allows for other shorthands."
14
+ s.email = "ryan@thewinograds.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "accountant_date.gemspec",
29
+ "lib/accountant_date.rb",
30
+ "spec/accountant_date_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = "http://github.com/rylwin/accountant_date"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.10"
37
+ s.summary = "Accounting-style date parsing"
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<rspec>, [">= 0"])
44
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
45
+ s.add_development_dependency(%q<bundler>, [">= 0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
47
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 0"])
50
+ s.add_dependency(%q<rdoc>, [">= 0"])
51
+ s.add_dependency(%q<bundler>, [">= 0"])
52
+ s.add_dependency(%q<jeweler>, [">= 0"])
53
+ s.add_dependency(%q<simplecov>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 0"])
57
+ s.add_dependency(%q<rdoc>, [">= 0"])
58
+ s.add_dependency(%q<bundler>, [">= 0"])
59
+ s.add_dependency(%q<jeweler>, [">= 0"])
60
+ s.add_dependency(%q<simplecov>, [">= 0"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,74 @@
1
+ require 'date'
2
+
3
+ if RUBY_VERSION >= '1.9'
4
+ # Modify parsing methods to handle accountant date format correctly.
5
+ class << Date
6
+ # American date format detected by the library.
7
+ ACCOUNTANT_SLASH_DATE_RE = %r_\A\s*(\d{1,2})/(\d{4}|\d{2})_.freeze
8
+
9
+ # Alias for stdlib Date._parse
10
+ alias _parse_without_accountant_date _parse
11
+
12
+ # Transform accountant dates into ISO dates before parsing.
13
+ def _parse(string, comp=true)
14
+ _parse_without_accountant_date(convert_accountant_to_iso(string), comp)
15
+ end
16
+
17
+ if RUBY_VERSION >= '1.9.3'
18
+ # Alias for stdlib Date.parse
19
+ alias parse_without_accountant_date parse
20
+
21
+ # Transform accountant dates into ISO dates before parsing.
22
+ def parse(string, comp=true)
23
+ parse_without_accountant_date(convert_accountant_to_iso(string), comp)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ # Transform accountant date fromat into ISO format.
30
+ def convert_accountant_to_iso(string)
31
+ if string.count('/') == 1
32
+ string.sub(ACCOUNTANT_SLASH_DATE_RE){ |m| "#$2-#$1-1" }
33
+ elsif string.length <= 8 and string =~ /\d{#{string.length}}/
34
+ # If the whole string is digits
35
+ convert_date_no_slashes_to_iso(string)
36
+ else
37
+ string
38
+ end
39
+ end
40
+
41
+ # Transform date to have slashes
42
+ def convert_date_no_slashes_to_iso(string)
43
+ case string.length
44
+ when 8 # mmddyyyy
45
+ "#{string[4,4]}-#{string[0,2]}-#{string[2,2]}"
46
+ when 7 # mddyyyy
47
+ "#{string[4,4]}-#{string[0]}-#{string[1,2]}"
48
+ when 6 # mmddyy
49
+ "#{string[4,2]}-#{string[0,2]}-#{string[2,2]}"
50
+ when 5 # mddyy
51
+ "#{string[3,2]}-#{string[0]}-#{string[1,2]}"
52
+ when 4 # mmyy
53
+ "#{string[2,2]}-#{string[0,2]}-1"
54
+ when 3 # myy
55
+ "#{string[1,2]}-#{string[0]}-1"
56
+ else
57
+ string
58
+ end
59
+ end
60
+ end
61
+
62
+ if RUBY_VERSION >= '1.9.3'
63
+ # Modify parsing methods to handle accountant date format correctly.
64
+ class << DateTime
65
+ # Alias for stdlib Date.parse
66
+ alias parse_without_accountant_date parse
67
+
68
+ # Transform accountant dates into ISO dates before parsing.
69
+ def parse(string, comp=true)
70
+ parse_without_accountant_date(convert_accountant_to_iso(string), comp)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,143 @@
1
+ require 'accountant_date'
2
+ require 'time'
3
+
4
+ describe "Date.parse" do
5
+ it "should use accountant date format for mm/yy" do
6
+ Date.parse('02/03', true).should == Date.new(2003, 2, 1)
7
+ end
8
+
9
+ it "should use accountant date format for m/yy" do
10
+ Date.parse('2/03', true).should == Date.new(2003, 2, 1)
11
+ end
12
+
13
+ it "should use accountant date format for mmyy" do
14
+ Date.parse('0203', true).should == Date.new(2003, 2, 1)
15
+ end
16
+
17
+ it "should use accountant date format for myy" do
18
+ Date.parse('203', true).should == Date.new(2003, 2, 1)
19
+ end
20
+
21
+ it "should use accountant date format for mmddyyyy" do
22
+ Date.parse('01152012', true).should == Date.new(2012, 1, 15)
23
+ end
24
+
25
+ it "should use accountant date format for mmddyy" do
26
+ Date.parse('020503', true).should == Date.new(2003, 2, 5)
27
+ end
28
+
29
+ it "should use accountant date format for mddyy" do
30
+ Date.parse('20503', true).should == Date.new(2003, 2, 5)
31
+ end
32
+
33
+ it "should ignore preceding whitespace" do
34
+ Date.parse(' 02/2003').should == Date.new(2003, 2, 1)
35
+ end
36
+
37
+ it "should still process regularly formatted dates" do
38
+ Date.parse('1900-1-1', true).should == Date.new(1900, 1, 1)
39
+ end
40
+
41
+ it "should still process regularly formatted dates" do
42
+ Date.parse('1/1/1900', true).should == Date.new(1900, 1, 1)
43
+ end
44
+ end
45
+
46
+ describe "DateTime.parse" do
47
+ it "should use accountant date format for mm/yy" do
48
+ DateTime.parse('02/03', true).should == DateTime.new(2003, 2, 1)
49
+ end
50
+
51
+ it "should use accountant date format for m/yy" do
52
+ DateTime.parse('2/03', true).should == DateTime.new(2003, 2, 1)
53
+ end
54
+
55
+ it "should use accountant date format for mmyy" do
56
+ DateTime.parse('0203', true).should == DateTime.new(2003, 2, 1)
57
+ end
58
+
59
+ it "should use accountant date format for myy" do
60
+ DateTime.parse('203', true).should == DateTime.new(2003, 2, 1)
61
+ end
62
+
63
+ it "should use accountant date format for ddmmyy" do
64
+ DateTime.parse('020503', true).should == DateTime.new(2003, 2, 5)
65
+ end
66
+
67
+ it "should use accountant date format for dmmyy" do
68
+ DateTime.parse('020503', true).should == DateTime.new(2003, 2, 5)
69
+ end
70
+
71
+ specify "should ignore preceding whitespace" do
72
+ DateTime.parse(' 02/2003').should == DateTime.new(2003, 2, 1)
73
+ end
74
+ end
75
+
76
+ describe "Time.parse" do
77
+ it "should use accountant date format for mm/yy" do
78
+ Time.parse('02/03', true).should == Time.local(2003, 2, 1)
79
+ end
80
+
81
+ it "should use accountant date format for m/yy" do
82
+ Time.parse('2/03', true).should == Time.local(2003, 2, 1)
83
+ end
84
+
85
+ it "should use accountant date format for mmyy" do
86
+ Time.parse('0203', true).should == Time.local(2003, 2, 1)
87
+ end
88
+
89
+ it "should use accountant date format for myy" do
90
+ Time.parse('203', true).should == Time.local(2003, 2, 1)
91
+ end
92
+
93
+ it "should use accountant date format for ddmmyy" do
94
+ Time.parse('020503', true).should == Time.local(2003, 2, 5)
95
+ end
96
+
97
+ it "should use accountant date format for dmmyy" do
98
+ Time.parse('020503', true).should == Time.local(2003, 2, 5)
99
+ end
100
+
101
+ it "should ignore preceding whitespace" do
102
+ Time.parse(' 02/2003').should == Time.local(2003, 2, 1)
103
+ end
104
+
105
+ it "should work with times" do
106
+ Time.parse('02/2003 10:20:30').should == Time.local(2003, 2, 1, 10, 20, 30)
107
+ end
108
+ end
109
+
110
+ describe "Date._parse" do
111
+ specify "should use accountant date format for dd/mm/yy" do
112
+ Date._parse('02/03', true).should == {:year=>2003, :mon=>2, :mday=>1}
113
+ end
114
+
115
+ it "should use accountant date format for m/yy" do
116
+ Date._parse('2/03', true).should == {:year=>2003, :mon=>2, :mday=>1}
117
+ end
118
+
119
+ it "should use accountant date format for mmyy" do
120
+ Date._parse('0203', true).should == {:year=>2003, :mon=>2, :mday=>1}
121
+ end
122
+
123
+ it "should use accountant date format for myy" do
124
+ Date._parse('203', true).should == {:year=>2003, :mon=>2, :mday=>1}
125
+ end
126
+
127
+ it "should use accountant date format for ddmmyy" do
128
+ Date._parse('020503', true).should == {:year=>2003, :mon=>2, :mday=>5}
129
+ end
130
+
131
+ it "should use accountant date format for dmmyy" do
132
+ Date._parse('020503', true).should == {:year=>2003, :mon=>2, :mday=>5}
133
+ end
134
+
135
+ specify "should ignore preceding whitespace" do
136
+ Date._parse(' 02/2003').should == {:year=>2003, :mon=>2, :mday=>1}
137
+ end
138
+
139
+ specify "should work with times" do
140
+ DateTime._parse('02/2003 10:20:30').should ==
141
+ {:year=>2003, :mon=>2, :mday=>1, :hour=>10, :min=>20, :sec=>30}
142
+ end
143
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'accountant_date'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accountant_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Winograd
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &14624360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *14624360
25
+ - !ruby/object:Gem::Dependency
26
+ name: rdoc
27
+ requirement: &14639800 !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: *14639800
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &14638180 !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: *14638180
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &14635820 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *14635820
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &14634040 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *14634040
69
+ description: Accountants deals with dates that are simply months (e.g., 01/13). accountant_date
70
+ parses these mm/yy style dates and allows for other shorthands.
71
+ email: ryan@thewinograds.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE.txt
76
+ - README.rdoc
77
+ files:
78
+ - .document
79
+ - .rspec
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.rdoc
84
+ - Rakefile
85
+ - VERSION
86
+ - accountant_date.gemspec
87
+ - lib/accountant_date.rb
88
+ - spec/accountant_date_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: http://github.com/rylwin/accountant_date
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: -1519716211196290850
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.10
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Accounting-style date parsing
118
+ test_files: []