Sha256: e0a1de3cc55df837021e2961691650ced608283f04cec26456f15758f8e4c8ea

Contents?: true

Size: 577 Bytes

Versions: 20

Compression:

Stored size: 577 Bytes

Contents

require "virtus"

# confession: i love virtus' coercion.
module Disposable::Twin::Coercion
  module ClassMethods
    def property(name, options={}, &block)
      super(name, options, &block).tap do
        coercing_setter!(name, options[:type])  # define coercing setter after twin.
      end
    end

    def coercing_setter!(name, type)
      mod = Module.new do
        define_method("#{name}=") { |value| super Virtus::Attribute.build(type).coerce(value) }
      end
      include mod
    end
  end

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
disposable-0.2.6 lib/disposable/twin/coercion.rb
disposable-0.2.5 lib/disposable/twin/coercion.rb
disposable-0.2.4 lib/disposable/twin/coercion.rb
disposable-0.2.3 lib/disposable/twin/coercion.rb
disposable-0.2.2 lib/disposable/twin/coercion.rb
disposable-0.2.1 lib/disposable/twin/coercion.rb
disposable-0.1.15 lib/disposable/twin/coercion.rb
disposable-0.2.0 lib/disposable/twin/coercion.rb
disposable-0.2.0.rc2 lib/disposable/twin/coercion.rb
disposable-0.2.0.rc1 lib/disposable/twin/coercion.rb
disposable-0.1.14 lib/disposable/twin/coercion.rb
disposable-0.1.13 lib/disposable/twin/coercion.rb
disposable-0.1.12 lib/disposable/twin/coercion.rb
disposable-0.1.11 lib/disposable/twin/coercion.rb
disposable-0.1.9 lib/disposable/twin/coercion.rb
disposable-0.1.8 lib/disposable/twin/coercion.rb
disposable-0.1.7 lib/disposable/twin/coercion.rb
disposable-0.1.6 lib/disposable/twin/coercion.rb
disposable-0.1.5 lib/disposable/twin/coercion.rb
disposable-0.1.4 lib/disposable/twin/coercion.rb