Sha256: 2288f6b2e99554b13594c0fa929377bb09eb72ad56b6c39e51a18dfb3c09010a

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

module Labelizer
  class Container
    include Enumerable

    def initialize(keys, accept_array_key: false, &block)
      @data = {
        is_accept_array_key: accept_array_key,
      }

      case keys
      when Hash
        @keys = keys.keys
        @values = keys.values
        @map = keys.invert
      else
        @keys = keys
        @values = []
        @map = {}
      end
      @hash = Hash.new(&block)

      normalized_keys = @keys
      self.singleton_class.class_eval do
        normalized_keys.each do |key|
          define_method key do
            @hash[key]
          end
        end
      end
    end

    def [](key)
      return @hash[key] if @keys.include?(key)
      return @hash[@map[key]] if @values.include?(key)

      if @data[:is_accept_array_key]
        if key.respond_to?(:each)
          if key.all?{|k| @keys.include?(k)}
            return @hash[key]
          end
          if key.all?{|k| @values.include?(k)}
            return @hash[key.map{|k| @map[k]}]
          end
        end
      end

      raise KeyError, "key: #{key.inspect} not found"
    end
    def has_key?(key)
      @keys.include?(key) || @values.include?(key)
    end

    def each
      @keys.each do |key|
        yield key, @hash[key]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
labelizer-0.1.5 lib/labelizer/container.rb
labelizer-0.1.4 lib/labelizer/container.rb
labelizer-0.1.3 lib/labelizer/container.rb
labelizer-0.1.2 lib/labelizer/container.rb
labelizer-0.1.1 lib/labelizer/container.rb