curt-comparable_ext 0.0.0
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.
- data/README.rdoc +57 -0
- data/Rakefile +23 -0
- data/comparable_ext.gemspec +15 -0
- data/lib/comparable_ext.rb +1 -0
- data/lib/core_ext/integer.rb +26 -0
- data/lib/core_ext/integer/instance_methods.rb +43 -0
- data/test/comparable_ext_test.rb +61 -0
- metadata +59 -0
data/README.rdoc
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= Comparable Extensions
|
2
|
+
|
3
|
+
A Ruby gem containing extensions that make <tt>Comparable</tt> more useful
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
Comparable Extensions extends the Ruby core libraries to make the
|
8
|
+
<tt>Comparable</tt> module more useful. <tt>Comparable</tt> is the module that
|
9
|
+
provides the <tt><=></tt> operator, among other things.
|
10
|
+
|
11
|
+
Currently the only extension is the addition of a <tt>zthen</tt> method on the
|
12
|
+
<tt>Integer</tt> class. The method can be used to string comparisons together
|
13
|
+
more easily.
|
14
|
+
|
15
|
+
== Examples
|
16
|
+
|
17
|
+
=== Simple <tt>zthen</tt> example
|
18
|
+
|
19
|
+
Include the library.
|
20
|
+
|
21
|
+
require 'comparable_ext'
|
22
|
+
|
23
|
+
Define an Employee class with two attributes and an initializer.
|
24
|
+
|
25
|
+
class Employee
|
26
|
+
attr :name
|
27
|
+
attr :salary
|
28
|
+
def initialize(name, salary)
|
29
|
+
@name = name
|
30
|
+
@salary = salary
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
Define an array of Employee objects.
|
35
|
+
|
36
|
+
employees = [Employee.new('Bob', 24000), Employee.new('Jill', 24000),
|
37
|
+
Employee.new('Bob', 32000), Employee.new('Sarah', 32000)]
|
38
|
+
|
39
|
+
Sort employees by name, then salary.
|
40
|
+
|
41
|
+
employees.sort{|x, y| (x.name <=> y.name).zthen(x.salary <=> y.salary)}
|
42
|
+
|
43
|
+
# => [#<Employee:0xb7cd21cc @salary=24000, @name="Bob">, #<Employee:0xb7cd20c8
|
44
|
+
# @salary=32000, @name="Bob">, #<Employee:0xb7cd2154 @salary=24000,
|
45
|
+
# @name="Jill">, #<Employee:0xb7cd203c @salary=32000, @name="Sarah">]
|
46
|
+
|
47
|
+
Sort employees by salary, then name.
|
48
|
+
|
49
|
+
employees.sort{|x, y| (x.salary <=> y.salary).zthen(x.name <=> y.name)}
|
50
|
+
|
51
|
+
# => [#<Employee:0xb7cd21cc @salary=24000, @name="Bob">, #<Employee:0xb7cd2154
|
52
|
+
# @salary=24000, @name="Jill">, #<Employee:0xb7cd20c8 @salary=32000,
|
53
|
+
# @name="Bob">, #<Employee:0xb7cd203c @salary=32000, @name="Sarah">]
|
54
|
+
|
55
|
+
== License
|
56
|
+
|
57
|
+
Copyright (c) 2008 Curt A. Gilman, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the paranoid_find plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the paranoid_find plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Comparable Extenstions'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : 'doc/template/horo'
|
21
|
+
rdoc.rdoc_files.include('README.rdoc')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "comparable_ext"
|
3
|
+
s.version = "0.0.0"
|
4
|
+
s.date = "2008-06-23"
|
5
|
+
s.summary = "Extensions to make Comparable more useful"
|
6
|
+
s.email = "curt.gilman@reductivereason.com"
|
7
|
+
s.homepage = "http://github.com/curt/comparable_ext"
|
8
|
+
s.description = "Comparable Extensions is a Ruby library that extends the core libraries to make the Comparable module more useful."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Curt A. Gilman"]
|
11
|
+
s.files = ["README.rdoc", "Rakefile", "comparable_ext.gemspec", "lib/comparable_ext.rb", "lib/core_ext/integer/instance_methods.rb", "lib/core_ext/integer.rb"]
|
12
|
+
s.test_files = ["test/comparable_ext_test.rb"]
|
13
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
14
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
15
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'core_ext/integer'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Copyright (c) 2008 Curt A. Gilman
|
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.
|
21
|
+
|
22
|
+
require 'core_ext/integer/instance_methods'
|
23
|
+
|
24
|
+
class Integer #:nodoc:
|
25
|
+
include ComparableExt::CoreExtensions::Integer::InstanceMethods
|
26
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Copyright (c) 2008 Curt A. Gilman
|
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.
|
21
|
+
|
22
|
+
# Extensions that make Comparable more useful
|
23
|
+
module ComparableExt
|
24
|
+
# Extensions to Ruby core libraries
|
25
|
+
module CoreExtensions
|
26
|
+
# Extensions to the Integer class
|
27
|
+
module Integer
|
28
|
+
# Instance methods that extend the Integer class
|
29
|
+
module InstanceMethods
|
30
|
+
# The <tt>zthen</tt> method always returns the receiver object when its
|
31
|
+
# value is not zero. Otherwise, it returns <tt>arg</tt>.
|
32
|
+
# 1.zthen(0) # => 1
|
33
|
+
# -1.zthen(1) # => -1
|
34
|
+
# 0.zthen(1) # => 1
|
35
|
+
# 0.zthen(-1) # => -1
|
36
|
+
# 0.zthen(0) # => 0
|
37
|
+
def zthen(arg)
|
38
|
+
unless self.zero? then self else arg end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Copyright (c) 2008 Curt A. Gilman
|
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.
|
21
|
+
|
22
|
+
require 'comparable_ext'
|
23
|
+
require 'test/unit'
|
24
|
+
|
25
|
+
class ComparableExtTest < Test::Unit::TestCase
|
26
|
+
def test_positive_zthen_positive
|
27
|
+
assert_equal 1, 1.zthen(1)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_positive_zthen_zero
|
31
|
+
assert_equal 1, 1.zthen(0)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_positive_zthen_negative
|
35
|
+
assert_equal 1, 1.zthen(-1)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_zero_zthen_positive
|
39
|
+
assert_equal 1, 0.zthen(1)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_zero_zthen_zero
|
43
|
+
assert_equal 0, 0.zthen(0)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_zero_zthen_negative
|
47
|
+
assert_equal -1, 0.zthen(-1)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_negative_zthen_positive
|
51
|
+
assert_equal -1, -1.zthen(1)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_negative_zthen_zero
|
55
|
+
assert_equal -1, -1.zthen(0)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_negative_zthen_negative
|
59
|
+
assert_equal -1, -1.zthen(-1)
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: curt-comparable_ext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Curt A. Gilman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-23 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Comparable Extensions is a Ruby library that extends the core libraries to make the Comparable module more useful.
|
17
|
+
email: curt.gilman@reductivereason.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- comparable_ext.gemspec
|
28
|
+
- lib/comparable_ext.rb
|
29
|
+
- lib/core_ext/integer/instance_methods.rb
|
30
|
+
- lib/core_ext/integer.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/curt/comparable_ext
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options:
|
35
|
+
- --main
|
36
|
+
- README.rdoc
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.0.1
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: Extensions to make Comparable more useful
|
58
|
+
test_files:
|
59
|
+
- test/comparable_ext_test.rb
|