attr_comparable 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/lib/attr_comparable.rb +25 -18
- data/lib/attr_comparable/version.rb +1 -1
- data/test/attr_comparable_test.rb +0 -7
- metadata +59 -59
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/attr_comparable.rb
CHANGED
@@ -3,36 +3,43 @@
|
|
3
3
|
#
|
4
4
|
# Use attr_comparabie <attribute list>
|
5
5
|
# to declare the attributes which should be compared, and the order they should be
|
6
|
-
# Attributes may be nil; nil attributes sort earlier than non-nil to match
|
6
|
+
# Attributes may be nil; nil attributes sort earlier than non-nil to match SQL NULL ordering
|
7
7
|
#
|
8
8
|
module AttrComparable
|
9
9
|
include Comparable
|
10
10
|
|
11
11
|
module ClassMethods
|
12
12
|
# like <=> but handles nil values
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
# returns the code in string form to be eval'd
|
14
|
+
# code will return nil instead of 0 for equal iff return_nil_on_equal is truthy
|
15
|
+
def compare_with_nil_code(left, right, return_nil_on_equal)
|
16
|
+
<<-EOS
|
17
|
+
if #{ left }.nil?
|
18
|
+
if #{ right }.nil?
|
19
|
+
#{ return_nil_on_equal ? 'nil' : '0' }
|
20
|
+
else
|
21
|
+
-1
|
22
|
+
end
|
23
|
+
elsif #{ right }.nil?
|
24
|
+
1
|
18
25
|
else
|
19
|
-
|
26
|
+
(#{ left } <=> #{ right })#{ '.nonzero?' if return_nil_on_equal }
|
20
27
|
end
|
21
|
-
|
22
|
-
1
|
23
|
-
else
|
24
|
-
(left <=> right).nonzero?
|
25
|
-
end
|
28
|
+
EOS
|
26
29
|
end
|
27
30
|
|
28
|
-
def attr_compare(*
|
29
|
-
attributes =
|
31
|
+
def attr_compare(*attributes_arg)
|
32
|
+
attributes = attributes_arg.flatten
|
33
|
+
|
34
|
+
remaining_attrs = attributes.size;
|
35
|
+
attr_exprs = attributes.map do |attribute|
|
36
|
+
remaining_attrs -= 1
|
37
|
+
compare_with_nil_code("self.#{attribute}", "rhs.#{attribute}", remaining_attrs.nonzero?).strip
|
38
|
+
end
|
39
|
+
|
30
40
|
class_eval <<-EOS
|
31
41
|
def <=>(rhs)
|
32
|
-
#{
|
33
|
-
"self.class.compare_with_nil(self.#{attribute}, rhs.#{attribute})"
|
34
|
-
end.join(" || ")
|
35
|
-
} || 0
|
42
|
+
#{ attr_exprs.join(" || ") }
|
36
43
|
end
|
37
44
|
EOS
|
38
45
|
end
|
@@ -75,13 +75,6 @@ describe 'AttrComparable' do
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
it "should be able to compare with nil" do
|
79
|
-
assert_equal nil, ComparableTestManyParameters.compare_with_nil(nil, nil)
|
80
|
-
assert_equal -1, ComparableTestManyParameters.compare_with_nil(nil, 1)
|
81
|
-
assert_equal 1, ComparableTestOneParameter.compare_with_nil(1, nil)
|
82
|
-
assert_equal 1, ComparableTestOneParameter.compare_with_nil("dog", "cat")
|
83
|
-
end
|
84
|
-
|
85
78
|
describe "many parameters with nil" do
|
86
79
|
before do # sort order
|
87
80
|
@d1 = ComparableTestManyParameters.new(nil, 'S') # 1
|
metadata
CHANGED
@@ -1,55 +1,57 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_comparable
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
6
10
|
platform: ruby
|
7
|
-
authors:
|
11
|
+
authors:
|
8
12
|
- Colin Kelley
|
9
13
|
autorequire:
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
16
|
+
|
17
|
+
date: 2013-08-10 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
22
21
|
type: :runtime
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 0
|
28
|
+
version: "0"
|
29
|
+
name: minitest
|
30
|
+
requirement: *id001
|
23
31
|
prerelease: false
|
24
|
-
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: rake
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0.9'
|
32
|
+
- !ruby/object:Gem::Dependency
|
38
33
|
type: :development
|
34
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
segments:
|
39
|
+
- 0
|
40
|
+
- 9
|
41
|
+
version: "0.9"
|
42
|
+
name: rake
|
43
|
+
requirement: *id002
|
39
44
|
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0.9'
|
46
45
|
description: AttrComparable
|
47
|
-
email:
|
46
|
+
email:
|
48
47
|
- colindkelley@gmail.com
|
49
48
|
executables: []
|
49
|
+
|
50
50
|
extensions: []
|
51
|
+
|
51
52
|
extra_rdoc_files: []
|
52
|
-
|
53
|
+
|
54
|
+
files:
|
53
55
|
- .gitignore
|
54
56
|
- Gemfile
|
55
57
|
- Gemfile.lock
|
@@ -59,37 +61,35 @@ files:
|
|
59
61
|
- lib/attr_comparable.rb
|
60
62
|
- lib/attr_comparable/version.rb
|
61
63
|
- test/attr_comparable_test.rb
|
64
|
+
has_rdoc: true
|
62
65
|
homepage: https://github.com/RingRevenue/attr_comparable
|
63
66
|
licenses: []
|
67
|
+
|
64
68
|
post_install_message:
|
65
69
|
rdoc_options: []
|
66
|
-
|
70
|
+
|
71
|
+
require_paths:
|
67
72
|
- lib
|
68
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
version: '0'
|
74
|
-
segments:
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
75
78
|
- 0
|
76
|
-
|
77
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
version: '0'
|
83
|
-
segments:
|
79
|
+
version: "0"
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
84
85
|
- 0
|
85
|
-
|
86
|
+
version: "0"
|
86
87
|
requirements: []
|
88
|
+
|
87
89
|
rubyforge_project:
|
88
|
-
rubygems_version: 1.
|
90
|
+
rubygems_version: 1.3.6
|
89
91
|
signing_key:
|
90
92
|
specification_version: 3
|
91
|
-
summary: Mix-in to make a value class Comparable. Simply declare the order of attributes
|
92
|
-
|
93
|
-
support for nil. Includes Comparable.
|
94
|
-
test_files:
|
93
|
+
summary: Mix-in to make a value class Comparable. Simply declare the order of attributes to compare and the <=> (as needed by Comparable) is generated for you, including support for nil. Includes Comparable.
|
94
|
+
test_files:
|
95
95
|
- test/attr_comparable_test.rb
|