range_builder 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/lib/range_builder/version.rb +3 -0
- data/lib/range_builder.rb +37 -0
- data/range_builder.gemspec +17 -0
- data/test/range_builder_test.rb +26 -0
- metadata +54 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Thomas Sonntag
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# RangeBuilder
|
2
|
+
|
3
|
+
Builds ranges from an enumeration
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'range_builder'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install range_builder
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
|
22
|
+
persons = [15, 60, 15, 61, 59, 19].map{|age| Person.new(age: age) }
|
23
|
+
|
24
|
+
RangeBuilder.build_range( persons ){|person| person.age }
|
25
|
+
|
26
|
+
# returns range from mininal to maximal data:
|
27
|
+
# ( <Person aged 15> .. <Person aged 61> )
|
28
|
+
|
29
|
+
|
30
|
+
sorted_array = [ 2, 3, 4, 6, 7, 9, 11, 12, 13]
|
31
|
+
|
32
|
+
# sorted_array must be sorted and elements must implement successor: #succ
|
33
|
+
|
34
|
+
RangeBuilder.build_from_sorted(sorted_array)
|
35
|
+
|
36
|
+
# returns array of ranges:
|
37
|
+
# [ (2..4), (6..7), (9..9), (11..13) ]
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
class RangeBuilder
|
2
|
+
|
3
|
+
# Usage:
|
4
|
+
# build_range( persons ){|person| person.age }
|
5
|
+
def self.build_range enum
|
6
|
+
first, last = nil, nil
|
7
|
+
comp_first, comp_last = nil, nil
|
8
|
+
enum.each do |obj|
|
9
|
+
comp = yield obj
|
10
|
+
if first == nil || comp < comp_first
|
11
|
+
first = obj
|
12
|
+
comp_first = yield first
|
13
|
+
end
|
14
|
+
if last == nil || comp > comp_last
|
15
|
+
last = obj
|
16
|
+
comp_last = yield last
|
17
|
+
end
|
18
|
+
end
|
19
|
+
[first, last]
|
20
|
+
end
|
21
|
+
|
22
|
+
# arg: sorted enum. elements must implement succ
|
23
|
+
def self.build_from_sorted enum
|
24
|
+
ranges = []
|
25
|
+
first = enum.first
|
26
|
+
last = nil
|
27
|
+
enum.each_cons(2) do |a,b|
|
28
|
+
last = b
|
29
|
+
if a.succ != b
|
30
|
+
ranges << Range.new(first,a)
|
31
|
+
first = b
|
32
|
+
end
|
33
|
+
end
|
34
|
+
ranges << Range.new(first,last)
|
35
|
+
ranges
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/range_builder/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Thomas Sonntag"]
|
6
|
+
gem.email = ["git@sonntagsbox.de"]
|
7
|
+
gem.description = %q{Build ranges from an enumeration}
|
8
|
+
gem.summary = %q{Build ranges from an enumeration}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "range_builder"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RangeBuilder::VERSION
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
$:.unshift File.expand_path( '../lib/', File.dirname( __FILE__))
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'range_builder'
|
5
|
+
|
6
|
+
class RangeBuilderTest < MiniTest::Unit::TestCase
|
7
|
+
M = Struct.new(:foo)
|
8
|
+
|
9
|
+
def test_build_range
|
10
|
+
m = [3, 2, 6, 2].map{|i|M.new(i)}
|
11
|
+
puts RangeBuilder.build_range(m){|a|a.foo}.inspect
|
12
|
+
first, last = RangeBuilder.build_range(m){|a|a.foo}
|
13
|
+
assert_equal 2, first.foo
|
14
|
+
assert_equal 6, last.foo
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_build_from_sorted
|
18
|
+
m = [ 2, 3, 4, 6, 7, 9, 11, 12, 13]
|
19
|
+
ranges = RangeBuilder.build_from_sorted(m)
|
20
|
+
assert_equal 4, ranges.size
|
21
|
+
assert_equal (2..4), ranges[0]
|
22
|
+
assert_equal (6..7), ranges[1]
|
23
|
+
assert_equal (9..9), ranges[2]
|
24
|
+
assert_equal (11..13), ranges[3]
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: range_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thomas Sonntag
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-04 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Build ranges from an enumeration
|
15
|
+
email:
|
16
|
+
- git@sonntagsbox.de
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/range_builder.rb
|
27
|
+
- lib/range_builder/version.rb
|
28
|
+
- range_builder.gemspec
|
29
|
+
- test/range_builder_test.rb
|
30
|
+
homepage: ''
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.11
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Build ranges from an enumeration
|
54
|
+
test_files: []
|