triad 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +0 -1
- data/README.md +3 -3
- data/lib/triad.rb +22 -30
- data/lib/triad/version.rb +1 -1
- data/test/triad_test.rb +13 -28
- data/triad.gemspec +1 -1
- metadata +18 -27
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5c2803e0e4b1a381b1bf4b0dde05a3e75f459404
|
4
|
+
data.tar.gz: 5bf33b48f87d5a03eb3579181cc6fa3f3bf0c483
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bd4f351d5d91f0133667918cade75192fbf1f46e45c33e78090d7d4a45a80a62a496eb1a9a17f6e1c46bac5a2afb85b8b37be02f18751b68f6cb3ef71e8b8de5
|
7
|
+
data.tar.gz: 3ad7881dc4182e9a8ecd7b16b5d5f6cfbadd9cbe7bf717149579f3b0dc0a703878f5a2381bca2c9dd6b24e90dfb7e8cf97b49d0a3f30fbd11343c72ff0a6c364
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -22,17 +22,17 @@ map << [:admin, 'Admin', user]
|
|
22
22
|
map.keys('Admin') #=> [:admin]
|
23
23
|
map.values('Admin') #=> [user]
|
24
24
|
map.descriptors('Admin') #=> ['Admin']
|
25
|
-
# or raise Triad::
|
25
|
+
# or raise Triad::ItemNotPresent
|
26
26
|
|
27
27
|
map.descriptors(user) #=> ['Admin']
|
28
28
|
map.keys(user) #=> [:admin]
|
29
29
|
map.values(user) #=> [user]
|
30
|
-
# or raise Triad::
|
30
|
+
# or raise Triad::ItemNotPresent
|
31
31
|
|
32
32
|
map.values(:admin) #=> [user]
|
33
33
|
map.descriptors(:admin) #=> ['Admin']
|
34
34
|
map.keys(:admin) #=> [:admin]
|
35
|
-
# or raise Triad::
|
35
|
+
# or raise Triad::ItemNotPresent
|
36
36
|
|
37
37
|
map.each do |key, descriptor, value|
|
38
38
|
#...
|
data/lib/triad.rb
CHANGED
@@ -5,9 +5,7 @@ class Triad
|
|
5
5
|
include Enumerable
|
6
6
|
|
7
7
|
class InvalidAddition < StandardError; end
|
8
|
-
class
|
9
|
-
class DescriptorNotPresent < StandardError; end
|
10
|
-
class KeyNotPresent < StandardError; end
|
8
|
+
class ItemNotPresent < StandardError; end
|
11
9
|
|
12
10
|
# stored as {key => ['Descriptor', value]}
|
13
11
|
def initialize(*args)
|
@@ -41,12 +39,12 @@ class Triad
|
|
41
39
|
end
|
42
40
|
|
43
41
|
def <<(array)
|
44
|
-
array_key = array
|
42
|
+
array_key = array[0]
|
45
43
|
raise InvalidAddition.new("your array length must be 3") if array.length != 3
|
46
44
|
raise InvalidAddition.new("the provided key already exists") if key_exists?(array_key)
|
47
45
|
|
48
|
-
array_descriptor = array
|
49
|
-
array_value = array
|
46
|
+
array_descriptor = array[1]
|
47
|
+
array_value = array[2]
|
50
48
|
|
51
49
|
storage[array_key] = [array_descriptor, array_value]
|
52
50
|
self
|
@@ -67,35 +65,29 @@ class Triad
|
|
67
65
|
def key_exists?(key)
|
68
66
|
storage.has_key?(key)
|
69
67
|
end
|
70
|
-
|
71
|
-
def
|
72
|
-
[
|
73
|
-
end
|
74
|
-
|
75
|
-
def argument_type(arg)
|
76
|
-
case arg
|
77
|
-
when Symbol then :key
|
78
|
-
when String then :descriptor
|
79
|
-
else :value
|
80
|
-
end
|
68
|
+
|
69
|
+
def descriptor_exists?(descriptor)
|
70
|
+
storage.values.map{|arr| arr[0] }.include?(descriptor)
|
81
71
|
end
|
82
|
-
|
83
|
-
def
|
84
|
-
|
85
|
-
error_class = Triad.const_get("#{error_name}NotPresent")
|
86
|
-
raise error_class.new
|
72
|
+
|
73
|
+
def value_exists?(value)
|
74
|
+
storage.values.map{|arr| arr[1] }.include?(value)
|
87
75
|
end
|
88
76
|
|
89
77
|
def with_interest(interest)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
78
|
+
index = case
|
79
|
+
when key_exists?(interest)
|
80
|
+
0
|
81
|
+
when descriptor_exists?(interest)
|
82
|
+
1
|
83
|
+
when value_exists?(interest)
|
84
|
+
2
|
85
|
+
else
|
86
|
+
raise ItemNotPresent.new
|
87
|
+
end
|
88
|
+
|
89
|
+
storage.select{|key, array|
|
94
90
|
[key, *array][index] == interest
|
95
91
|
}.map{|key, array| [key, *array] }
|
96
|
-
|
97
|
-
raise_error(type) if lookup.empty?
|
98
|
-
|
99
|
-
lookup
|
100
92
|
end
|
101
93
|
end
|
data/lib/triad/version.rb
CHANGED
data/test/triad_test.rb
CHANGED
@@ -26,26 +26,11 @@ describe Triad, '#<<' do
|
|
26
26
|
triad << [:test, 'More', Object.new]
|
27
27
|
}
|
28
28
|
end
|
29
|
-
|
30
|
-
it '
|
31
|
-
object =
|
32
|
-
triad << ['
|
33
|
-
assert_equal [
|
34
|
-
assert_equal [:surprise], triad.keys(object)
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'assigns string as descriptor' do
|
38
|
-
object = Object.new
|
39
|
-
triad << ['OutOfOrder', object, :surprise]
|
40
|
-
assert_equal ['OutOfOrder'], triad.descriptors(:surprise)
|
41
|
-
assert_equal ['OutOfOrder'], triad.descriptors(object)
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'assigns non-symbol, non-string objects as value' do
|
45
|
-
object = Object.new
|
46
|
-
triad << ['OutOfOrder', object, :surprise]
|
47
|
-
assert_equal [object], triad.values(:surprise)
|
48
|
-
assert_equal [object], triad.values('OutOfOrder')
|
29
|
+
|
30
|
+
it 'accepts strings as values' do
|
31
|
+
object = "a string as an value"
|
32
|
+
triad << [:key, 'Descriptor', object]
|
33
|
+
assert_equal [object], triad.values(:key)
|
49
34
|
end
|
50
35
|
end
|
51
36
|
|
@@ -87,19 +72,19 @@ describe Triad, '#keys' do
|
|
87
72
|
end
|
88
73
|
|
89
74
|
it 'errors when the given key is not found' do
|
90
|
-
assert_raises(Triad::
|
75
|
+
assert_raises(Triad::ItemNotPresent){
|
91
76
|
triad.keys(:invalid_key)
|
92
77
|
}
|
93
78
|
end
|
94
79
|
|
95
80
|
it 'errors when the given descriptor is not found' do
|
96
|
-
assert_raises(Triad::
|
81
|
+
assert_raises(Triad::ItemNotPresent){
|
97
82
|
triad.keys('Invalid Descriptor')
|
98
83
|
}
|
99
84
|
end
|
100
85
|
|
101
86
|
it 'errors when the given value is not found' do
|
102
|
-
assert_raises(Triad::
|
87
|
+
assert_raises(Triad::ItemNotPresent){
|
103
88
|
triad.keys(Object.new)
|
104
89
|
}
|
105
90
|
end
|
@@ -131,19 +116,19 @@ describe Triad, '#descriptors' do
|
|
131
116
|
end
|
132
117
|
|
133
118
|
it 'errors when the given descriptor is not found' do
|
134
|
-
assert_raises(Triad::
|
119
|
+
assert_raises(Triad::ItemNotPresent){
|
135
120
|
triad.descriptors('Not Present')
|
136
121
|
}
|
137
122
|
end
|
138
123
|
|
139
124
|
it 'errors when the given key is not found' do
|
140
|
-
assert_raises(Triad::
|
125
|
+
assert_raises(Triad::ItemNotPresent){
|
141
126
|
triad.descriptors(:invalid_key)
|
142
127
|
}
|
143
128
|
end
|
144
129
|
|
145
130
|
it 'errors when the given value is not found' do
|
146
|
-
assert_raises(Triad::
|
131
|
+
assert_raises(Triad::ItemNotPresent){
|
147
132
|
triad.descriptors(Object.new)
|
148
133
|
}
|
149
134
|
end
|
@@ -172,13 +157,13 @@ describe Triad, '#values' do
|
|
172
157
|
|
173
158
|
|
174
159
|
it 'errors when the given key is not found' do
|
175
|
-
assert_raises(Triad::
|
160
|
+
assert_raises(Triad::ItemNotPresent){
|
176
161
|
triad.values(:invalid_key)
|
177
162
|
}
|
178
163
|
end
|
179
164
|
|
180
165
|
it 'errors when the given descriptor is not found' do
|
181
|
-
assert_raises(Triad::
|
166
|
+
assert_raises(Triad::ItemNotPresent){
|
182
167
|
triad.values('Invalid Descriptor')
|
183
168
|
}
|
184
169
|
end
|
data/triad.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "thread_safe", "~> 0.
|
21
|
+
spec.add_dependency "thread_safe", "~> 0.3.3"
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: triad
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
|
-
- '
|
7
|
+
- "'Jim Gay'"
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-05-08 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: thread_safe
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.
|
19
|
+
version: 0.3.3
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.
|
26
|
+
version: 0.3.3
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: bundler
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ~>
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '1.3'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ~>
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '1.3'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
description: Triad allows you to access data from keys, descriptors, and values
|
@@ -66,8 +59,8 @@ executables: []
|
|
66
59
|
extensions: []
|
67
60
|
extra_rdoc_files: []
|
68
61
|
files:
|
69
|
-
- .gitignore
|
70
|
-
- .travis.yml
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
71
64
|
- Gemfile
|
72
65
|
- LICENSE.txt
|
73
66
|
- README.md
|
@@ -80,29 +73,27 @@ files:
|
|
80
73
|
homepage: ''
|
81
74
|
licenses:
|
82
75
|
- MIT
|
76
|
+
metadata: {}
|
83
77
|
post_install_message:
|
84
78
|
rdoc_options: []
|
85
79
|
require_paths:
|
86
80
|
- lib
|
87
81
|
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
-
none: false
|
89
82
|
requirements:
|
90
|
-
- -
|
83
|
+
- - ">="
|
91
84
|
- !ruby/object:Gem::Version
|
92
85
|
version: '0'
|
93
86
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
-
none: false
|
95
87
|
requirements:
|
96
|
-
- -
|
88
|
+
- - ">="
|
97
89
|
- !ruby/object:Gem::Version
|
98
90
|
version: '0'
|
99
91
|
requirements: []
|
100
92
|
rubyforge_project:
|
101
|
-
rubygems_version:
|
93
|
+
rubygems_version: 2.2.0
|
102
94
|
signing_key:
|
103
|
-
specification_version:
|
95
|
+
specification_version: 4
|
104
96
|
summary: Manage a collection with 3 data points
|
105
97
|
test_files:
|
106
98
|
- test/test_helper.rb
|
107
99
|
- test/triad_test.rb
|
108
|
-
has_rdoc:
|