ellipsis 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.rdoc +56 -0
- data/Rakefile +29 -0
- data/ellipsis.gemspec +23 -0
- data/lib/ellipsis.rb +7 -0
- data/lib/ellipsis/core_ext/array/conversions.rb +35 -0
- data/lib/ellipsis/version.rb +3 -0
- data/test/core_ext/array_ext_test.rb +64 -0
- data/test/test_helper.rb +5 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
|
2
|
+
= Ellipsis
|
3
|
+
|
4
|
+
By making use of an ellipsis or similar symbols the representation of a list of
|
5
|
+
elements can be abbreviated. This can be helpful in situtations where character
|
6
|
+
space is limited, and at the same time may be more aesthetically pleasing.
|
7
|
+
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
Installation as usual:
|
12
|
+
|
13
|
+
gem install ellipsis
|
14
|
+
|
15
|
+
== Usage and Examples
|
16
|
+
|
17
|
+
After you require the gem with
|
18
|
+
|
19
|
+
require 'ellipsis'
|
20
|
+
|
21
|
+
The array class is extended with the following two methods that are similar
|
22
|
+
to the +to_sentence+ method in Rails.
|
23
|
+
|
24
|
+
=== to_abbr_series
|
25
|
+
|
26
|
+
This method assumes that the elements represent a sorted list;
|
27
|
+
Using the method displays an ellipsis. For example:
|
28
|
+
|
29
|
+
[1,2,3,4,5].to_abbr_series # => "1, 2, ..., 5"
|
30
|
+
|
31
|
+
|
32
|
+
=== to_abbr_set
|
33
|
+
|
34
|
+
This method summarizes consecutive subsets in with dash. See the following
|
35
|
+
example:
|
36
|
+
|
37
|
+
[1,2,3, 5,6].to_abbr_set # => "1-3, 5, 6"
|
38
|
+
|
39
|
+
So far the notion of consecutive refers to integer successors. Thus the array
|
40
|
+
needs to be an integer array. If you don't want integers in the actual string
|
41
|
+
representation you can pass an additional block that will convert an integer
|
42
|
+
to a custom string representation:
|
43
|
+
|
44
|
+
WEEKDAYS = ['mon', 'tue', 'wed','thu', 'fri', 'sat', 'sun']
|
45
|
+
days = [1,2,3,5,6]
|
46
|
+
|
47
|
+
days.to_abbr_set {|e| WEEKDAYS[e] } # => 'mon-wed, fri, sat'
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
== Outlook
|
52
|
+
|
53
|
+
* refactor unit tests
|
54
|
+
* Implement options similar to the +to_sentence+ method in Rails.
|
55
|
+
|
56
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
Bundler::GemHelper.install_tasks
|
6
|
+
|
7
|
+
desc 'Default: run all tests'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
desc 'Run unit tests'
|
11
|
+
Rake::TestTask.new(:test) do |test|
|
12
|
+
test.libs << 'lib' << 'test'
|
13
|
+
test.pattern = 'test/**/*_test.rb'
|
14
|
+
test.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate Documentation'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'Ellipsis'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline_source'
|
22
|
+
rdoc.rdoc_files.include('README.rdoc')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
data/ellipsis.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "ellipsis/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ellipsis"
|
7
|
+
s.version = Ellipsis::VERSION
|
8
|
+
s.authors = ["Kai Rubarth"]
|
9
|
+
s.email = ["kai@doxter.de"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Converts arrays into abbreviated String representations.}
|
12
|
+
s.description = %q{The gem provides various methods for turning a list of elements into an abbreviated string representation.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "ellipsis"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
|
data/lib/ellipsis.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def to_abbr_series(options={})
|
4
|
+
case length
|
5
|
+
when 0
|
6
|
+
""
|
7
|
+
when 1
|
8
|
+
self[0].to_s
|
9
|
+
when 2, 3
|
10
|
+
self.join(', ')
|
11
|
+
else
|
12
|
+
self[0..1].join(', ').to_s + ', ..., ' + self[-1].to_s
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_abbr_set(options={}, &to_s)
|
17
|
+
return '' if self.empty?
|
18
|
+
to_s ||= lambda{|e| e}
|
19
|
+
ci = 0
|
20
|
+
consecutive_sets = [[self.shift]]
|
21
|
+
|
22
|
+
self.each_with_index do |e,i|
|
23
|
+
if e <= consecutive_sets[ci].last + 1
|
24
|
+
consecutive_sets[ci] << e
|
25
|
+
else
|
26
|
+
ci+=1
|
27
|
+
consecutive_sets[ci] = [e]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
consecutive_sets.map{|s| s.map{|e| to_s.call(e)}}.map{|s| s.size > 2 ? "#{s[0]}-#{s[-1]}" : s.join(', ') }.join(', ')
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ArrayExtToAbbrSeries < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_empty
|
6
|
+
assert_equal '', [].to_abbr_series
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_one_element
|
10
|
+
assert_equal '1', [1].to_abbr_series
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_two_elements
|
14
|
+
assert_equal '1, 2', [1, 2].to_abbr_series
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_three_elements
|
18
|
+
assert_equal '1, 2, 3', [1, 2, 3].to_abbr_series
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_more_than_three_elements
|
22
|
+
assert_equal '1, 2, ..., 4', [1, 2, 3, 4].to_abbr_series
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
class ArrayExtToConsolidatedSeries < Test::Unit::TestCase
|
30
|
+
|
31
|
+
def test_empty
|
32
|
+
assert_equal '', [].to_abbr_set
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_single_element
|
36
|
+
assert_equal '1', [1].to_abbr_set
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_two_elements
|
40
|
+
assert_equal '1, 2', [1,2].to_abbr_set
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_non_consecutive_three_elements
|
44
|
+
assert_equal '1, 3, 5', [1,3,5].to_abbr_set
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_consecutive_three_elements
|
48
|
+
assert_equal '1-3', [1,2,3].to_abbr_set
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_consecutive_non_consecutive
|
52
|
+
assert_equal '1-3, 5, 6', [1,2,3, 5,6].to_abbr_set
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_consecutive_non_and_consecutive
|
56
|
+
assert_equal '1-3, 5, 7-9', [1,2,3, 5,7,8,9].to_abbr_set
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_block
|
60
|
+
assert_equal '1-9', [1,2,3].to_abbr_set {|e| "#{e*e}" }
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ellipsis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kai Rubarth
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-28 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: The gem provides various methods for turning a list of elements into an abbreviated string representation.
|
23
|
+
email:
|
24
|
+
- kai@doxter.de
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- ellipsis.gemspec
|
37
|
+
- lib/ellipsis.rb
|
38
|
+
- lib/ellipsis/core_ext/array/conversions.rb
|
39
|
+
- lib/ellipsis/version.rb
|
40
|
+
- test/core_ext/array_ext_test.rb
|
41
|
+
- test/test_helper.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: ""
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
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
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: ellipsis
|
72
|
+
rubygems_version: 1.5.2
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Converts arrays into abbreviated String representations.
|
76
|
+
test_files:
|
77
|
+
- test/core_ext/array_ext_test.rb
|
78
|
+
- test/test_helper.rb
|