Sha256: 4e2f5c493c48377a91c0d2bad3a58bc576d6012406c3ed24b73829b7495b8d00

Contents?: true

Size: 1.95 KB

Versions: 13

Compression:

Stored size: 1.95 KB

Contents

module Oj

  # A Hash subclass that normalizes the hash keys to allow lookup by the
  # key.to_s or key.to_sym. It also supports looking up hash values by methods
  # that match the keys.
  class EasyHash < Hash

    # Replaces the Object.respond_to?() method.
    # @param [Symbol] m method symbol
    # @param [Boolean] include_all whether to include private and protected methods in the search
    # @return [Boolean] true for any method that matches an instance
    #                   variable reader, otherwise false.
    def respond_to?(m, include_all = false)
      return true if super
      return true if has_key?(m)
      return true if has_key?(m.to_s)

      has_key?(m.to_sym)
    end

    def [](key)
      return fetch(key, nil) if has_key?(key)
      return fetch(key.to_s, nil) if has_key?(key.to_s)

      fetch(key.to_sym, nil)
    end

    # Handles requests for Hash values. Others cause an Exception to be raised.
    # @param [Symbol|String] m method symbol
    # @return [Boolean] the value of the specified instance variable.
    # @raise [ArgumentError] if an argument is given. Zero arguments expected.
    # @raise [NoMethodError] if the instance variable is not defined.
    def method_missing(m, *args, &block)
      if m.to_s.end_with?('=')
        raise ArgumentError.new("wrong number of arguments (#{args.size} for 1 with #{m}) to method #{m}") if args.nil? or 1 != args.length

        m = m[0..-2]
        return store(m.to_s, args[0]) if has_key?(m.to_s)
        return store(m.to_sym, args[0]) if has_key?(m.to_sym)

        return store(m, args[0])
      else
        raise ArgumentError.new("wrong number of arguments (#{args.size} for 0 with #{m}) to method #{m}") unless args.nil? or args.empty?
        return fetch(m, nil) if has_key?(m)
        return fetch(m.to_s, nil) if has_key?(m.to_s)
        return fetch(m.to_sym, nil) if has_key?(m.to_sym)
      end
      raise NoMethodError.new("undefined method #{m}", m)
    end

  end # EasyHash
end # Oj

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
oj-3.16.9 lib/oj/easy_hash.rb
oj-3.16.8 lib/oj/easy_hash.rb
oj-3.16.7 lib/oj/easy_hash.rb
oj-3.16.6 lib/oj/easy_hash.rb
oj-3.16.5 lib/oj/easy_hash.rb
oj-3.16.4 lib/oj/easy_hash.rb
oj-3.16.3 lib/oj/easy_hash.rb
oj-3.16.2 lib/oj/easy_hash.rb
oj-3.16.1 lib/oj/easy_hash.rb
oj-3.16.0 lib/oj/easy_hash.rb
oj-3.15.1 lib/oj/easy_hash.rb
oj-3.15.0 lib/oj/easy_hash.rb
oj-3.14.3 lib/oj/easy_hash.rb