Sha256: 563cac815ca366eaa77dc36aeae9f52ac848db1da8ebc136d1a45e6ae8c681f0

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

require "dry-types"

module Disposable::Twin::Coercion
  module Types
    # NOTE: Use Dry.Types() instead. Beware, it exports strict types by default, for old behavior use Dry.Types(default: :nominal)
    DRY_MODULE =  Gem::Version.new(Dry::Types::VERSION) < Gem::Version.new("0.15.0") ? Dry::Types.module : Dry.Types()
    include DRY_MODULE
  end

  DRY_TYPES_VERSION = Gem::Version.new(Dry::Types::VERSION)
  DRY_TYPES_CONSTANT = DRY_TYPES_VERSION < Gem::Version.new("0.13.0") ? Types::Form : Types::Params

  module ClassMethods
    def property(name, options={}, &block)
      super(name, options, &block).tap do
        coercing_setter!(name, options[:type], options[:nilify]) if options[:type] || options[:nilify]
      end
    end

    def coercing_setter!(name, type, nilify=false)
      # TODO: remove nilily with next release (0.5) for new dry-type versions
      type = type ? (DRY_TYPES_CONSTANT::Nil | type) : DRY_TYPES_CONSTANT::Nil if nilify

      warn "DEPRECATION WARNING [Disposable]: nilify is deprecated and it will be removed with the next release" if nilify && DRY_TYPES_VERSION >= Gem::Version.new("1.0.0")

      mod = Module.new do
        define_method("#{name}=") do |value|
          super type.(value)
        end
      end
      include mod
    end
  end

  def self.included(includer)
    includer.extend ClassMethods
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
disposable-0.6.3 lib/disposable/twin/coercion.rb
disposable-0.6.2 lib/disposable/twin/coercion.rb
disposable-0.6.1 lib/disposable/twin/coercion.rb
disposable-0.6.0 lib/disposable/twin/coercion.rb
disposable-0.5.0 lib/disposable/twin/coercion.rb
disposable-0.4.7 lib/disposable/twin/coercion.rb