class Enumeration::Collection

Attributes

data[R]

Public Class Methods

new(map_or_list) click to toggle source
# File lib/enumeration/collection.rb, line 7
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

Public Instance Methods

[](key) click to toggle source

lookup collection value by a key

# File lib/enumeration/collection.rb, line 15
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
key(value) click to toggle source

lookup collection key by a value

# File lib/enumeration/collection.rb, line 27
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
list?() click to toggle source
# File lib/enumeration/collection.rb, line 38
def list?
  @data.kind_of?(::Array)
end
map?() click to toggle source
# File lib/enumeration/collection.rb, line 42
def map?
  @data.kind_of?(::Hash)
end
set() click to toggle source
# File lib/enumeration/collection.rb, line 46
def set
  self.map? ? @data.keys : @data
end