1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
require 'spec_helper'
describe 'custom attributes' do
before do
module Examples
class NoisyString < Virtus::Attribute
lazy true
def coerce(input)
input.to_s.upcase
end
end
class RegularExpression < Virtus::Attribute
primitive Regexp
end
class User
include Virtus
attribute :name, String
attribute :scream, NoisyString
attribute :expression, RegularExpression
end
end
end
subject { Examples::User.new }
specify 'allows you to define custom attributes' do
regexp = /awesome/
subject.expression = regexp
expect(subject.expression).to eq(regexp)
end
specify 'allows you to define coercion methods' do
subject.scream = 'welcome'
expect(subject.scream).to eq('WELCOME')
end
end
|