Sha256: 577989e5a4c6aea2c0a5a42c438ebd37ff862a6eafb75ccaee0f626a816fd381

Contents?: true

Size: 896 Bytes

Versions: 4

Compression:

Stored size: 896 Bytes

Contents

require './spec/spec_helper'

class MD5 < Virtus::Attribute::Object
  primitive       String
  coercion_method :to_md5
end

module Virtus
  class Coercion
    class String < Virtus::Coercion::Object
      def self.to_md5(value)
        Digest::MD5.hexdigest(value)
      end
    end
  end
end

class User
  include Virtus

  attribute :name,     String
  attribute :password, MD5
end

describe User do
  it { should respond_to(:name)      }
  it { should respond_to(:name=)     }
  it { should respond_to(:password)  }
  it { should respond_to(:password=) }

  describe '#name=' do
    let(:value) { 'Piotr' }

    before do
      subject.name = value
    end

    its(:name) { should == value }
  end

  describe '#password=' do
    let(:value) { 'foobar' }

    before do
      subject.password = value
    end

    its(:password) { should == Virtus::Coercion::String.to_md5(value) }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
virtus-0.0.9 examples/custom_coercion_spec.rb
virtus-0.0.8 examples/custom_coercion_spec.rb
virtus-0.0.7 examples/custom_coercion_spec.rb
virtus-0.0.6 examples/custom_coercion_spec.rb