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 CHANGED
@@ -1 +1,2 @@
1
1
  .idea
2
+ pkg
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attr_comparable (0.0.1)
4
+ attr_comparable (0.0.2)
5
5
  minitest
6
6
 
7
7
  GEM
@@ -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 the SQL convention
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
- # when equal, returns nil rather than 0 so the caller can || together
14
- def compare_with_nil(left, right)
15
- if left.nil?
16
- if right.nil?
17
- nil
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
- -1
26
+ (#{ left } <=> #{ right })#{ '.nonzero?' if return_nil_on_equal }
20
27
  end
21
- elsif right.nil?
22
- 1
23
- else
24
- (left <=> right).nonzero?
25
- end
28
+ EOS
26
29
  end
27
30
 
28
- def attr_compare(*attributes)
29
- attributes = attributes.flatten
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
- #{attributes.map do |attribute|
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
@@ -1,3 +1,3 @@
1
1
  module AttrComparable
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  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
- version: 0.0.1
5
- prerelease:
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
- date: 2013-08-10 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: minitest
16
- requirement: !ruby/object:Gem::Requirement
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
- version_requirements: !ruby/object:Gem::Requirement
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
- files:
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
- require_paths:
70
+
71
+ require_paths:
67
72
  - lib
68
- required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
- requirements:
71
- - - ! '>='
72
- - !ruby/object:Gem::Version
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
- hash: 4162700180279227236
77
- required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
- requirements:
80
- - - ! '>='
81
- - !ruby/object:Gem::Version
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
- hash: 4162700180279227236
86
+ version: "0"
86
87
  requirements: []
88
+
87
89
  rubyforge_project:
88
- rubygems_version: 1.8.25
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
- to compare and the <=> (as needed by Comparable) is generated for you, including
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