lib/enumeration/collection.rb in enumeration-1.0.0 vs lib/enumeration/collection.rb in enumeration-1.1.0

- old
+ new

@@ -1,19 +1,34 @@ module Enumeration class Collection + attr_reader :data + def initialize(map_or_list) unless map_or_list.kind_of?(::Hash) || map_or_list.kind_of?(::Array) raise ArgumentError, "please specify the enum collection as a Hash or Array" end @data = map_or_list end - def [](value) - if self.map? && @data.has_key?(value) - @data[value] - elsif (self.map? && @data.has_value?(value)) || - (@data.include?(value)) + # lookup collection value by a key + def [](key) + if self.map? && @data.has_key?(key) + @data[key] + elsif (self.map? && @data.has_value?(key)) || + (self.list? && @data.include?(key)) + key + else + nil + end + end + + # lookup collection key by a value + def key(value) + if self.map? && @data.has_value?(value) + @data.index(value) + elsif (self.map? && @data.has_key?(value)) || + (self.list? && @data.include?(value)) value else nil end end \ No newline at end of file