reciter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +20 -0
- data/README.rdoc +53 -0
- data/lib/reciter.rb +5 -0
- data/lib/reciter/config.rb +9 -0
- data/lib/reciter/error.rb +4 -0
- data/lib/reciter/parser.rb +60 -0
- data/lib/reciter/version.rb +3 -0
- metadata +69 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Rodrigo Manhães
|
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,53 @@
|
|
1
|
+
= reciter
|
2
|
+
|
3
|
+
{<img src="https://secure.travis-ci.org/rodrigomanhaes/reciter.png"/>}[http://travis-ci.org/rodrigomanhaes/reciter]
|
4
|
+
{<img src="https://gemnasium.com/rodrigomanhaes/reciter.png"/>}[https://gemnasium.com/rodrigomanhaes/reciter]
|
5
|
+
|
6
|
+
Parses number sequences in the form:
|
7
|
+
|
8
|
+
p.parse('1-3;5;9-11')
|
9
|
+
|
10
|
+
|
11
|
+
when "p" is an instance of Reciter::Parse, returning, for this case, the
|
12
|
+
following array:
|
13
|
+
|
14
|
+
[1, 2, 3, 5, 9, 10, 11]
|
15
|
+
|
16
|
+
|
17
|
+
reciter also unparses an array to a human-readable sentence, when:
|
18
|
+
|
19
|
+
p.unparse(1, 2, 3, 5, 9, 10, 11)
|
20
|
+
|
21
|
+
|
22
|
+
returns:
|
23
|
+
|
24
|
+
"1 to 3, 5, 9 to 11"
|
25
|
+
|
26
|
+
|
27
|
+
An alternative text can be provided for replacing "to":
|
28
|
+
|
29
|
+
Reciter::Parser.config.text_for_to = 'a'
|
30
|
+
p.unparse(1, 2, 3) # "1 a 3"
|
31
|
+
|
32
|
+
|
33
|
+
== How to install
|
34
|
+
|
35
|
+
Just run:
|
36
|
+
|
37
|
+
gem install reciter
|
38
|
+
|
39
|
+
|
40
|
+
== Contributing to reciter
|
41
|
+
|
42
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
43
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
44
|
+
* Fork the project.
|
45
|
+
* Start a feature/bugfix branch.
|
46
|
+
* Commit and push until you are happy with your contribution.
|
47
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
48
|
+
* 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.
|
49
|
+
|
50
|
+
== Copyright
|
51
|
+
|
52
|
+
Copyright (c) 2012 Rodrigo Manhães. See LICENSE.txt for
|
53
|
+
further details.
|
data/lib/reciter.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Reciter
|
2
|
+
class Parser
|
3
|
+
def parse(sequence)
|
4
|
+
validate_format(sequence)
|
5
|
+
sequence.
|
6
|
+
split(';').
|
7
|
+
map {|subsequence|
|
8
|
+
if subsequence.include?('-')
|
9
|
+
r = Range.new(*subsequence.split('-').map(&:to_i))
|
10
|
+
raise InvalidInput unless r.begin <= r.end
|
11
|
+
r.to_a
|
12
|
+
else
|
13
|
+
subsequence
|
14
|
+
end
|
15
|
+
}.
|
16
|
+
flatten.
|
17
|
+
uniq.
|
18
|
+
map(&:to_i).
|
19
|
+
sort
|
20
|
+
end
|
21
|
+
|
22
|
+
def unparse(*sequence)
|
23
|
+
ranges = []
|
24
|
+
last = nil
|
25
|
+
sequence.sort.each do |n|
|
26
|
+
if last.nil? || n != last + 1
|
27
|
+
ranges << Range.new(n, n)
|
28
|
+
else
|
29
|
+
ranges[-1] = Range.new(ranges[-1].begin, n)
|
30
|
+
end
|
31
|
+
last = n
|
32
|
+
end
|
33
|
+
ranges.map {|r|
|
34
|
+
if r.end == r.begin || r.end == r.begin + 1
|
35
|
+
r.to_a
|
36
|
+
else
|
37
|
+
"%s %s %s" % [r.begin, config.text_for_to, r.end]
|
38
|
+
end
|
39
|
+
}.
|
40
|
+
flatten.
|
41
|
+
join(', ')
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.config
|
45
|
+
@config ||= Reciter::Config.new
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def validate_format(sequence)
|
51
|
+
subsequences = sequence.split(';')
|
52
|
+
raise InvalidInput unless !subsequences.empty? &&
|
53
|
+
subsequences.all? {|subsequence| subsequence =~ /^(\d+\-\d+)$|^\d+$/ }
|
54
|
+
end
|
55
|
+
|
56
|
+
def config
|
57
|
+
self.class.config
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reciter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rodrigo Manhães
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.11.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.11.0
|
30
|
+
description: Parses number sequences.
|
31
|
+
email: rmanhaes@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- lib/reciter/parser.rb
|
37
|
+
- lib/reciter/config.rb
|
38
|
+
- lib/reciter/version.rb
|
39
|
+
- lib/reciter/error.rb
|
40
|
+
- lib/reciter.rb
|
41
|
+
- README.rdoc
|
42
|
+
- LICENSE.txt
|
43
|
+
homepage: https://github.com/rodrigomanhaes/reciter
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Parses number sequences
|
69
|
+
test_files: []
|