lib/triad.rb in triad-0.3.0 vs lib/triad.rb in triad-1.0.0

- old
+ new

@@ -12,67 +12,75 @@ @storage = Concurrent::Hash.new end attr_reader :storage private :storage - def keys(arg=:__no_argument_given__) - if arg == :__no_argument_given__ + # Return the keys for a given descriptor or value + def keys(arg=nil) + if arg == nil storage.keys else with_interest(arg).map{|key, _, _| key } end end - def descriptors(arg=:__no_argument_given__) - if arg == :__no_argument_given__ + # Return the descriptors for a given key or value + def descriptors(arg=nil) + if arg.nil? storage.map{|_,(descriptor,_)| descriptor } else with_interest(arg).map{|_, descriptor, _| descriptor } end end + # Return the values for a given key or descriptor 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 + # Add new entries to the object def <<(array) - array_key = array[0] 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[1] - array_value = array[2] + 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 + # Alter the descriptor and value in-place for the given key def update(key, descriptor, value) + raise InvalidAddition.new("the provided descriptor cannot be nil") if descriptor.nil? storage[key] = [descriptor, value] end - def each(&block) + def each storage.each do |key, (descriptor, value)| - block.call key, descriptor, value + yield key, descriptor, value end end private def key_exists?(key) - storage.has_key?(key) + storage.key?(key) end - + def descriptor_exists?(descriptor) - storage.values.map{|arr| arr[0] }.include?(descriptor) + storage.values.map{|arr| arr.fetch(0) }.include?(descriptor) end - + def value_exists?(value) - storage.values.map{|arr| arr[1] }.include?(value) + storage.values.map{|arr| arr.fetch(1) }.include?(value) end def with_interest(interest) position = case when key_exists?(interest) then 0