Sha256: da6f861e74824544ad7d8b9982fe097325cf6121445d997270c6d30dbe007fd3

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

require 'dry/types/options'

module Dry
  module Types
    module Decorator
      include Options

      attr_reader :type

      def initialize(type, *)
        super
        @type = type
      end

      def constructor
        type.constructor
      end

      def try(input, &block)
        type.try(input, &block)
      end

      def valid?(value)
        type.valid?(value)
      end

      def default?
        type.default?
      end

      def constrained?
        type.constrained?
      end

      def respond_to_missing?(meth, include_private = false)
        super || type.respond_to?(meth)
      end

      private

      def decorate?(response)
        response.kind_of?(type.class)
      end

      def method_missing(meth, *args, &block)
        if type.respond_to?(meth)
          response = type.__send__(meth, *args, &block)

          if decorate?(response)
            self.class.new(response, options)
          else
            response
          end
        else
          super
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-types-0.9.2 lib/dry/types/decorator.rb
dry-types-0.9.1 lib/dry/types/decorator.rb
dry-types-0.9.0 lib/dry/types/decorator.rb