lib/enumeration/collection.rb in enumeration-1.3.1 vs lib/enumeration/collection.rb in enumeration-1.3.2

- old
+ new

@@ -1,49 +1,52 @@ -module Enumeration; end -class Enumeration::Collection +module Enumeration - attr_reader :data + class Collection - 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" + 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 - @data = map_or_list - end - # 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 + # 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 - end - # lookup collection key by a value - def key(value) - if self.map? && @data.has_value?(value) - @data.invert[value] - elsif (self.map? && @data.has_key?(value)) || - (self.list? && @data.include?(value)) - value - else - nil + # lookup collection key by a value + def key(value) + if self.map? && @data.has_value?(value) + @data.invert[value] + elsif (self.map? && @data.has_key?(value)) || + (self.list? && @data.include?(value)) + value + else + nil + end end - end - def list? - @data.kind_of?(::Array) - end + def list? + @data.kind_of?(::Array) + end - def map? - @data.kind_of?(::Hash) - end + def map? + @data.kind_of?(::Hash) + end - def set - self.map? ? @data.keys : @data + def set + self.map? ? @data.keys : @data + end + end end