Class: Triad
- Inherits:
-
Object
- Object
- Triad
- Includes:
- Enumerable
- Defined in:
- lib/triad.rb,
lib/triad/version.rb
Defined Under Namespace
Classes: InvalidAddition, ItemNotPresent
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Method Summary collapse
-
#<<(array) ⇒ Object
Add new entries to the object.
-
#descriptors(arg = nil) ⇒ Object
Return the descriptors for a given key or value.
- #each ⇒ Object
-
#initialize ⇒ Triad
constructor
stored as => [‘Descriptor’, value].
-
#keys(arg = nil) ⇒ Object
Return the keys for a given descriptor or value.
-
#update(key, descriptor, value) ⇒ Object
Alter the descriptor and value in-place for the given key.
-
#values(arg = :__no_argument_given__) ⇒ Object
Return the values for a given key or descriptor.
Constructor Details
#initialize ⇒ Triad
stored as => [‘Descriptor’, value]
11 12 13 |
# File 'lib/triad.rb', line 11 def initialize @storage = Concurrent::Hash.new end |
Instance Method Details
#<<(array) ⇒ Object
Add new entries to the object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/triad.rb', line 45 def <<(array) raise InvalidAddition.new("your array length must be 3") if array.length != 3 array_key = array.fetch(0) raise InvalidAddition.new("the provided key must be a Symbol") unless array_key.is_a?(Symbol) raise InvalidAddition.new("the provided key already exists") if key_exists?(array_key) array_descriptor = array.fetch(1) raise InvalidAddition.new("the provided descriptor must be a String") unless array_descriptor.is_a?(String) array_value = array.fetch(2) storage[array_key] = [array_descriptor, array_value] self end |
#descriptors(arg = nil) ⇒ Object
Return the descriptors for a given key or value
27 28 29 30 31 32 33 |
# File 'lib/triad.rb', line 27 def descriptors(arg=nil) if arg.nil? storage.map{|_,(descriptor,_)| descriptor } else with_interest(arg).map{|_, descriptor, _| descriptor } end end |
#each ⇒ Object
65 66 67 68 69 |
# File 'lib/triad.rb', line 65 def each storage.each do |key, (descriptor, value)| yield key, descriptor, value end end |
#keys(arg = nil) ⇒ Object
Return the keys for a given descriptor or value
18 19 20 21 22 23 24 |
# File 'lib/triad.rb', line 18 def keys(arg=nil) if arg == nil storage.keys else with_interest(arg).map{|key, _, _| key } end end |
#update(key, descriptor, value) ⇒ Object
Alter the descriptor and value in-place for the given key
60 61 62 63 |
# File 'lib/triad.rb', line 60 def update(key, descriptor, value) raise InvalidAddition.new("the provided descriptor cannot be nil") if descriptor.nil? storage[key] = [descriptor, value] end |
#values(arg = :__no_argument_given__) ⇒ Object
Return the values for a given key or descriptor
36 37 38 39 40 41 42 |
# File 'lib/triad.rb', line 36 def values(arg=:__no_argument_given__) if arg == :__no_argument_given__ storage.map{|_,(_,value)| value }.uniq else with_interest(arg).map{|_, _, value| value } end end |