-
-
Save kzaitsev/1453c6e74bee451a658f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class NoisyString | |
def coerce(value, options = {}) | |
value.to_s.upcase | |
end | |
end | |
class User | |
include ShallowAttributes | |
attribute :scream, NoisyString | |
attribute :screams, Array, of: NoisyString | |
end | |
class Member < User | |
def coerce(value, options = {}) | |
raise ArgumentError | |
end | |
end | |
class Organization | |
include ShallowAttributes | |
attribute :members, Array, of: Member, coerce: false | |
end | |
describe ShallowAttributes do | |
describe 'with custom attribute encapsulating configuration' do | |
let(:user) { User.new(scream: 'hello world!') } | |
it 'allow custom coercion' do | |
user.scream.must_equal "HELLO WORLD!" | |
end | |
it 'allow array of custom coercion' do | |
user.screams = %w[hello world!] | |
user.screams.must_equal %w[HELLO WORLD!] | |
end | |
end | |
describe 'with disabled coercion' do | |
let(:member) { Member.new(scream: 'hello world!') } | |
let(:organization) { Organization.new } | |
it 'allow to disable coercion' do | |
organization.members = [member] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment