attr_comparable 0.0.2 → 0.2.0.pre.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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +7 -1
- data/Gemfile.lock +16 -4
- data/Jenkinsfile +56 -0
- data/README.md +6 -1
- data/Rakefile +4 -4
- data/attr_comparable.gemspec +4 -3
- data/lib/attr_comparable.rb +12 -12
- data/lib/attr_comparable/version.rb +1 -1
- data/semaphore_ci/setup.sh +3 -0
- data/test/attr_comparable_test.rb +49 -4
- data/test/test_helper.rb +6 -0
- metadata +32 -67
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4298a8f8bf1a126196d9bedaeca3e22a91b3a33c6c862b5355436c1120675fc8
|
4
|
+
data.tar.gz: eb72be741397d6f77ff7bd45e80175fc08e9cd0d00b1fae4327486a27b07e974
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46bdc2c502aa48134ce9b8484099311b69ccc8a6361bcd4743d8d8159d3d50a6b64fd668cbff092456677642529977c76eae827c2cd820ee16adfb20faede4d9
|
7
|
+
data.tar.gz: 533d118ee77b4041a197faa6c86f3558a6e3be4c70685200fd2edddbd1dff0cef0573d82a537daeef040b8cee5db1646073a1b1365f18fa6982283111f17acae
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.1
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,18 +1,30 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
attr_comparable (0.0.
|
5
|
-
minitest
|
4
|
+
attr_comparable (0.2.0.pre.1)
|
6
5
|
|
7
6
|
GEM
|
8
7
|
remote: https://rubygems.org/
|
9
8
|
specs:
|
10
|
-
|
11
|
-
|
9
|
+
ansi (1.5.0)
|
10
|
+
builder (3.2.3)
|
11
|
+
minitest (5.13.0)
|
12
|
+
minitest-reporters (1.4.2)
|
13
|
+
ansi
|
14
|
+
builder
|
15
|
+
minitest (>= 5.0)
|
16
|
+
ruby-progressbar
|
17
|
+
rake (13.0.1)
|
18
|
+
ruby-progressbar (1.10.1)
|
12
19
|
|
13
20
|
PLATFORMS
|
14
21
|
ruby
|
15
22
|
|
16
23
|
DEPENDENCIES
|
17
24
|
attr_comparable!
|
25
|
+
minitest
|
26
|
+
minitest-reporters
|
18
27
|
rake (>= 0.9)
|
28
|
+
|
29
|
+
BUNDLED WITH
|
30
|
+
1.17.2
|
data/Jenkinsfile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/groovy
|
2
|
+
@Library('jenkins-pipeline@v0.4.4')
|
3
|
+
import com.invoca.docker.*;
|
4
|
+
|
5
|
+
pipeline {
|
6
|
+
agent {
|
7
|
+
kubernetes {
|
8
|
+
defaultContainer 'ruby'
|
9
|
+
yaml '''
|
10
|
+
apiVersion: v1
|
11
|
+
kind: Pod
|
12
|
+
metadata:
|
13
|
+
labels:
|
14
|
+
jenkins/attr-comparable: true
|
15
|
+
namespace: jenkins
|
16
|
+
name: attr-comparable
|
17
|
+
spec:
|
18
|
+
containers:
|
19
|
+
- name: ruby
|
20
|
+
image: ruby:2.6.1
|
21
|
+
tty: true
|
22
|
+
command:
|
23
|
+
- cat
|
24
|
+
'''
|
25
|
+
}
|
26
|
+
}
|
27
|
+
stages {
|
28
|
+
stage('Unit Tests') {
|
29
|
+
environment {
|
30
|
+
GITHUB_KEY = credentials('github_key')
|
31
|
+
}
|
32
|
+
|
33
|
+
steps {
|
34
|
+
script {
|
35
|
+
sh '''
|
36
|
+
# get SSH setup inside the container
|
37
|
+
eval `ssh-agent -s`
|
38
|
+
echo "$GITHUB_KEY" | ssh-add -
|
39
|
+
mkdir -p /root/.ssh
|
40
|
+
ssh-keyscan -t rsa github.com > /root/.ssh/known_hosts
|
41
|
+
|
42
|
+
# run tests
|
43
|
+
bundle install --path vendor/bundle
|
44
|
+
bundle exec rake test
|
45
|
+
'''
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
post {
|
50
|
+
always { junit 'test/reports/*.xml' }
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
post { always { notifySlack(currentBuild.result) } }
|
56
|
+
}
|
data/README.md
CHANGED
@@ -31,6 +31,11 @@ class FullName
|
|
31
31
|
(middle <=> other.middle).nonzero? ||
|
32
32
|
suffix <=> other.suffix
|
33
33
|
end
|
34
|
+
|
35
|
+
def to_s
|
36
|
+
no_suffix = [first.presence, middle.presence, last.presence].compact.join(' ')
|
37
|
+
[no_suffix, suffix.presence].compact.join(', ')
|
38
|
+
end
|
34
39
|
end
|
35
40
|
```
|
36
41
|
You can see that the `<=>` method isn't very DRY, and as shown it doesn't even work with `nil`.
|
@@ -70,6 +75,6 @@ Example Usage
|
|
70
75
|
>> junior = FullName.new("John", "Q.", "Public", "Jr.")
|
71
76
|
>> junior > dad
|
72
77
|
=> true
|
73
|
-
>> [mom, dad
|
78
|
+
>> [junior, mom, dad].sort.map &:to_s
|
74
79
|
=> ["Kathy Doe", "John Q. Public", "John Q. Public, Jr."]
|
75
80
|
```
|
data/Rakefile
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
-
require
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake/testtask'
|
3
4
|
|
4
5
|
task :default => :test
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
ruby "test/attr_comparable_test.rb"
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.pattern = "test/**/*_test.rb"
|
9
9
|
end
|
data/attr_comparable.gemspec
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
require File.expand_path('../lib/attr_comparable/version', __FILE__)
|
2
2
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
|
-
gem.add_dependency 'minitest' # Included in Ruby 1.9, but we want the latest.
|
5
|
-
gem.add_development_dependency 'rake', '>=0.9'
|
6
|
-
|
7
4
|
gem.authors = ["Colin Kelley"]
|
8
5
|
gem.email = ["colindkelley@gmail.com"]
|
9
6
|
gem.description = %q{AttrComparable}
|
10
7
|
gem.summary = %q{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.}
|
11
8
|
gem.homepage = "https://github.com/RingRevenue/attr_comparable"
|
12
9
|
|
10
|
+
gem.metadata = {
|
11
|
+
'allowed_push_host' => 'https://rubygems.org'
|
12
|
+
}
|
13
|
+
|
13
14
|
gem.files = `git ls-files`.split($\)
|
14
15
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
16
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/.*\.rb})
|
data/lib/attr_comparable.rb
CHANGED
@@ -11,35 +11,35 @@ module AttrComparable
|
|
11
11
|
module ClassMethods
|
12
12
|
# like <=> but handles nil values
|
13
13
|
# returns the code in string form to be eval'd
|
14
|
-
|
15
|
-
def compare_with_nil_code(left, right, return_nil_on_equal)
|
14
|
+
def compare_with_nil_code(left, right)
|
16
15
|
<<-EOS
|
17
16
|
if #{ left }.nil?
|
18
17
|
if #{ right }.nil?
|
19
|
-
|
18
|
+
0
|
20
19
|
else
|
21
20
|
-1
|
22
21
|
end
|
23
22
|
elsif #{ right }.nil?
|
24
23
|
1
|
25
24
|
else
|
26
|
-
(#{ left } <=> #{ right })
|
25
|
+
if (cmp_result = (#{ left } <=> #{ right })).nil?
|
26
|
+
return nil
|
27
|
+
end
|
28
|
+
cmp_result
|
27
29
|
end
|
28
30
|
EOS
|
29
31
|
end
|
30
32
|
|
31
|
-
def attr_compare(*
|
32
|
-
|
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
|
33
|
+
def attr_compare(*attributes)
|
34
|
+
attr_exprs = (attributes.flatten).map do |attribute|
|
35
|
+
'(' + compare_with_nil_code("self.#{attribute}", "rhs.#{attribute}").strip + ')'
|
38
36
|
end
|
39
37
|
|
40
38
|
class_eval <<-EOS
|
41
39
|
def <=>(rhs)
|
42
|
-
|
40
|
+
unless rhs.nil?
|
41
|
+
#{ attr_exprs.join(".nonzero? || ") }
|
42
|
+
end
|
43
43
|
end
|
44
44
|
EOS
|
45
45
|
end
|
@@ -1,8 +1,5 @@
|
|
1
|
+
require_relative './test_helper'
|
1
2
|
require File.expand_path('../../lib/attr_comparable', __FILE__)
|
2
|
-
require 'bundler'
|
3
|
-
Bundler.require(:default)
|
4
|
-
require 'minitest/autorun'
|
5
|
-
|
6
3
|
|
7
4
|
class ComparableTestOneParameter
|
8
5
|
include AttrComparable
|
@@ -97,4 +94,52 @@ describe 'AttrComparable' do
|
|
97
94
|
assert @d2 != @d3
|
98
95
|
end
|
99
96
|
end
|
97
|
+
|
98
|
+
describe "parameters that are incompatible to order" do
|
99
|
+
it "should return nil when the objects contain incompatible attributes" do
|
100
|
+
d1 = ComparableTestOneParameter.new(false)
|
101
|
+
d2 = ComparableTestOneParameter.new(true)
|
102
|
+
d3 = ComparableTestManyParameters.new(false, 'D')
|
103
|
+
d4 = ComparableTestManyParameters.new(true, 'D')
|
104
|
+
d5 = ComparableTestManyParameters.new('Kelly', false)
|
105
|
+
d6 = ComparableTestManyParameters.new('Kelly', true)
|
106
|
+
|
107
|
+
assert_nil d1 <=> d2
|
108
|
+
assert_nil d3 <=> d4
|
109
|
+
assert_nil d5 <=> d6
|
110
|
+
end
|
111
|
+
|
112
|
+
# For ComparableTestManyParameters the objects are compared by last_name then first_name
|
113
|
+
# If the comparison of last_names returns nil, that should immediately return nil,
|
114
|
+
# and not compare the remaining attributes. This code tests that by raising a runtime error
|
115
|
+
# if the first_name attribute (later in the compare order) is acessed
|
116
|
+
|
117
|
+
it "should return immediately return nils, not evaulate further attributes" do
|
118
|
+
incompatible_first_attribute = ComparableTestManyParameters.new('Kelly', false)
|
119
|
+
compatible_first_and_second_attribute = ComparableTestManyParameters.new(true, 'D')
|
120
|
+
|
121
|
+
d7 = Minitest::Mock.new
|
122
|
+
d8 = Minitest::Mock.new
|
123
|
+
|
124
|
+
2.times { d7.expect :last_name, false, [] } # :last_name is used exactly twice per <=> call
|
125
|
+
d7.expect :nil?, false, [] # :nil? is used exactly once per <=> call
|
126
|
+
assert_nil incompatible_first_attribute <=> d7
|
127
|
+
|
128
|
+
2.times { d8.expect :last_name, true, [] }
|
129
|
+
2.times { d8.expect :first_name, 'D', [] }
|
130
|
+
d8.expect :nil?, false, []
|
131
|
+
assert_equal 0, compatible_first_and_second_attribute <=> d8
|
132
|
+
|
133
|
+
d7.verify
|
134
|
+
d8.verify
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should return nil if the rhs is nil" do
|
138
|
+
d1 = ComparableTestOneParameter.new(false)
|
139
|
+
d2 = ComparableTestManyParameters.new('Kelly', false)
|
140
|
+
|
141
|
+
assert_nil d1 <=> nil
|
142
|
+
assert_nil d2 <=> nil
|
143
|
+
end
|
144
|
+
end
|
100
145
|
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,95 +1,60 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_comparable
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0.pre.1
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
6
|
+
authors:
|
12
7
|
- Colin Kelley
|
13
8
|
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
default_executable:
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
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
|
31
|
-
prerelease: false
|
32
|
-
- !ruby/object:Gem::Dependency
|
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
|
44
|
-
prerelease: false
|
11
|
+
date: 2020-04-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
45
13
|
description: AttrComparable
|
46
|
-
email:
|
14
|
+
email:
|
47
15
|
- colindkelley@gmail.com
|
48
16
|
executables: []
|
49
|
-
|
50
17
|
extensions: []
|
51
|
-
|
52
18
|
extra_rdoc_files: []
|
53
|
-
|
54
|
-
|
55
|
-
- .
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".ruby-version"
|
56
22
|
- Gemfile
|
57
23
|
- Gemfile.lock
|
24
|
+
- Jenkinsfile
|
58
25
|
- README.md
|
59
26
|
- Rakefile
|
60
27
|
- attr_comparable.gemspec
|
61
28
|
- lib/attr_comparable.rb
|
62
29
|
- lib/attr_comparable/version.rb
|
30
|
+
- semaphore_ci/setup.sh
|
63
31
|
- test/attr_comparable_test.rb
|
64
|
-
|
32
|
+
- test/test_helper.rb
|
65
33
|
homepage: https://github.com/RingRevenue/attr_comparable
|
66
34
|
licenses: []
|
67
|
-
|
35
|
+
metadata:
|
36
|
+
allowed_push_host: https://rubygems.org
|
68
37
|
post_install_message:
|
69
38
|
rdoc_options: []
|
70
|
-
|
71
|
-
require_paths:
|
39
|
+
require_paths:
|
72
40
|
- lib
|
73
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
-
requirements:
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
75
43
|
- - ">="
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
- !ruby/object:Gem::Version
|
84
|
-
segments:
|
85
|
-
- 0
|
86
|
-
version: "0"
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 1.3.1
|
87
51
|
requirements: []
|
88
|
-
|
89
|
-
rubyforge_project:
|
90
|
-
rubygems_version: 1.3.6
|
52
|
+
rubygems_version: 3.0.1
|
91
53
|
signing_key:
|
92
|
-
specification_version:
|
93
|
-
summary: Mix-in to make a value class Comparable. Simply declare the order of attributes
|
94
|
-
|
54
|
+
specification_version: 4
|
55
|
+
summary: Mix-in to make a value class Comparable. Simply declare the order of attributes
|
56
|
+
to compare and the <=> (as needed by Comparable) is generated for you, including
|
57
|
+
support for nil. Includes Comparable.
|
58
|
+
test_files:
|
95
59
|
- test/attr_comparable_test.rb
|
60
|
+
- test/test_helper.rb
|