Sha256: d07589b08c000b9b7ff15fddfc0cc98888ed886de7edfe972ba70e550bc8d951

Contents?: true

Size: 812 Bytes

Versions: 1

Compression:

Stored size: 812 Bytes

Contents

module MiniCamel

  # A StrictDTO is stricter than a common open struct object.
  # It will throw an argument error if the key / method you ask for does not exist.
  class StrictDto < Dto

    def [](key)
      fetch(key)
    end

    def fetch(key, default: nil)
      if @table.has_key?(key)
        @table.fetch(key, default)
      else
        return default unless default.nil?
        raise_key_not_found!(key)
      end
    end

    def method_missing(method_name, *arguments, &block)
      if respond_to?(method_name) || method_name.to_s.end_with?('=')
        super
      else
        raise_key_not_found!(method_name)
      end
    end

    private

    def raise_key_not_found!(key)
      raise ArgumentError, "Method or key '#{key}' not found! Available keys: #{keys.join(', ')}"
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mini_camel-0.5.8 lib/mini_camel/strict_dto.rb