Sha256: 486f1c4604ff519b606f55afd512b2baa126b89c2bc02963b1942efce7006ee3

Contents?: true

Size: 1.68 KB

Versions: 426

Compression:

Stored size: 1.68 KB

Contents

class Thor
  module CoreExt #:nodoc:

    # A hash with indifferent access and magic predicates.
    #
    #   hash = Thor::CoreExt::HashWithIndifferentAccess.new 'foo' => 'bar', 'baz' => 'bee', 'force' => true
    #
    #   hash[:foo]  #=> 'bar'
    #   hash['foo'] #=> 'bar'
    #   hash.foo?   #=> true
    #
    class HashWithIndifferentAccess < ::Hash #:nodoc:

      def initialize(hash={})
        super()
        hash.each do |key, value|
          self[convert_key(key)] = value
        end
      end

      def [](key)
        super(convert_key(key))
      end

      def []=(key, value)
        super(convert_key(key), value)
      end

      def delete(key)
        super(convert_key(key))
      end

      def values_at(*indices)
        indices.collect { |key| self[convert_key(key)] }
      end

      def merge(other)
        dup.merge!(other)
      end

      def merge!(other)
        other.each do |key, value|
          self[convert_key(key)] = value
        end
        self
      end

      protected

        def convert_key(key)
          key.is_a?(Symbol) ? key.to_s : key
        end

        # Magic predicates. For instance:
        #
        #   options.force?                  # => !!options['force']
        #   options.shebang                 # => "/usr/lib/local/ruby"
        #   options.test_framework?(:rspec) # => options[:test_framework] == :rspec
        #
        def method_missing(method, *args, &block)
          method = method.to_s
          if method =~ /^(\w+)\?$/
            if args.empty?
              !!self[$1]
            else
              self[$1] == args.first
            end
          else
            self[method]
          end
        end

    end
  end
end

Version data entries

426 entries across 357 versions & 32 rubygems

Version Path
engineyard-serverside-3.0.4 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-3.0.3 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.8.0 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.8.0.pre4 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.8.0.pre3 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.8.0.pre2 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.8.0.pre lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.7.0.pre lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.19 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.17 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.16 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.15.alpha1 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.15.cf2 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.15.pre lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
arcabouco-0.2.13 vendor/bundle/gems/thor-0.14.6/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.14 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.13 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.12 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.12.prewut5 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb
engineyard-serverside-2.6.12.prewut4 lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb