rangify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig ADDED
Binary file
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ Rakefile
3
+ lib/rangify.rb
4
+ spec/rangify_spec.rb
5
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = Rangify
2
+
3
+ This gem will add the rangify method to Ruby's Array class.
4
+ The rangify method will produce appropriate ranges from the objects in the array.
5
+
6
+ = Usage
7
+
8
+ Examples:
9
+
10
+ [1,2,3,6,7,8].rangify = [1..3, 6..8]
11
+
12
+ [10..15, 16..20, 21, 22].rangify = [10..22]
13
+
14
+
15
+ Assumes inclusive ranges (ie. 1..4) and range.first <= range.last.
16
+
17
+ Works with integers, dates and strings. However, all the objects in the array must be of the same class.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('rangify', '0.1.0') do |p|
6
+ p.description = "Combine array elements into ranges."
7
+ p.url = "http://github.com/monocle/rangify"
8
+ p.author = "monocle"
9
+ p.email = "frappez_2000@yahoo.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext}
data/lib/rangify.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Rangify
2
+ def rangify
3
+ raise 'Array elements are not consistent.' if self.collect { |value| comparison_value(value, :first).class }.uniq.size > 1
4
+ array = self.uniq.sort_by { |element| comparison_value(element, :first) }
5
+ array.inject([]) do |collection, value|
6
+ unless collection.empty?
7
+ last = collection.last
8
+ last_value = comparison_value(last, :last)
9
+ current_value = comparison_value(value, :first)
10
+ if (last_value.succ <=> current_value) == -1
11
+ collection << value
12
+ else
13
+ first = [comparison_value(last, :first), comparison_value(value, :first)].min
14
+ second = [comparison_value(last, :last), comparison_value(value, :last)].max
15
+ collection[-1] = [first..second]
16
+ collection.flatten!
17
+ end
18
+ else
19
+ collection << value
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ # For a Range, will return value.first or value.last. A non-Range will return itself.
27
+ def comparison_value(value, position)
28
+ return value if value.class != Range
29
+ position == :first ? value.first : value.last
30
+ end
31
+ end
32
+
33
+ class Array
34
+ include Rangify
35
+ end
data/rangify.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rangify}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["monocle"]
9
+ s.date = %q{2010-06-29}
10
+ s.description = %q{Combine array elements into ranges.}
11
+ s.email = %q{frappez_2000@yahoo.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/rangify.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "lib/rangify.rb", "spec/rangify_spec.rb", "Manifest", "rangify.gemspec"]
14
+ s.homepage = %q{http://github.com/monocle/rangify}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rangify", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{rangify}
18
+ s.rubygems_version = %q{1.3.6}
19
+ s.summary = %q{Combine array elements into ranges.}
20
+ s.signing_key = '/ruby/keys/rangify/gem-private_key.pem'
21
+ s.cert_chain = ['/ruby/keys/rangify/gem-public_cert.pem']
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
@@ -0,0 +1,45 @@
1
+ require 'lib/rangify'
2
+
3
+ describe Rangify do
4
+ it 'An array of consecutive integers should return an array made up of a single range.' do
5
+ arr = [1,2,3,4,5]
6
+ arr.rangify.should == [1..5]
7
+ end
8
+
9
+ it 'An array of non-consecutive integers should return the original array.' do
10
+ arr = [1,3,5,7]
11
+ arr.rangify.should == arr
12
+ end
13
+
14
+ describe 'An array of consecutive and non-consecutive integers' do
15
+ before :each do
16
+ @result = [1..3, 6..8, 10, 15]
17
+ end
18
+
19
+ it 'should return the correct ranges.' do
20
+ arr = [1,2,3,6,7,8,10,15]
21
+ arr.rangify.should == @result
22
+ end
23
+
24
+ it 'Array element order should not affect the result.' do
25
+ arr = [8, 1, 15, 2, 6, 3, 7, 10]
26
+ arr.rangify.should == @result
27
+ end
28
+
29
+ it 'Duplicate elements should not affect the result.' do
30
+ arr = [8, 1, 15, 2, 6, 3, 7, 10, 8, 15, 2, 3, 1, 2]
31
+ arr.rangify.should == @result
32
+ end
33
+ end
34
+
35
+ it 'An array of ranges should return the correct ranges.' do
36
+ arr = [40..45, 1..3, 4..10, 20..30, 24..28, 42..50, 1..6, 1..3]
37
+ arr.rangify.should == [1..10, 20..30, 40..50]
38
+ end
39
+
40
+ it 'An array of ranges and integers should return the correct ranges.' do
41
+ arr = [99, 100, 1..3, 101, 4..5, 103, 10..19, 99, 20..20, 31, 32..33, 98, 97]
42
+ arr.rangify.should == [1..5, 10..20, 31..33, 97..101, 103]
43
+ end
44
+ end
45
+
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rangify
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - monocle
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain:
16
+ - |
17
+ -----BEGIN CERTIFICATE-----
18
+ MIIDOjCCAiKgAwIBAgIBADANBgkqhkiG9w0BAQUFADBDMRUwEwYDVQQDDAxmcmFw
19
+ cGV6XzIwMDAxFTATBgoJkiaJk/IsZAEZFgV5YWhvbzETMBEGCgm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w9fnBtY=
36
+ -----END CERTIFICATE-----
37
+
38
+ date: 2010-06-29 00:00:00 -07:00
39
+ default_executable:
40
+ dependencies: []
41
+
42
+ description: Combine array elements into ranges.
43
+ email: frappez_2000@yahoo.com
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - README.rdoc
50
+ - lib/rangify.rb
51
+ files:
52
+ - README.rdoc
53
+ - Rakefile
54
+ - lib/rangify.rb
55
+ - spec/rangify_spec.rb
56
+ - Manifest
57
+ - rangify.gemspec
58
+ has_rdoc: true
59
+ homepage: http://github.com/monocle/rangify
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --line-numbers
65
+ - --inline-source
66
+ - --title
67
+ - Rangify
68
+ - --main
69
+ - README.rdoc
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 1
85
+ - 2
86
+ version: "1.2"
87
+ requirements: []
88
+
89
+ rubyforge_project: rangify
90
+ rubygems_version: 1.3.6
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Combine array elements into ranges.
94
+ test_files: []
95
+
metadata.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ axw��h��\�q����VM�>ĞA���G�F�R�8��k@ʫW�o:ԟ�8�����"�\�Y��F�Q�i,d�k��i�C��d�%A Ii�������o�0#4�%�J1d�K�F9"�#s�w��=����$�
2
+ � s4���M)U���l|0�T�E�:�O���6�f:60!��˕����H̤�K��Q�E�:������V�c�l6�����fW��6�!