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.1"
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: ‘Descriptor’, value: 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: ‘Descriptor’, value: value}
12 13 14 15 16 |
# File 'lib/triad.rb', line 12 def initialize @storage = Concurrent::Hash.new @descriptor_index = Concurrent::Hash.new @value_index = Concurrent::Hash.new end |
Instance Method Details
#<<(array) ⇒ Object
Add new entries to the object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/triad.rb', line 48 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) add_to_storage(array_key, array_descriptor, array_value) self end |
#descriptors(arg = nil) ⇒ Object
Return the descriptors for a given key or value
30 31 32 33 34 35 36 |
# File 'lib/triad.rb', line 30 def descriptors(arg = nil) if arg.nil? storage.map { |_, data| data[:descriptor] } else with_interest(arg).map { |_, descriptor, _| descriptor } end end |
#each ⇒ Object
69 70 71 72 73 |
# File 'lib/triad.rb', line 69 def each storage.each do |key, data| yield key, data[:descriptor], data[:value] end end |
#keys(arg = nil) ⇒ Object
Return the keys for a given descriptor or value
21 22 23 24 25 26 27 |
# File 'lib/triad.rb', line 21 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
63 64 65 66 67 |
# File 'lib/triad.rb', line 63 def update(key, descriptor, value) raise InvalidAddition.new("the provided descriptor cannot be nil") if descriptor.nil? remove_from_indices(key) if storage.key?(key) add_to_storage(key, descriptor, value) end |
#values(arg = :__no_argument_given__) ⇒ Object
Return the values for a given key or descriptor
39 40 41 42 43 44 45 |
# File 'lib/triad.rb', line 39 def values(arg = :__no_argument_given__) if arg == :__no_argument_given__ value_index.keys else with_interest(arg).map { |_, _, value| value } end end |