All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gems.virtus-1.0.5.spec.integration.injectible_coercers_spec.rb Maven / Gradle / Ivy

The newest version!
require 'virtus'

describe 'Injectible coercer' do
  before do
    module Examples
      class EmailAddress
        include Virtus.value_object

        values do
          attribute :address, String, :coercer => lambda { |add| add.downcase }
        end

        def self.coerce(input)
          if input.is_a?(String)
            new(:address => input)
          else
            new(input)
          end
        end
      end

      class User
        include Virtus.model

        attribute :email, EmailAddress,
          :coercer => lambda { |input| Examples::EmailAddress.coerce(input) }
      end
    end
  end

  after do
    Examples.send(:remove_const, :EmailAddress)
    Examples.send(:remove_const, :User)
  end

  let(:doe) { Examples::EmailAddress.new(:address => '[email protected]') }

  it 'accepts an email hash' do
    user = Examples::User.new :email => { :address => '[email protected]' }
    expect(user.email).to eq(doe)
  end

  it 'coerces an embedded string' do
    user = Examples::User.new :email => '[email protected]'
    expect(user.email).to eq(doe)
  end

end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy