range_sentence_parser 0.0.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.
@@ -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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in range_sentence_parser.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gabriel Sobrinho
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.
@@ -0,0 +1,58 @@
1
+ # RangeSentenceParser
2
+
3
+ Parser for range sentences like '1990; 1995-2000; 2005'.
4
+
5
+ I use it primarily for reports but can be used for anything else.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'range_sentence_parser'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install range_sentence_parser
20
+
21
+ ## Usage
22
+
23
+ Stand-alone:
24
+
25
+ RangeSentenceParser.parse!('1990; 1995-2000; 2005')
26
+ # => [1990, 1995..2000, 2005]
27
+
28
+ RangeSentenceParser.valid?('1990; 1995-2000; 2005')
29
+ # => true
30
+
31
+ RangeSentenceParser.invalid?('1990; 1995-2000; 2005')
32
+ # => false
33
+
34
+ Integrated with ActiveRecord:
35
+
36
+ class AccountingReport < ActiveRecord::Base
37
+ validates :year_sentence, :range_sentence => true
38
+
39
+ def years
40
+ RangeSentenceParser.parse!(year_sentence)
41
+ end
42
+ end
43
+
44
+ accounting_report = AccountingReport.new(:year_sentence => '1990; 1995-2000; 2005')
45
+
46
+ accounting_report.valid?
47
+ # => true
48
+
49
+ accounting_report.years
50
+ # => [1990, 1995..2000, 2005]
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,65 @@
1
+ class RangeSentenceParser
2
+ # Parse the sentence and return a collection of numbers and/or ranges
3
+ #
4
+ # Usage:
5
+ #
6
+ # RangeSentenceParser.parse!('') # => []
7
+ # RangeSentenceParser.parse!('1990') # => [1990]
8
+ # RangeSentenceParser.parse!('1990; 1995') # => [1990, 1995]
9
+ # RangeSentenceParser.parse!('1990-1995') # => [1990..1995]
10
+ # RangeSentenceParser.parse!('1990; 1995-2000; 2005') # => [1990, 1995..2000, 2005]
11
+ def self.parse!(sentence)
12
+ range_sentence_parser = self.new(sentence)
13
+ range_sentence_parser.parse!
14
+ end
15
+
16
+ # Validate the sentence and return true or false
17
+ #
18
+ # Usage:
19
+ #
20
+ # RangeSentenceParser.valid?('1990; 1995-2000; 2005') # => true
21
+ # RangeSentenceParser.valid?('1990, 1995-2000, 2005') # => false
22
+ def self.valid?(sentence)
23
+ range_sentence_parser = self.new(sentence)
24
+ range_sentence_parser.valid?
25
+ end
26
+
27
+ # Performs the opposite of valid?
28
+ #
29
+ # Usage:
30
+ #
31
+ # RangeSentenceParser.invalid?('1990; 1995-2000; 2005') # => false
32
+ # RangeSentenceParser.invalid?('1990, 1995-2000, 2005') # => true
33
+ def self.invalid?(sentence)
34
+ range_sentence_parser = self.new(sentence)
35
+ range_sentence_parser.invalid?
36
+ end
37
+
38
+ def initialize(sentence)
39
+ self.sentence = sentence
40
+ end
41
+
42
+ def parse!
43
+ raise InvalidSentenceError if invalid?
44
+
45
+ sentence.split(';').map do |number|
46
+ if number =~ /(\d+)-(\d+)/
47
+ Range.new($1.to_i, $2.to_i)
48
+ else
49
+ number.to_i
50
+ end
51
+ end
52
+ end
53
+
54
+ def valid?
55
+ sentence.empty? || sentence =~ /^\d+(-\d+)?(\s*;\s*\d+(-\d+)?)*;?$/
56
+ end
57
+
58
+ def invalid?
59
+ !valid?
60
+ end
61
+
62
+ private
63
+
64
+ attr_accessor :sentence
65
+ end
@@ -0,0 +1,4 @@
1
+ class RangeSentenceParser
2
+ class InvalidSentenceError < ArgumentError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module RangeSentenceParser
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ class RangeSentenceValidator < ActiveModel::EachValidator
2
+ # Validates whether the value of the specified attribute is of the correct form for RangeSentenceParser.
3
+ #
4
+ # Usage:
5
+ #
6
+ # class AccountingReport
7
+ # include ActiveModel::Validations
8
+ #
9
+ # validates :year_sentence, :range_sentence => true
10
+ # end
11
+ def validate_each(record, attribute, value)
12
+ record.errors.add(attribute) if RangeSentenceParser.invalid?(value)
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/range_sentence_parser/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Gabriel Sobrinho"]
6
+ gem.email = ["gabriel.sobrinho@gmail.com"]
7
+ gem.description = %q{Parser for range sentences like '1990; 1995-2000; 2005'}
8
+ gem.summary = %q{Parser for range sentences like '1990; 1995-2000; 2005'}
9
+ gem.homepage = "https://github.com/sobrinho/range_sentence_parser"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "range_sentence_parser"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RangeSentenceParser::VERSION
17
+
18
+ gem.add_development_dependency 'activemodel', '>= 3.0'
19
+ gem.add_development_dependency 'rspec', '>= 2.9'
20
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe RangeSentenceParser do
4
+ context '.parse!' do
5
+ it 'parses empty string' do
6
+ ''.should be_parsed_to []
7
+ end
8
+
9
+ it 'parses one number' do
10
+ '1990'.should be_parsed_to [1990]
11
+ end
12
+
13
+ it 'parses many numbers' do
14
+ '1990; 1995'.should be_parsed_to [1990, 1995]
15
+ end
16
+
17
+ it 'parses one range' do
18
+ '1990-1995'.should be_parsed_to [1990..1995]
19
+ end
20
+
21
+ it 'parses many ranges' do
22
+ '1990-1995; 2000-2005'.should be_parsed_to [1990..1995, 2000..2005]
23
+ end
24
+
25
+ it 'parses one number and one range' do
26
+ '1990; 1995-2000'.should be_parsed_to [1990, 1995..2000]
27
+ end
28
+
29
+ it 'parses many numbers and many ranges' do
30
+ '1990; 1995-2000; 2005; 2010-2015'.should be_parsed_to [1990, 1995..2000, 2005, 2010..2015]
31
+ end
32
+
33
+ it 'do not cares about inner spaces' do
34
+ '1990; 1995-2000; 2005; 2010-2015'.should be_parsed_to [1990, 1995..2000, 2005, 2010..2015]
35
+ '1990;1995-2000;2005;2010-2015'.should be_parsed_to [1990, 1995..2000, 2005, 2010..2015]
36
+ end
37
+
38
+ it 'do not cares about last semicolon' do
39
+ '1990;'.should be_parsed_to [1990]
40
+ '1990; 1995-2000; 2005; 2010-2015;'.should be_parsed_to [1990, 1995..2000, 2005, 2010..2015]
41
+ end
42
+
43
+ it 'raises an error for invalid sentences' do
44
+ expect { described_class.parse!('1990..1995') }.to raise_error RangeSentenceParser::InvalidSentenceError
45
+ expect { described_class.parse!('1990, 1995, 2000') }.to raise_error RangeSentenceParser::InvalidSentenceError
46
+ end
47
+ end
48
+
49
+ context '#valid?' do
50
+ it 'should be for valid sentences' do
51
+ ''.should be_a_valid_sentence
52
+ '1990'.should be_a_valid_sentence
53
+ '1990; 1995'.should be_a_valid_sentence
54
+ '1990-1995'.should be_a_valid_sentence
55
+ '1990-1995; 2000-2005'.should be_a_valid_sentence
56
+ '1990; 1995-2000; 2005; 2010-2015'.should be_a_valid_sentence
57
+ end
58
+ end
59
+
60
+ context '#invalid?' do
61
+ it 'should be for invalid sentences' do
62
+ '1990..1995'.should be_an_invalid_sentence
63
+ '1990, 1995, 2000'.should be_an_invalid_sentence
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe RangeSentenceValidator do
4
+ subject do
5
+ AccountingReport.new
6
+ end
7
+
8
+ context 'using valid sentence' do
9
+ before do
10
+ subject.stub(:year_sentence).and_return('1990; 1995-2000; 2005')
11
+ end
12
+
13
+ it { should be_valid }
14
+ end
15
+
16
+ context 'using invalid sentence' do
17
+ before do
18
+ subject.stub(:year_sentence).and_return('1990, 1995-2000, 2005')
19
+ end
20
+
21
+ it { should be_invalid }
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_model'
2
+
3
+ require 'range_sentence_parser'
4
+ require 'range_sentence_parser/invalid_sentence_error'
5
+ require 'range_sentence_validator'
6
+
7
+ require 'support/matchers'
8
+ require 'support/accounting_report'
@@ -0,0 +1,5 @@
1
+ class AccountingReport
2
+ include ActiveModel::Validations
3
+
4
+ validates :year_sentence, :range_sentence => true
5
+ end
@@ -0,0 +1,17 @@
1
+ RSpec::Matchers.define :be_parsed_to do |expected|
2
+ match do |actual|
3
+ RangeSentenceParser.parse!(actual) == expected
4
+ end
5
+ end
6
+
7
+ RSpec::Matchers.define :be_a_valid_sentence do |expected|
8
+ match do |actual|
9
+ RangeSentenceParser.valid?(actual)
10
+ end
11
+ end
12
+
13
+ RSpec::Matchers.define :be_an_invalid_sentence do |expected|
14
+ match do |actual|
15
+ RangeSentenceParser.invalid?(actual)
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: range_sentence_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gabriel Sobrinho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &70159654975460 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70159654975460
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70159654974520 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '2.9'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70159654974520
36
+ description: Parser for range sentences like '1990; 1995-2000; 2005'
37
+ email:
38
+ - gabriel.sobrinho@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rspec
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - lib/range_sentence_parser.rb
50
+ - lib/range_sentence_parser/invalid_sentence_error.rb
51
+ - lib/range_sentence_parser/version.rb
52
+ - lib/range_sentence_validator.rb
53
+ - range_sentence_parser.gemspec
54
+ - spec/range_sentence_parser_spec.rb
55
+ - spec/range_sentence_validator_spec.rb
56
+ - spec/spec_helper.rb
57
+ - spec/support/accounting_report.rb
58
+ - spec/support/matchers.rb
59
+ homepage: https://github.com/sobrinho/range_sentence_parser
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.11
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Parser for range sentences like '1990; 1995-2000; 2005'
83
+ test_files:
84
+ - spec/range_sentence_parser_spec.rb
85
+ - spec/range_sentence_validator_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/support/accounting_report.rb
88
+ - spec/support/matchers.rb