Skip to content

Commit

Permalink
Merge pull request #343 from dslh/collection_value_coerced
Browse files Browse the repository at this point in the history
Virtus::Attribute::Collection.value_coerced? override.
  • Loading branch information
booch committed Feb 2, 2016
2 parents 3248a46 + de9224b commit 1db0d28
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/virtus/attribute/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def coerce(value)
end
end

# @api public
def value_coerced?(value)
super && value.all? { |item| member_type.value_coerced? item }
end

# @api private
def finalize
return self if finalized?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'
require 'set'

describe Virtus::Attribute::Collection, '#value_coerced?' do
subject { object.value_coerced?(input) }

let(:object) { described_class.build(Array[Integer]) }

context 'when input has correctly typed members' do
let(:input) { [1, 2, 3] }

it { is_expected.to be(true) }
end

context 'when input has incorrectly typed members' do
let(:input) { [1, 2, '3'] }

it { is_expected.to be(false) }
end

context 'when the collection type is incorrect' do
let(:input) { Set[1, 2, 3] }

it { is_expected.to be(false) }
end

context 'when the input is empty' do
let(:input) { [] }
it { is_expected.to be(true) }
end
end

0 comments on commit 1db0d28

Please sign in to comment.