ruby-development-toolbox 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/toolbox/array.rb +41 -0
- data/ruby-development-toolbox.gemspec +7 -3
- data/test/representations/address.rb +66 -0
- data/test/representations/person.rb +47 -0
- data/test/test_toolbox_array.rb +109 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9bb887cbfc473c25506ca33604187b4b15a33f1
|
4
|
+
data.tar.gz: 25eef0d7c4cfbc4c698444642a02bd2328f24dae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9977bce3f042f36f8d9f191a944bb8359ba7da27187d9ff178ab66696141beb5d89de8453ef3841c285a496277e9164fa4dc42ba05015efea836c4e1e2dbe38e
|
7
|
+
data.tar.gz: dc32154bbe283c1114ef590ddc625293ae7ff835531cc19ce7190357a8257d03d429783b354a98664156088c218c6385c028f74665c406ec934ce0f5633f4a74
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.3.
|
1
|
+
1.3.1
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
##
|
4
|
+
# Adds the ability to check if Array contains a subset of objects or a single object. See toolbox/array
|
5
|
+
#
|
6
|
+
def =~(other)
|
7
|
+
if other.is_a?(::Array)
|
8
|
+
other.each { |obj| return false unless self.include?(obj) }
|
9
|
+
true
|
10
|
+
else
|
11
|
+
self.include?(other)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :contains?, :=~
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
module Toolbox
|
20
|
+
|
21
|
+
##
|
22
|
+
# This is to implement equivalence check for arrays. In the context of arrays,
|
23
|
+
# we use =~ as a means of checking if array1 is contained in array2; or if
|
24
|
+
# object is contained in array1
|
25
|
+
#
|
26
|
+
# sample_array = [1, 2, 3]
|
27
|
+
# puts sample_array =~ 3 ## This prints true
|
28
|
+
# puts sample_array =~ 4 ## This prints false
|
29
|
+
#
|
30
|
+
# The other way is the following:
|
31
|
+
#
|
32
|
+
# sample_array1 = [1, 2, 3]
|
33
|
+
# sample_array2 = [1, 2, 3, 4]
|
34
|
+
#
|
35
|
+
# puts sample_array2 =~ sample_array1 ## This prints true, because everything in array1 is in array2
|
36
|
+
# puts sample_array1 =~ sample_array2 ## This prints false, because not everything in array2 is in array1
|
37
|
+
#
|
38
|
+
module Array
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: ruby-development-toolbox 1.3.
|
5
|
+
# stub: ruby-development-toolbox 1.3.1 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "ruby-development-toolbox"
|
9
|
-
s.version = "1.3.
|
9
|
+
s.version = "1.3.1"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Peter Salas"]
|
14
|
-
s.date = "2014-
|
14
|
+
s.date = "2014-12-17"
|
15
15
|
s.description = "A collection of useful utilities and libraries for Ruby development (not Rails)"
|
16
16
|
s.email = "psalas+github@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"Rakefile",
|
30
30
|
"VERSION",
|
31
31
|
"lib/ruby-development-toolbox.rb",
|
32
|
+
"lib/toolbox/array.rb",
|
32
33
|
"lib/toolbox/boolean.rb",
|
33
34
|
"lib/toolbox/gem_specification.rb",
|
34
35
|
"lib/toolbox/hash_diff.rb",
|
@@ -37,7 +38,10 @@ Gem::Specification.new do |s|
|
|
37
38
|
"lib/toolbox/uuid.rb",
|
38
39
|
"ruby-development-toolbox.gemspec",
|
39
40
|
"test/helper.rb",
|
41
|
+
"test/representations/address.rb",
|
42
|
+
"test/representations/person.rb",
|
40
43
|
"test/test_toolbox-integer.rb",
|
44
|
+
"test/test_toolbox_array.rb",
|
41
45
|
"test/test_toolbox_json.rb"
|
42
46
|
]
|
43
47
|
s.homepage = "http://github.com/gradeawarrior/ruby-development-toolbox"
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class Address
|
4
|
+
include Comparable
|
5
|
+
|
6
|
+
attr_accessor :address1, :address2, :city, :state, :zip
|
7
|
+
|
8
|
+
def initialize(entity={})
|
9
|
+
@address1 = entity['address1']
|
10
|
+
@address2 = entity['address2']
|
11
|
+
@city = entity['city']
|
12
|
+
@state = entity['state']
|
13
|
+
@zip = entity['zip']
|
14
|
+
end
|
15
|
+
|
16
|
+
def ==(obj)
|
17
|
+
return false unless obj.is_a?(::Address)
|
18
|
+
return false unless @address1 == obj.address1
|
19
|
+
return false unless @address2 == obj.address2
|
20
|
+
return false unless @city == obj.city
|
21
|
+
return false unless @state == obj.state
|
22
|
+
return false unless @zip == obj.zip
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def <=>(obj)
|
27
|
+
address1 = @address1 <=> obj.address1
|
28
|
+
address2 = @address2 <=> obj.address2
|
29
|
+
city = @city <=> obj.city
|
30
|
+
state = @state <=> obj.state
|
31
|
+
zip = @zip <=> obj.zip
|
32
|
+
|
33
|
+
case address1
|
34
|
+
when 0
|
35
|
+
case address2
|
36
|
+
when 0
|
37
|
+
case city
|
38
|
+
when 0
|
39
|
+
case state
|
40
|
+
when 0
|
41
|
+
zip
|
42
|
+
else
|
43
|
+
state
|
44
|
+
end
|
45
|
+
else
|
46
|
+
city
|
47
|
+
end
|
48
|
+
else
|
49
|
+
address2
|
50
|
+
end
|
51
|
+
else
|
52
|
+
address1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_json(*args)
|
57
|
+
{
|
58
|
+
:address1 => @address1,
|
59
|
+
:address2 => @address2,
|
60
|
+
:city => @city,
|
61
|
+
:state => @state,
|
62
|
+
:zip => @zip
|
63
|
+
}.to_json
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'representations/address'
|
3
|
+
|
4
|
+
class Person
|
5
|
+
include Comparable
|
6
|
+
|
7
|
+
attr_accessor :first_name, :last_name, :email, :addresses
|
8
|
+
|
9
|
+
def initialize(entity={})
|
10
|
+
@first_name = entity['first_name']
|
11
|
+
@last_name = entity['last_name']
|
12
|
+
@email = entity['email']
|
13
|
+
@addresses = []
|
14
|
+
entity['addresses'].each { |address| @addresses << Address.new(address) } unless entity['addresses'].nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(obj)
|
18
|
+
return false unless obj.is_a?(::Person)
|
19
|
+
return false unless @first_name == obj.first_name
|
20
|
+
return false unless @last_name == obj.last_name
|
21
|
+
return false unless @addresses.sort == obj.addresses.sort
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
def <=>(obj)
|
26
|
+
last_name_comparison = @last_name <=> obj.last_name
|
27
|
+
case last_name_comparison
|
28
|
+
when 0
|
29
|
+
@first_name <=> obj.first_name
|
30
|
+
else
|
31
|
+
last_name_comparison
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_json(*args)
|
36
|
+
{
|
37
|
+
:first_name => @first_name,
|
38
|
+
:last_name => @last_name,
|
39
|
+
:email => @email,
|
40
|
+
:addresses => @addresses
|
41
|
+
}.to_json
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_s
|
45
|
+
"#{@first_name} #{@last_name}"
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'toolbox/array'
|
3
|
+
require 'representations/person'
|
4
|
+
|
5
|
+
class TestToolboxArray < Test::Unit::TestCase
|
6
|
+
|
7
|
+
PERSONS_ENTITY1 = [
|
8
|
+
{
|
9
|
+
'first_name' => 'C',
|
10
|
+
'last_name' => 'D',
|
11
|
+
'email' => 'CD@proofpoint.com',
|
12
|
+
'addresses' => [
|
13
|
+
'address1' => '12345 Foobar Way'
|
14
|
+
]
|
15
|
+
},
|
16
|
+
{
|
17
|
+
'first_name' => 'A',
|
18
|
+
'last_name' => 'B',
|
19
|
+
'email' => 'AB@proofpoint.com'
|
20
|
+
}
|
21
|
+
]
|
22
|
+
PERSONS_ENTITY2 = [
|
23
|
+
{
|
24
|
+
'first_name' => 'E',
|
25
|
+
'last_name' => 'F',
|
26
|
+
'email' => 'EF@proofpoint.com',
|
27
|
+
'addresses' => [
|
28
|
+
'address1' => '6789 Hello World Dr.',
|
29
|
+
'city' => 'Sunnyvale',
|
30
|
+
'state' => 'CA',
|
31
|
+
'zip' => '94536'
|
32
|
+
]
|
33
|
+
},
|
34
|
+
{
|
35
|
+
'first_name' => 'C',
|
36
|
+
'last_name' => 'D',
|
37
|
+
'email' => 'CD@proofpoint.com',
|
38
|
+
'addresses' => [
|
39
|
+
'address1' => '12345 Foobar Way'
|
40
|
+
]
|
41
|
+
},
|
42
|
+
{
|
43
|
+
'first_name' => 'A',
|
44
|
+
'last_name' => 'B',
|
45
|
+
'email' => 'AB@proofpoint.com'
|
46
|
+
}
|
47
|
+
]
|
48
|
+
PERSONS1 = PERSONS_ENTITY1.inject([]) { |result, person_entity| result << Person.new(person_entity); result }
|
49
|
+
PERSONS2 = PERSONS_ENTITY2.inject([]) { |result, person_entity| result << Person.new(person_entity); result }
|
50
|
+
|
51
|
+
should 'be able to validate whether two arrays are the same' do
|
52
|
+
persons1 = PERSONS1.dup
|
53
|
+
persons1.sort!
|
54
|
+
persons2 = PERSONS1.dup
|
55
|
+
|
56
|
+
assert_equal false, persons1 == persons2, 'Expect that a sorted and an unsorted array should not equal each other'
|
57
|
+
assert_equal true, persons1 =~ persons2, 'Expect that sorted and an unsorted array with =~ equivalence check should be equal (regardless of order)'
|
58
|
+
assert_equal true, persons2 =~ persons1, 'Expect that sorted and an unsorted array (inverse) with =~ equivalence check should be equal (regardless of order)'
|
59
|
+
end
|
60
|
+
|
61
|
+
should 'be able to validate whether elements in persons1 are contained in persons2' do
|
62
|
+
persons1 = PERSONS1.dup
|
63
|
+
persons1.sort!
|
64
|
+
persons2 = PERSONS2.dup
|
65
|
+
|
66
|
+
assert_equal false, persons1 == persons2, 'Expect that two arrays with similar objects should not equal each other'
|
67
|
+
assert_equal false, persons1 =~ persons2, 'Expect that persons2 array is not contained in persons1 array'
|
68
|
+
assert_equal true, persons2 =~ persons1, 'Expect that persons1 array is contained in persons2 array'
|
69
|
+
end
|
70
|
+
|
71
|
+
should 'be able to validate whether element is contained in an array' do
|
72
|
+
person_exist = PERSONS2[1].dup
|
73
|
+
person_not_exist = PERSONS2[0].dup
|
74
|
+
persons = PERSONS1.dup
|
75
|
+
|
76
|
+
assert_equal false, persons =~ person_not_exist, 'Expect that EF@proofpoint.com does not exist in persons array'
|
77
|
+
assert_equal true, persons =~ person_exist, 'Expect that CD@proofpoint.com exists in persons array'
|
78
|
+
end
|
79
|
+
|
80
|
+
should 'be able to validate whether two arrays are the same (with alias method contains?)' do
|
81
|
+
persons1 = PERSONS1.dup
|
82
|
+
persons1.sort!
|
83
|
+
persons2 = PERSONS1.dup
|
84
|
+
|
85
|
+
assert_equal false, persons1 == persons2, 'Expect that a sorted and an unsorted array should not equal each other'
|
86
|
+
assert_equal true, persons1.contains?(persons2), 'Expect that sorted and an unsorted array with =~ equivalence check should be equal (regardless of order)'
|
87
|
+
assert_equal true, persons2.contains?(persons1), 'Expect that sorted and an unsorted array (inverse) with =~ equivalence check should be equal (regardless of order)'
|
88
|
+
end
|
89
|
+
|
90
|
+
should 'be able to validate whether elements in persons1 are contained in persons2 (with alias method contains?)' do
|
91
|
+
persons1 = PERSONS1.dup
|
92
|
+
persons1.sort!
|
93
|
+
persons2 = PERSONS2.dup
|
94
|
+
|
95
|
+
assert_equal false, persons1 == persons2, 'Expect that two arrays with similar objects should not equal each other'
|
96
|
+
assert_equal false, persons1.contains?(persons2), 'Expect that persons2 array is not contained in persons1 array'
|
97
|
+
assert_equal true, persons2.contains?(persons1), 'Expect that persons1 array is contained in persons2 array'
|
98
|
+
end
|
99
|
+
|
100
|
+
should 'be able to validate whether element is contained in an array (with alias method contains?)' do
|
101
|
+
person_exist = PERSONS2[1].dup
|
102
|
+
person_not_exist = PERSONS2[0].dup
|
103
|
+
persons = PERSONS1.dup
|
104
|
+
|
105
|
+
assert_equal false, persons.contains?(person_not_exist), 'Expect that EF@proofpoint.com does not exist in persons array'
|
106
|
+
assert_equal true, persons.contains?(person_exist), 'Expect that CD@proofpoint.com exists in persons array'
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-development-toolbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Salas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: shoulda
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- Rakefile
|
114
114
|
- VERSION
|
115
115
|
- lib/ruby-development-toolbox.rb
|
116
|
+
- lib/toolbox/array.rb
|
116
117
|
- lib/toolbox/boolean.rb
|
117
118
|
- lib/toolbox/gem_specification.rb
|
118
119
|
- lib/toolbox/hash_diff.rb
|
@@ -121,7 +122,10 @@ files:
|
|
121
122
|
- lib/toolbox/uuid.rb
|
122
123
|
- ruby-development-toolbox.gemspec
|
123
124
|
- test/helper.rb
|
125
|
+
- test/representations/address.rb
|
126
|
+
- test/representations/person.rb
|
124
127
|
- test/test_toolbox-integer.rb
|
128
|
+
- test/test_toolbox_array.rb
|
125
129
|
- test/test_toolbox_json.rb
|
126
130
|
homepage: http://github.com/gradeawarrior/ruby-development-toolbox
|
127
131
|
licenses:
|