Sha256: 972fffeb01937b016728d0428d6ee201e38b5f2ee1de99ec3cbb4fc7ff5c8b58

Contents?: true

Size: 865 Bytes

Versions: 1

Compression:

Stored size: 865 Bytes

Contents

require "dry-types"

module Disposable::Twin::Coercion
  module Types
    include Dry::Types.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)
     type = type ? (DRY_TYPES_CONSTANT::Nil | type) : DRY_TYPES_CONSTANT::Nil if nilify

      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

1 entries across 1 versions & 1 rubygems

Version Path
disposable-0.4.5 lib/disposable/twin/coercion.rb