fuzzy-date 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c76cd668fdf666c2a3c389eb6654ede8a67ce78c
4
- data.tar.gz: 384f9a86a76d423497de05af75952acc6226b150
3
+ metadata.gz: 3566a7b503dd739c3eaee9cfc6cdbfe4a8a0e3bf
4
+ data.tar.gz: f3f2ddbe5c6d38efad627d8e293843267f7f2c05
5
5
  SHA512:
6
- metadata.gz: f177d19cd1e39645ec975a12035db464f886b350c35bcbb89d1c7f2bb47a68cad4894692dd1a4679e469556a216013383c9257e1ea114c1189bf30f8dfd636b2
7
- data.tar.gz: 94b41c11f31e266a3123786a0983dcd0c0883e45bde478a612b92207c001a5d499617b199af0b1a7a8d10d96d15005d8c8bdd2a02faf99021915cb62c4ac0977
6
+ metadata.gz: 54bef59ade20151fc60bab750d1153d815753b40101fab78ac212a8172cfa80da737265e9456d83bd9edf78053a6dec1899e96ec3a1461add276fc512fed4506
7
+ data.tar.gz: e95bf44e9ce6d7e12f59e561ff9c8c7565ee2498252ac94751a0e4614498510dd2e6ff1109237b773c54aba0aeec6992084e35f877e7bb4010eeada2599366a6
data/README.md CHANGED
@@ -4,12 +4,31 @@ The fuzzy-date gem provides a way to parse and use incomplete dates, like those
4
4
 
5
5
  For example, if you know your great-great-great grandmother was born in April, 1836, but you don't know the day of the month, that's not going to work if you try to parse it as a Date object.
6
6
 
7
- With fuzzy-date, when you parse an incomplete date, you'll be given a hash of information about the date that can be stored as a string.
7
+ With fuzzy-date, when you parse an incomplete date, you'll be given an object of information about the date.
8
8
 
9
9
  ## Installation
10
10
 
11
11
  `gem install fuzzy-date`
12
12
 
13
+ ## Usage:
14
+
15
+ require 'rubygems'
16
+ require 'fuzzy-date'
17
+
18
+ fuzzy_date = FuzzyDate::parse( '15 March 1971' )
19
+
20
+ puts "PARSING: #{ fuzzy_date.original }"
21
+
22
+ puts "Short date: #{ fuzzy_date.short }"
23
+ puts "Long date: #{ fuzzy_date.long }"
24
+ puts "Full date: #{ fuzzy_date.full }"
25
+ puts "Year: #{ fuzzy_date.year }"
26
+ puts "Month: #{ fuzzy_date.month }"
27
+ puts "Day: #{ fuzzy_date.day }"
28
+ puts "Month name: #{ fuzzy_date.month_name }"
29
+ puts "Circa: #{ fuzzy_date.circa }"
30
+ puts "Era: #{ fuzzy_date.era }"
31
+
13
32
  ## Contributing
14
33
 
15
34
  If you'd like to hack on FuzzyDate - and we hope you do! - start by forking the repo on GitHub:
data/demo.rb CHANGED
@@ -1,5 +1,5 @@
1
- require( 'rubygems' )
2
- require( 'fuzzy-date' )
1
+ require 'rubygems'
2
+ require 'fuzzy-date'
3
3
 
4
4
  fuzzy_date = FuzzyDate::parse( '15 March 1971' )
5
5
 
@@ -3,7 +3,7 @@ require 'fuzzy-date/variables'
3
3
  require 'fuzzy-date/fuzzy-date'
4
4
 
5
5
  class FuzzyDate
6
- VERSION = '0.2.0'
6
+ VERSION = '0.2.1'
7
7
 
8
8
  def self.parse( date, euro = false )
9
9
  FuzzyDate.new date, euro
@@ -17,6 +17,7 @@ class FuzzyDate
17
17
  # * DD-MMM-YYYY - 1 or 2 digit day, then month name or abbreviation, then 1 to 4 digit year
18
18
  # * MMM-YYYY - month name or abbreviation, then 1 to 4 digit year
19
19
  # * MMM-DD-YYYY - month name or abbreviation, then 1 or 2 digit day, then 1 to 4 digit year
20
+ # * YYYY-MMM - 1 to 4 digit year, then month name or abbreviation
20
21
  #
21
22
  # Notes:
22
23
  # - Commas are optional.
@@ -30,115 +31,107 @@ class FuzzyDate
30
31
 
31
32
  date = clean_parameter date
32
33
 
33
- @date_parts[ :original ] = date
34
+ @original = date
34
35
 
35
36
  date = massage date
36
- @date_parts[ :fixed ] = date
37
+ @fixed = date
37
38
 
38
- year, month, day = nil
39
+ @year, @month, @day = nil
39
40
 
40
41
  if date =~ @date_patterns[ :yyyy ]
41
- year = stringify $1
42
+ @year = $1.to_i
42
43
 
43
44
  elsif date =~ @date_patterns[ :yyyy_mm_dd_and_yyyy_mm ]
44
- year = stringify $1
45
- month = stringify $2 unless $2.nil?
46
- day = stringify $3 unless $3.nil?
45
+ @year = $1.to_i
46
+ @month = $2.to_i unless $2.nil?
47
+ @day = $3.to_i unless $3.nil?
47
48
 
48
49
  elsif date =~ @date_patterns[ :dd_mm_yyyy ] and euro
49
- day = stringify $1
50
- month = stringify $2
51
- year = stringify $3
50
+ @day = $1.to_i
51
+ @month = $2.to_i
52
+ @year = $3.to_i
52
53
 
53
54
  elsif date =~ @date_patterns[ :mm_dd_yyyy ]
54
- month = stringify $1
55
- day = stringify $2
56
- year = stringify $3
55
+ @month = $1.to_i
56
+ @day = $2.to_i
57
+ @year = $3.to_i
57
58
 
58
59
  elsif date =~ @date_patterns[ :mm_yyyy ]
59
- month = stringify $1
60
- year = stringify $2
60
+ @month = $1.to_i
61
+ @year = $2.to_i
61
62
 
62
63
  elsif date =~ @date_patterns[ :dd_mmm_yyyy_and_dd_mmm ]
63
64
  month_text = $2.to_s.capitalize
64
- month = stringify @month_names.key( @month_abbreviations[ month_text ] )
65
- day = stringify $1
66
- year = stringify $3 unless $3.nil?
65
+ @month = @month_names.key( @month_abbreviations[ month_text ] )
66
+ @day = $1.to_i
67
+ @year = $3.to_i unless $3.nil?
67
68
 
68
69
  elsif date =~ @date_patterns[ :mmm_dd_yyyy ]
69
70
  month_text = $1.to_s.capitalize
70
- month = stringify @month_names.key( @month_abbreviations[ month_text ] )
71
- day = stringify $2
72
- year = stringify $3 unless $3.nil?
71
+ @month = @month_names.key( @month_abbreviations[ month_text ] )
72
+ @day = $2.to_i
73
+ @year = $3.to_i unless $3.nil?
73
74
 
74
75
  elsif date =~ @date_patterns[ :mmm_yyyy_and_mmm ]
75
76
  month_text = $1.to_s.capitalize
76
- month = stringify @month_names.key( @month_abbreviations[ month_text ] )
77
- year = stringify $2 unless $2.nil?
77
+ @month = @month_names.key( @month_abbreviations[ month_text ] )
78
+ @year = $2.to_i unless $2.nil?
79
+
80
+ elsif date =~ @date_patterns[ :yyyy_mmm_dd ]
81
+ @year = $1.to_i unless $1.nil?
82
+ month_text = $2.to_s.capitalize
83
+ @month = @month_names.key( @month_abbreviations[ month_text ] )
84
+ @day = $3.to_i
85
+
86
+ elsif date =~ @date_patterns[ :yyyy_mmm ]
87
+ @year = $1.to_i unless $1.nil?
88
+ month_text = $2.to_s.capitalize
89
+ @month = @month_names.key( @month_abbreviations[ month_text ] )
78
90
 
79
91
  else
80
92
  raise ArgumentError.new( 'Cannot parse date.' )
81
93
  end
82
94
 
83
- @date_parts[ :year ] = year ? year.to_i : nil
84
- @date_parts[ :month ] = month ? month.to_i : nil
85
- @date_parts[ :day ] = day ? day.to_i : nil
86
-
87
- #- Some error checking at this point
88
- if month.to_i > 13
95
+ #- Make sure the dates make sense
96
+ if @month and @month > 13
89
97
  raise ArgumentError.new( 'Month cannot be greater than 12.' )
90
- elsif month and day and day.to_i > @days_in_month[ month.to_i ]
91
- unless month.to_i == 2 and year and Date.parse( '1/1/' + year ).leap? and day.to_i == 29
98
+ elsif @month and @day and @day > @days_in_month[ @month ]
99
+ unless @month == 2 and @year and Date.parse( '1/1/' + @year ).leap? and @day == 29
92
100
  raise ArgumentError.new( 'Too many days in this month.' )
93
101
  end
94
- elsif month and month.to_i < 1
102
+ elsif @month and @month < 1
95
103
  raise ArgumentError.new( 'Month cannot be less than 1.' )
96
- elsif day and day.to_i < 1
104
+ elsif @day and @day < 1
97
105
  raise ArgumentError.new( 'Day cannot be less than 1.' )
98
106
  end
99
107
 
100
- month_name = @month_names[ month.to_i ]
101
- @date_parts[ :month_name ] = month_name
108
+ @month_name = @month_names[ @month ]
102
109
 
103
110
  # ----------------------------------------------------------------------
104
111
 
105
- show_era = @date_parts[ :era ] == 'BC' ? ' ' + @date_parts[ :era ] : ''
106
- show_circa = @date_parts[ :circa ] == true ? 'About ' : ''
107
-
108
- if year and month and day
109
- @date_parts[ :short ] = show_circa + month + '/' + day + '/' + year + show_era
110
- @date_parts[ :long ] = show_circa + month_name + ' ' + day + ', ' + year + show_era
111
- modified_long = show_circa + month_name + ' ' + day + ', ' + year.rjust( 4, "0" ) + show_era
112
- @date_parts[ :full ] = show_circa + Date.parse( modified_long ).strftime( '%A,' ) + Date.parse( day + ' ' + month_name + ' ' + year.rjust( 4, "0" ) ).strftime( ' %B %-1d, %Y' ) + show_era
113
- elsif year and month
114
- @date_parts[ :short ] = show_circa + month + '/' + year + show_era
115
- @date_parts[ :long ] = show_circa + month_name + ', ' + year + show_era
116
- @date_parts[ :full ] = @date_parts[ :long ]
117
- elsif month and day
118
- month_text = @month_abbreviations.key(month_text) || month_text
119
- @date_parts[ :short ] = show_circa + day + '-' + month_text
120
- @date_parts[ :long ] = show_circa + day + ' ' + month_name
121
- @date_parts[ :full ] = @date_parts[ :long ]
112
+ show_era = @eras[@era] == :bce ? ' ' + @era : ''
113
+ show_circa = @circa ? 'About ' : ''
114
+
115
+ if @year and @month and @day
116
+ @short = show_circa + @month.to_s + '/' + @day.to_s + '/' + @year.to_s + show_era
117
+ @long = show_circa + @month_name + ' ' + @day.to_s + ', ' + @year.to_s + show_era
118
+ modified_long = show_circa + @month_name + ' ' + @day.to_s + ', ' + @year.to_s.rjust( 4, "0" ) + show_era
119
+ @full = show_circa + Date.parse( modified_long ).strftime( '%A,' ) + Date.parse( @day.to_s + ' ' + @month_name + ' ' + @year.to_s.rjust( 4, "0" ) ).strftime( ' %B %-1d, %Y' ) + show_era
120
+ elsif @year and @month
121
+ @short = show_circa + @month.to_s + '/' + @year.to_s + show_era
122
+ @long = show_circa + @month_name + ', ' + @year.to_s + show_era
123
+ @full = @long
124
+ elsif @month and @day
125
+ month_text = @month_abbreviations.key( month_text ) || month_text
126
+ @short = show_circa + @day.to_s + '-' + month_text
127
+ @long = show_circa + @day.to_s + ' ' + @month_name
128
+ @full = @long
122
129
  elsif year
123
- @date_parts[ :short ] = show_circa + year + show_era
124
- @date_parts[ :long ] = @date_parts[ :short ]
125
- @date_parts[ :full ] = @date_parts[ :long ]
130
+ @short = show_circa + @year.to_s + show_era
131
+ @long = @short
132
+ @full = @long
126
133
  end
127
134
 
128
- @short = @date_parts[ :short ]
129
- @long = @date_parts[ :long ]
130
- @full = @date_parts[ :full ]
131
- @original = @date_parts[ :original ]
132
- @fixed = @date_parts[ :fixed ]
133
- @year = @date_parts[ :year ]
134
- @month = @date_parts[ :month ]
135
- @day = @date_parts[ :day ]
136
- @month_name = @date_parts[ :month_name ]
137
- @circa = @date_parts[ :circa ]
138
- @era = @date_parts[ :era ]
139
-
140
- @date_parts
141
-
142
135
  end
143
136
 
144
137
  def clean_parameter( date )
@@ -156,18 +149,14 @@ class FuzzyDate
156
149
  date_in_parts = date.split date_separator
157
150
  date_in_parts.delete_if { |d| d.to_s.empty? }
158
151
  if date_in_parts.first.match Regexp.new( @circa_words.join( '|' ), true )
159
- @date_parts[ :circa ] = true
152
+ @circa = true
160
153
  date_in_parts.shift
161
154
  end
162
- if date_in_parts.last.match Regexp.new( @era_words.join( '|' ), true )
163
- @date_parts[ :era ] = date_in_parts.pop.upcase.strip
155
+ if date_in_parts.last.match Regexp.new( @eras.keys.join( '|' ), true )
156
+ @era = date_in_parts.pop.upcase.strip
164
157
  end
165
158
 
166
159
  date_in_parts.join '-'
167
160
  end
168
161
 
169
- def stringify( capture )
170
- capture.to_i.to_s
171
- end
172
-
173
- end
162
+ end
@@ -6,7 +6,19 @@ class FuzzyDate
6
6
  end
7
7
 
8
8
  def to_hash
9
- @date_parts
9
+ {
10
+ circa: @circa,
11
+ day: @day,
12
+ era: @era,
13
+ fixed: @fixed,
14
+ full: @full,
15
+ long: @long,
16
+ month: @month,
17
+ month_name: @month_name,
18
+ original: @original,
19
+ short: @short,
20
+ year: @year
21
+ }
10
22
  end
11
23
 
12
24
  end
@@ -5,7 +5,7 @@ class FuzzyDate
5
5
 
6
6
  def setup
7
7
 
8
- @date_parts = set_up_date_parts
8
+ set_up_date_parts
9
9
 
10
10
  @month_names = {
11
11
  1 => 'January',
@@ -73,15 +73,17 @@ class FuzzyDate
73
73
  '~'
74
74
  ]
75
75
 
76
- @era_words = [
77
- 'AD',
78
- 'BC',
79
- 'CE',
80
- 'BCE'
81
- ]
82
-
76
+ @eras = {
77
+ 'AD' => :ce,
78
+ 'BC' => :bce,
79
+ 'CE' => :ce,
80
+ 'BCE' => :bce
81
+ }
82
+
83
83
  @date_patterns = {
84
84
  yyyy: /^(\d{1,4})$/,
85
+ yyyy_mmm: /^(\d{1,4})-(#{ @month_abbreviations.keys.join( '|' ) }).*?$/i,
86
+ yyyy_mmm_dd: /^(\d{1,4})-(#{ @month_abbreviations.keys.join( '|' ) }).*?-(\d{1,2})$/i,
85
87
  yyyy_mm_dd_and_yyyy_mm: /^(\d{3,4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?$/,
86
88
  dd_mm_yyyy: /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/,
87
89
  mm_dd_yyyy: /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/,
@@ -89,18 +91,16 @@ class FuzzyDate
89
91
  dd_mmm_yyyy_and_dd_mmm: /^(\d{1,2})(?:-(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?)?$/i,
90
92
  mmm_dd_yyyy: /^(#{ @month_abbreviations.keys.join( '|' ) }).*?-(\d{1,2})-(\d{1,4})$/i,
91
93
  mmm_yyyy_and_mmm: /^(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?$/i
92
- }
94
+ }
93
95
 
94
96
  end
95
97
 
96
98
  def set_up_date_parts
97
- @date_parts = {}
98
- @date_parts[ :original ] = nil
99
- @date_parts[ :circa ] = false
100
- @date_parts[ :year ] = nil
101
- @date_parts[ :month ] = nil
102
- @date_parts[ :day ] = nil
103
- @date_parts[ :era ] = 'AD'
104
- @date_parts
99
+ @original = nil
100
+ @circa = false
101
+ @year = nil
102
+ @month = nil
103
+ @day = nil
104
+ @era = 'AD'
105
105
  end
106
- end
106
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fuzzy-date
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cole
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-25 00:00:00.000000000 Z
11
+ date: 2015-06-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: The fuzzy-date gem provides a way to parse and use incomplete dates,
14
14
  like those found in history or genealogy.
@@ -45,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  version: '0'
46
46
  requirements: []
47
47
  rubyforge_project:
48
- rubygems_version: 2.0.3
48
+ rubygems_version: 2.0.14
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: Provides a way to use partial and incomplete dates.