Sha256: b89dd9eaaab8da2f199b9285c50cdd8f459ac8b8cd94cc1b8a0a1f5dd3690c67

Contents?: true

Size: 1.26 KB

Versions: 26

Compression:

Stored size: 1.26 KB

Contents

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/PerceivedComplexity

# 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 key?(string_key)
              string_key
            elsif 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.empty?

        self[key]
      end
    end

    def respond_to_missing?(method, include_private = false)
      string_key = method.to_s.sub(/=\z/, '')
      key?(string_key) || key?(string_key.to_sym) || super
    end
  end
end
# rubocop:enable all

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
adyen-ruby-api-library-10.1.2 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-10.1.1 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-10.1.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-10.0.1 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-10.0.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.9.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.8.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.7.1 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.7.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.6.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.5.2 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.5.1 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.5.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.3.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.2.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.1.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-9.0.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-8.0.1 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-8.0.0 lib/adyen/hash_with_accessors.rb
adyen-ruby-api-library-8.0.0.pre.beta.1 lib/adyen/hash_with_accessors.rb