curt-comparable_ext 0.0.0 → 0.0.1

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.
@@ -1,14 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "comparable_ext"
3
- s.version = "0.0.0"
4
- s.date = "2008-06-23"
3
+ s.version = "0.0.1"
4
+ s.date = "2008-06-24"
5
5
  s.summary = "Extensions to make Comparable more useful"
6
6
  s.email = "curt.gilman@reductivereason.com"
7
7
  s.homepage = "http://github.com/curt/comparable_ext"
8
8
  s.description = "Comparable Extensions is a Ruby library that extends the core libraries to make the Comparable module more useful."
9
9
  s.has_rdoc = true
10
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"]
11
+ s.files = ["README.rdoc", "Rakefile", "comparable_ext.gemspec", "lib/comparable_ext.rb", "lib/core_ext/integer/instance_methods.rb", "lib/core_ext/array/instance_methods.rb", "lib/core_ext/integer.rb", "lib/core_ext/array.rb", "test/comparable_ext_test.rb"]
12
12
  s.test_files = ["test/comparable_ext_test.rb"]
13
13
  s.rdoc_options = ["--main", "README.rdoc"]
14
14
  s.extra_rdoc_files = ["README.rdoc"]
@@ -1 +1,3 @@
1
1
  require 'core_ext/integer'
2
+ require 'core_ext/array'
3
+
@@ -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/array/instance_methods'
23
+
24
+ class Array #:nodoc:
25
+ include ComparableExt::CoreExtensions::Array::InstanceMethods
26
+ end
@@ -0,0 +1,84 @@
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 Array
28
+ # Instance methods that extend the Integer class
29
+ module InstanceMethods
30
+ # The <tt>sort_by_attributes</tt> method sorts an array based on a
31
+ # comparison of the attributes of the objects it contains. The arguments
32
+ # of the method are the attributes to be called. An attribute can be any
33
+ # method that takes no arguments.
34
+ #
35
+ # class Person
36
+ # attr_accessor :id
37
+ # attr_accessor :name
38
+ # attr_accessor :type
39
+ #
40
+ # def initialize(id, name, type)
41
+ # @id = id
42
+ # @name = name
43
+ # @type = type
44
+ # end
45
+ # end
46
+ #
47
+ # objs = [Person.new(1, 'Andy', 'a'),
48
+ # Person.new(2, 'Zach', 'a'),
49
+ # Person.new(2, 'Andy', 'z')]
50
+ #
51
+ # objs.sort_by_attributes(:name, :type, :id)
52
+ #
53
+ # # => [#<Person:0xb7cbc9e4 @type="a", @name="Andy", @id=1>,
54
+ # # #<Person:0xb7cbc96c @type="z", @name="Andy", @id=2>,
55
+ # # #<Person:0xb7cbc9a8 @type="a", @name="Zach", @id=2>]
56
+ def sort_by_attributes(*args)
57
+ self.sort do |arg0, arg1|
58
+ sorter_by_attributes args, arg0, arg1
59
+ end
60
+ end
61
+
62
+ # The <tt>sort_by_attributes!</tt> method behaves similarly to
63
+ # <tt>sort_by_attributes</tt>, except that it sorts <em>self</em>.
64
+ def sort_by_attributes!(*args)
65
+ self.sort! do |arg0, arg1|
66
+ sorter_by_attributes args, arg0, arg1
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def sorter_by_attributes(args, arg0, arg1)
73
+ comp = 0
74
+ args.each do |arg|
75
+ attr0 = arg0.method(arg).call
76
+ attr1 = arg1.method(arg).call
77
+ comp = comp.zthen(attr0 <=> attr1)
78
+ end
79
+ comp
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -29,6 +29,7 @@ module ComparableExt
29
29
  module InstanceMethods
30
30
  # The <tt>zthen</tt> method always returns the receiver object when its
31
31
  # value is not zero. Otherwise, it returns <tt>arg</tt>.
32
+ #
32
33
  # 1.zthen(0) # => 1
33
34
  # -1.zthen(1) # => -1
34
35
  # 0.zthen(1) # => 1
@@ -36,7 +37,7 @@ module ComparableExt
36
37
  # 0.zthen(0) # => 0
37
38
  def zthen(arg)
38
39
  unless self.zero? then self else arg end
39
- end
40
+ end
40
41
  end
41
42
  end
42
43
  end
@@ -22,7 +22,7 @@
22
22
  require 'comparable_ext'
23
23
  require 'test/unit'
24
24
 
25
- class ComparableExtTest < Test::Unit::TestCase
25
+ class IntegerZthenTest < Test::Unit::TestCase
26
26
  def test_positive_zthen_positive
27
27
  assert_equal 1, 1.zthen(1)
28
28
  end
@@ -59,3 +59,84 @@ class ComparableExtTest < Test::Unit::TestCase
59
59
  assert_equal -1, -1.zthen(-1)
60
60
  end
61
61
  end
62
+
63
+ class EnumerableSortByAttributesTest < Test::Unit::TestCase
64
+ def setup
65
+ klass = Class.new do
66
+ attr_accessor :id
67
+ attr_accessor :name
68
+ attr_accessor :type
69
+
70
+ def initialize(id, name, type)
71
+ @id = id
72
+ @name = name
73
+ @type = type
74
+ end
75
+ end
76
+
77
+ @objs = [klass.new(1, 'Andy', 'a'),
78
+ klass.new(2, 'Zach', 'a'),
79
+ klass.new(2, 'Andy', 'z')]
80
+ end
81
+
82
+ def test_length
83
+ sorted = @objs.sort_by_attributes :id
84
+
85
+ assert_equal 3, sorted.size
86
+ end
87
+
88
+ def test_sort_id_name_type
89
+ sorted = @objs.sort_by_attributes :id, :name, :type
90
+
91
+ assert_equal 1, sorted[0].id
92
+ assert_equal 'Zach', sorted[2].name
93
+ assert_equal 'z', sorted[1].type
94
+ end
95
+
96
+ def test_sort_name_type_id
97
+ sorted = @objs.sort_by_attributes :name, :type, :id
98
+
99
+ assert_equal 1, sorted[0].id
100
+ assert_equal 'Zach', sorted[2].name
101
+ assert_equal 'z', sorted[1].type
102
+ end
103
+
104
+ def test_sort_type_id_name
105
+ sorted = @objs.sort_by_attributes :type, :id, :name
106
+
107
+ assert_equal 1, sorted[0].id
108
+ assert_equal 'Zach', sorted[1].name
109
+ assert_equal 'z', sorted[2].type
110
+ end
111
+
112
+ def test_length_in_place
113
+ @objs.sort_by_attributes! :id
114
+
115
+ assert_equal 3, @objs.size
116
+ end
117
+
118
+ def test_sort_id_name_type_in_place
119
+ @objs.sort_by_attributes! :id, :name, :type
120
+
121
+ assert_equal 1, @objs[0].id
122
+ assert_equal 'Zach', @objs[2].name
123
+ assert_equal 'z', @objs[1].type
124
+ end
125
+
126
+ def test_sort_name_type_id_in_place
127
+ @objs.sort_by_attributes! :name, :type, :id
128
+
129
+ assert_equal 1, @objs[0].id
130
+ assert_equal 'Zach', @objs[2].name
131
+ assert_equal 'z', @objs[1].type
132
+ end
133
+
134
+ def test_sort_type_id_name_in_place
135
+ @objs.sort_by_attributes! :type, :id, :name
136
+
137
+ assert_equal 1, @objs[0].id
138
+ assert_equal 'Zach', @objs[1].name
139
+ assert_equal 'z', @objs[2].type
140
+ end
141
+ end
142
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curt-comparable_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Curt A. Gilman
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-23 00:00:00 -07:00
12
+ date: 2008-06-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -27,7 +27,10 @@ files:
27
27
  - comparable_ext.gemspec
28
28
  - lib/comparable_ext.rb
29
29
  - lib/core_ext/integer/instance_methods.rb
30
+ - lib/core_ext/array/instance_methods.rb
30
31
  - lib/core_ext/integer.rb
32
+ - lib/core_ext/array.rb
33
+ - test/comparable_ext_test.rb
31
34
  has_rdoc: true
32
35
  homepage: http://github.com/curt/comparable_ext
33
36
  post_install_message:
@@ -51,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
54
  requirements: []
52
55
 
53
56
  rubyforge_project:
54
- rubygems_version: 1.0.1
57
+ rubygems_version: 1.2.0
55
58
  signing_key:
56
59
  specification_version: 2
57
60
  summary: Extensions to make Comparable more useful