triad 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -4,7 +4,6 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  group :test do
7
- gem "rake"
8
7
  gem "simplecov"
9
8
  gem "minitest"
10
9
  gem "coveralls"
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::DescriptorNotPresent
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::ValueNotPresent
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::KeyNotPresent
35
+ # or raise Triad::ItemNotPresent
36
36
 
37
37
  map.each do |key, descriptor, value|
38
38
  #...
@@ -5,9 +5,7 @@ class Triad
5
5
  include Enumerable
6
6
 
7
7
  class InvalidAddition < StandardError; end
8
- class ValueNotPresent < StandardError; end
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.find{|item| item.is_a?(Symbol) }
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.find{|item| item.is_a?(String) }
49
- array_value = array.find{|item| !item.is_a?(String) && !item.is_a?(Symbol) }
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 positions
72
- [:key, :descriptor, :value]
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 raise_error(type)
84
- error_name = type.to_s.gsub(/(?:^|_)([a-z])/) { $1.upcase }
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
- type = argument_type(interest)
91
- index = positions.index(type)
92
-
93
- lookup = storage.select{|key, array|
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
@@ -1,3 +1,3 @@
1
1
  class Triad
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -26,26 +26,11 @@ describe Triad, '#<<' do
26
26
  triad << [:test, 'More', Object.new]
27
27
  }
28
28
  end
29
-
30
- it 'assigns symbol as key' do
31
- object = Object.new
32
- triad << ['OutOfOrder', object, :surprise]
33
- assert_equal [:surprise], triad.keys('OutOfOrder')
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::KeyNotPresent){
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::DescriptorNotPresent){
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::ValueNotPresent){
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::DescriptorNotPresent){
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::KeyNotPresent){
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::ValueNotPresent){
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::KeyNotPresent){
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::DescriptorNotPresent){
166
+ assert_raises(Triad::ItemNotPresent){
182
167
  triad.values('Invalid Descriptor')
183
168
  }
184
169
  end
@@ -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.1.3"
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.1.3
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
- - '''Jim Gay'''
7
+ - "'Jim Gay'"
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-02 00:00:00.000000000 Z
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.1.3
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.1.3
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: 1.8.26
93
+ rubygems_version: 2.2.0
102
94
  signing_key:
103
- specification_version: 3
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: