Skip to content

Commit

Permalink
Set strict type for member/key/value types too
Browse files Browse the repository at this point in the history
fixes #280
  • Loading branch information
solnic committed Jul 24, 2014
1 parent d238b7e commit ab1a0d5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/virtus/attribute/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def self.build_type(definition)

# @api private
def self.merge_options!(type, options)
options[:member_type] ||= Attribute.build(type.member_type)
options[:member_type] ||= Attribute.build(type.member_type, strict: options[:strict])
end

# @api public
Expand Down
6 changes: 3 additions & 3 deletions lib/virtus/attribute/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def self.infer_key_and_value_types(type)
value_type
end

{ :key_type => key_primitive, :value_type => value_primitive}
{ :key_type => key_primitive, :value_type => value_primitive}
end
end

Expand All @@ -92,8 +92,8 @@ def self.build_type(definition)

# @api private
def self.merge_options!(type, options)
options[:key_type] ||= Attribute.build(type.key_type)
options[:value_type] ||= Attribute.build(type.value_type)
options[:key_type] ||= Attribute.build(type.key_type, :strict => options[:strict])
options[:value_type] ||= Attribute.build(type.value_type, :strict => options[:strict])
end

# Coerce members
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'spec_helper'

describe Virtus::Attribute, '.build' do
subject { described_class.build(type) }
subject { described_class.build(type, options) }

let(:options) { {} }

shared_examples_for 'a valid collection attribute instance' do
it { should be_instance_of(Virtus::Attribute::Collection) }
Expand Down Expand Up @@ -91,4 +93,13 @@
expect(subject.type.member_type).to be(Axiom::Types::String)
end
end

context 'when strict mode is used' do
let(:type) { Array[String] }
let(:options) { { strict: true } }

it 'sets strict mode for member type' do
expect(subject.member_type).to be_strict
end
end
end
14 changes: 13 additions & 1 deletion spec/unit/virtus/attribute/hash/class_methods/build_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'spec_helper'

describe Virtus::Attribute::Hash, '.build' do
subject { described_class.build(type) }
subject { described_class.build(type, options) }

let(:options) { {} }

shared_examples_for 'a valid hash attribute instance' do
it { should be_instance_of(Virtus::Attribute::Hash) }
Expand Down Expand Up @@ -91,4 +93,14 @@
)
end
end

context 'when strict mode is used' do
let(:type) { Hash[String => Integer] }
let(:options) { { :strict => true } }

it 'sets the strict mode for key/value types' do
expect(subject.key_type).to be_strict
expect(subject.value_type).to be_strict
end
end
end

0 comments on commit ab1a0d5

Please sign in to comment.