flexyear 0.0.1 → 0.0.2

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: 03f0180f05ab5d01948b0a24796cbfd9a7b23970
4
+ data.tar.gz: cfaa51b4ed1ebae6f300a629cd1d500b3a1ab7e2
5
+ SHA512:
6
+ metadata.gz: b48806ae6b6b7658b7b0893a56bcfbb3ee28dbec7991cc3bad224495bb66ec3a8fd57fafbffd6a1b92c482ad431fbae6e21a215a0c62451fdb87c40e38cad2e5
7
+ data.tar.gz: 82427042b5d194fdc6afe64fc615e638ff9a4c7252071f76bb6cb6b4b2dc9cfd5c66ddaa724962d614985432cc921cb599dbea10fbd680beae595a958b73bdd0
data/CHANGELOG ADDED
@@ -0,0 +1,5 @@
1
+ 0.0.2
2
+ - Refactor main parser code into parser subclasses (@tmaier)
3
+ - Remove ReissueParser, moved to Reverb codebase as it's not generic
4
+ 0.0.1
5
+ - Initial Release
data/README.md CHANGED
@@ -4,11 +4,13 @@ Flexible years! Parse decades, ranges, and etc into low & high values.
4
4
 
5
5
  Examples:
6
6
 
7
- FlexYear.new("1980s").year_low = 1980
8
- FlexYear.new("1980s").year_high == 1989
7
+ ```ruby
8
+ FlexYear.new("1980s").year_low == 1980
9
+ FlexYear.new("1980s").year_high == 1989
9
10
 
10
- FlexYear.new("mid-80s").year_low == 1983
11
- FlexYear.new("mid-80s").year_high == 1986
11
+ FlexYear.new("mid-80s").year_low == 1983
12
+ FlexYear.new("mid-80s").year_high == 1986
13
+ ```
12
14
 
13
15
  It's pretty flexible in the kinds of things it takes. For more examples, see the spec.
14
16
 
@@ -16,7 +18,9 @@ It's pretty flexible in the kinds of things it takes. For more examples, see the
16
18
 
17
19
  Add this line to your application's Gemfile:
18
20
 
19
- gem 'flexyear'
21
+ ```ruby
22
+ gem 'flexyear'
23
+ ```
20
24
 
21
25
  And then execute:
22
26
 
@@ -26,7 +30,6 @@ Or install it yourself as:
26
30
 
27
31
  $ gem install flexyear
28
32
 
29
-
30
33
  ## Contributing
31
34
 
32
35
  1. Fork it
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/flexyear.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'flexyear/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "flexyear"
8
- spec.version = Flexyear::VERSION
8
+ spec.version = FlexYear::VERSION
9
9
  spec.authors = ["Dan Melnick & Yan Pritzker"]
10
10
  spec.email = ["pair+dm+yp@reverb.com"]
11
11
  spec.description = %q{Parse common year range formats like '1973-75' or natural language ranges like 'mid 80s'}
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
23
24
  end
@@ -0,0 +1,13 @@
1
+ class FlexYear
2
+ class CircaParser < RangeParser
3
+ CIRCA_KEYWORDS = ["circ", "ca", "c.a.", "ca.", "cca", "c.", "approx", "appx", "about", "around"]
4
+
5
+ def self.can_parse?(string)
6
+ CIRCA_KEYWORDS.any?{|circa_pattern| string.downcase.include?(circa_pattern)}
7
+ end
8
+
9
+ def parse
10
+ [-1, 1]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ class FlexYear
2
+ class DecadeParser < RangeParser
3
+ def self.can_parse?(string)
4
+ string.end_with?('s')
5
+ end
6
+
7
+ def parse
8
+ [0, 9]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class FlexYear
2
+ class EarlyParser < RangeParser
3
+ def self.can_parse?(string)
4
+ string.start_with?('early')
5
+ end
6
+
7
+ def parse
8
+ [0, 3]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class FlexYear
2
+ class LateParser < RangeParser
3
+ def self.can_parse?(string)
4
+ string.start_with?('late')
5
+ end
6
+
7
+ def parse
8
+ [6, 9]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class FlexYear
2
+ class MidParser < RangeParser
3
+ def self.can_parse?(string)
4
+ string.start_with?('mid')
5
+ end
6
+
7
+ def parse
8
+ [3, 6]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ class FlexYear
2
+ class RangeParser
3
+ class << self
4
+ attr_reader :parser_classes
5
+ end
6
+
7
+ @parser_classes = []
8
+
9
+ def self.parse(string)
10
+ parser = find_parser(string)
11
+ return nil unless parser
12
+ parser.parse
13
+ end
14
+
15
+ def self.find_parser(string)
16
+ parser_class = RangeParser.parser_classes.find do |klass|
17
+ klass.can_parse?(string)
18
+ end
19
+ return parser_class.new(string) if parser_class
20
+ nil
21
+ end
22
+
23
+ def self.inherited(subclass)
24
+ RangeParser.parser_classes << subclass
25
+ end
26
+
27
+ def initialize(string)
28
+ @string = string
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ class FlexYear
2
+ class SpecificYearParser < RangeParser
3
+ def self.can_parse?(string)
4
+ string =~ /\A\d+\z/
5
+ end
6
+
7
+ def parse
8
+ [0, 0]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class FlexYear
2
+ class YearRangeParser < RangeParser
3
+ def self.can_parse?(string)
4
+ string.include?('-') && string =~ /(\d+)\s*-\s*(\d+)/
5
+ end
6
+
7
+ # Nothing to do here, see FlexYear#parse_year
8
+ def parse; end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
- module Flexyear
2
- VERSION = "0.0.1"
1
+ class FlexYear
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/flexyear.rb CHANGED
@@ -1,3 +1,11 @@
1
+ require 'flexyear/range_parsers/range_parser'
2
+ require 'flexyear/range_parsers/early_parser'
3
+ require 'flexyear/range_parsers/mid_parser'
4
+ require 'flexyear/range_parsers/late_parser'
5
+ require 'flexyear/range_parsers/decade_parser'
6
+ require 'flexyear/range_parsers/year_range_parser'
7
+ require 'flexyear/range_parsers/circa_parser'
8
+ require 'flexyear/range_parsers/specific_year_parser'
1
9
  require 'flexyear/version'
2
10
  require 'date'
3
11
 
@@ -14,39 +22,10 @@ class FlexYear
14
22
 
15
23
  attr_reader :year_low, :year_high
16
24
 
17
- CIRCA_KEYWORDS = ["circ", "ca", "c.a.", "ca.", "cca", "c.", "approx", "appx", "about", "around"]
18
-
19
25
  def initialize(year_string)
20
26
  @year_string = year_string.to_s
21
27
 
22
- if @year_string.start_with?("early")
23
- @low = 0
24
- @high = 3
25
- elsif @year_string.start_with?("mid")
26
- @low = 3
27
- @high = 6
28
- elsif @year_string.start_with?("late")
29
- @low = 6
30
- @high = 9
31
- elsif @year_string.end_with?("s") # decade
32
- @low = 0
33
- @high = 9
34
- elsif @year_string.include?("-")
35
- @year_string =~ /(\d+)\s*-\s*(\d+)/
36
- if $1 && $2
37
- @year_low = centuryize($1).to_i
38
- @year_high = centuryize($2, @year_low).to_i
39
- return
40
- end
41
- elsif CIRCA_KEYWORDS.any?{|circa_pattern| @year_string.downcase.include?(circa_pattern)}
42
- @low = -1
43
- @high = 1
44
- elsif @year_string.downcase.include?('reissue')
45
- raise InvalidYearError, 'Please enter the date of manufacture, not the reissue era.'
46
- else # specific year
47
- @low = 0
48
- @high = 0
49
- end
28
+ @low, @high = RangeParser.parse(@year_string)
50
29
 
51
30
  parse_year
52
31
  end
@@ -73,20 +52,25 @@ class FlexYear
73
52
  end
74
53
 
75
54
  def parse_year
76
- if @year_string =~ /(\d+).*s$/
77
- @base_year = centuryize($1).to_i
78
- elsif @year_string =~ /^\w+\s+(\d+)/
79
- @base_year = centuryize($1).to_i
55
+ if @year_string =~ /(\d+)\s*-\s*(\d+)/ && $1 && $2
56
+ @year_low = centuryize($1).to_i
57
+ @year_low, @year_high = [@year_low, centuryize($2, @year_low).to_i].sort
80
58
  else
81
- @base_year = @year_string.gsub(/\D+/,'').to_i
82
- end
59
+ if @year_string =~ /(\d+).*s$/
60
+ @base_year = centuryize($1).to_i
61
+ elsif @year_string =~ /^\w+\s+(\d+)/
62
+ @base_year = centuryize($1).to_i
63
+ else
64
+ @base_year = @year_string.gsub(/\D+/,'').to_i
65
+ end
83
66
 
84
- if @base_year > 9999
85
- raise InvalidYearError, "Please use a four digit year."
86
- end
67
+ if @base_year > 9999
68
+ raise InvalidYearError, "Please use a four digit year."
69
+ end
87
70
 
88
- @year_low = @base_year + (@low || -1)
89
- @year_high = @base_year + (@high || 1)
71
+ @year_low = @base_year + (@low || 0)
72
+ @year_high = @base_year + (@high || 0)
73
+ end
90
74
  end
91
75
 
92
76
  # Represents a flexible year entry that must be in the past.
@@ -82,6 +82,12 @@ describe FlexYear do
82
82
  end
83
83
  end
84
84
 
85
+ context "given a range 1975-1973 (from high to low)" do
86
+ subject { FlexYear.new('1975-1973') }
87
+ its(:year_low) { should eq(1973) }
88
+ its(:year_high) { should eq(1975) }
89
+ end
90
+
85
91
  context "given a range" do
86
92
  ["2003-4", "2003-04"].each do |range|
87
93
  subject { FlexYear.new(range) }
@@ -90,12 +96,6 @@ describe FlexYear do
90
96
  end
91
97
  end
92
98
 
93
- context 'given a reissue' do
94
- specify do
95
- expect { FlexYear.new('65 reissue') }.to raise_error(FlexYear::InvalidYearError)
96
- end
97
- end
98
-
99
99
  context 'given a circa' do
100
100
  context 'at the end of the string' do
101
101
  subject { FlexYear.new('1973 (Circa)') }
metadata CHANGED
@@ -1,95 +1,107 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: flexyear
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 1
9
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
10
5
  platform: ruby
11
- authors:
6
+ authors:
12
7
  - Dan Melnick & Yan Pritzker
13
8
  autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
-
17
- date: 2013-06-13 00:00:00 -05:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2013-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: bundler
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
25
17
  - - ~>
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 1
29
- - 3
30
- version: "1.3"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
31
20
  type: :development
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
34
28
  name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
35
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- segments:
41
- - 0
42
- version: "0"
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: '0'
43
48
  type: :development
44
- version_requirements: *id002
45
- description: Parse common year range formats like '1973-75' or natural language ranges like 'mid 80s'
46
- email:
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Parse common year range formats like '1973-75' or natural language ranges
56
+ like 'mid 80s'
57
+ email:
47
58
  - pair+dm+yp@reverb.com
48
59
  executables: []
49
-
50
60
  extensions: []
51
-
52
61
  extra_rdoc_files: []
53
-
54
- files:
62
+ files:
55
63
  - .gitignore
64
+ - CHANGELOG
56
65
  - Gemfile
57
66
  - LICENSE.txt
58
67
  - README.md
59
68
  - Rakefile
60
69
  - flexyear.gemspec
61
70
  - lib/flexyear.rb
71
+ - lib/flexyear/range_parsers/circa_parser.rb
72
+ - lib/flexyear/range_parsers/decade_parser.rb
73
+ - lib/flexyear/range_parsers/early_parser.rb
74
+ - lib/flexyear/range_parsers/late_parser.rb
75
+ - lib/flexyear/range_parsers/mid_parser.rb
76
+ - lib/flexyear/range_parsers/range_parser.rb
77
+ - lib/flexyear/range_parsers/specific_year_parser.rb
78
+ - lib/flexyear/range_parsers/year_range_parser.rb
62
79
  - lib/flexyear/version.rb
63
80
  - spec/flexyear_spec.rb
64
- has_rdoc: true
65
81
  homepage: http://github.com/reverbdev/flexyear
66
- licenses:
82
+ licenses:
67
83
  - MIT
84
+ metadata: {}
68
85
  post_install_message:
69
86
  rdoc_options: []
70
-
71
- require_paths:
87
+ require_paths:
72
88
  - lib
73
- required_ruby_version: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- segments:
78
- - 0
79
- version: "0"
80
- required_rubygems_version: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- segments:
85
- - 0
86
- version: "0"
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
87
99
  requirements: []
88
-
89
100
  rubyforge_project:
90
- rubygems_version: 1.3.6
101
+ rubygems_version: 2.0.0
91
102
  signing_key:
92
- specification_version: 3
103
+ specification_version: 4
93
104
  summary: Natural language year range parser
94
- test_files:
105
+ test_files:
95
106
  - spec/flexyear_spec.rb
107
+ has_rdoc: