Sha256: f7504d20927a22dc6fbb1f67316ff8235f5627d2584e38bacbacf8bb2a0ab141

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Pragma
  module Contract
    module Coercion
      def self.included(klass)
        klass.extend ClassMethods
      end

      module ClassMethods
        def strict(type)
          build_type 'Strict', type
        end

        def coercible(type)
          build_type 'Coercible', type
        end

        def form(type)
          # Dry::Types 0.13 renames Types::Form to Types::Params and the Int type to Integer.

          if Dry::Types['form.int']
            build_type 'Form', type
          else
            build_type 'Params', type.to_sym == :int ? :integer : type
          end
        end

        def json(type)
          build_type 'Json', type
        end

        def maybe_strict(type)
          build_type 'Maybe::Strict', type
        end

        def maybe_coercible(type)
          build_type 'Maybe::Coercible', type
        end

        def build_type(namespace, type)
          Object.const_get "Pragma::Contract::Types::#{namespace}::#{type.to_s.capitalize}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pragma-contract-2.1.1 lib/pragma/contract/coercion.rb
pragma-contract-2.0.1 lib/pragma/contract/coercion.rb