Sha256: 6ba10d9f1afce53f13aea9befc334ee39d54c93135a8344bc82762567063fcbf

Contents?: true

Size: 1.12 KB

Versions: 14

Compression:

Stored size: 1.12 KB

Contents

# This utility method inherits from Hash, but allows keys to be read
# and updated with dot notation. Usage is entirely optional (i.e.,  hash values
# can still be accessed via symbol and string keys).
#
# Based on: https://gist.github.com/winfred/2185384#file-ruby-dot-hash-access-rb
module Adyen
  class HashWithAccessors < Hash
    def method_missing(method, *args)
      string_key = method.to_s.sub(/=\z/, '')
      sym_key = string_key.to_sym

      key = if has_key?(string_key)
        string_key
      elsif has_key?(sym_key)
        sym_key
      end

      return super unless key

      assignment = sym_key != method

      if assignment
        raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1)" unless args.size == 1

        self[key] = args.first
      else
        raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)" unless args.size == 0

        self[key]
      end
    end

    def respond_to_missing?(method, include_private = false)
      string_key = method.to_s.sub(/=\z/, '')
      has_key?(string_key) || has_key?(string_key.to_sym) || super
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
adyen-ruby-api-library-7.0.1 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-7.0.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-6.3.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-6.2.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-6.1.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-6.0.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-5.1.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-5.0.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-4.4.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-4.3.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-4.2.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-4.1.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-4.0.2 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-4.0.1 lib/adyen/hash_with_accessors.rb