Sha256: 9a23de1237318e875b06f052df7cb78cad5a26769f8f0e09df2df1e7cb23bfca

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

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

    # 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.invert[value]
      elsif (self.map?  && @data.has_key?(value)) ||
            (self.list? && @data.include?(value))
        value
      else
        nil
      end
    end

    def list?
      @data.kind_of?(::Array)
    end

    def map?
      @data.kind_of?(::Hash)
    end

    def set
      self.map? ? @data.keys : @data
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
enumeration-1.3.3 lib/enumeration/collection.rb
enumeration-1.3.2 lib/enumeration/collection.rb