Skip to content

Instantly share code, notes, and snippets.

@kzaitsev
Created March 26, 2016 00:09
Show Gist options
  • Save kzaitsev/1453c6e74bee451a658f to your computer and use it in GitHub Desktop.
Save kzaitsev/1453c6e74bee451a658f to your computer and use it in GitHub Desktop.
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