Skip to content

Commit

Permalink
Allow Proc type as default as long as return value is hash
Browse files Browse the repository at this point in the history
* Make specs 1.8 compatible
  • Loading branch information
Michael Hibbs committed Feb 16, 2017
1 parent 4b07f6a commit 4873b7f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
15 changes: 9 additions & 6 deletions lib/structural/model/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ def value_of(data)
end

def default
value = super
valid_type_check(super)
end

private

if value.is_a? Hash
value
else
raise Structural::InvalidDefaultTypeError
end
def valid_type_check(v)
case v
when Hash then v
when Proc then valid_type_check(v.call)
else raise Structural::InvalidDefaultTypeError end
end
end
end
Expand Down
66 changes: 45 additions & 21 deletions spec/lib/structural/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
class NestedModel
include Structural::Model
field :yak
field :name, :default => nil
field :surname, :default => nil
end

class SeparateClass
def self.as_hash
{ :name => 'Michael' }
end
end

class TestModel
Expand All @@ -20,23 +28,18 @@ class TestModel
has_one :aliased_model, :type => NestedModel
has_one :nested_model, :key => 'aliased_model'
has_one :extra_nested_model
has_one :nested_model_with_invalid_default_type, default: nil
has_one :nested_model_with_default, default: { name: 'Michael' }
has_one :missing_nested_model_without_default
has_one :nested_with_invalid_default, :default => nil
has_one :nested_with_hash_default, :default => { name: 'Michael' }, :type => NestedModel
has_one :nested_with_hash_proc_default, :default => Proc.new { SeparateClass.as_hash }, :type => NestedModel
has_one :nested_with_invalid_proc_default, :default => Proc.new { SeparateClass.new }, :type => NestedModel
has_one :missing_nested_without_default
has_one :test_model
has_many :nested_models

class ExtraNestedModel
include Structural::Model
field :cats
end

class NestedModelWithDefault
include Structural::Model

field :name, default: nil
field :surname, default: nil
end
end

describe Structural::Model do
Expand Down Expand Up @@ -97,32 +100,53 @@ class NestedModelWithDefault
end

describe ".has_one" do
it "allows nested models" do
it 'allows nested models' do
model.aliased_model.should be_a NestedModel
end
it "allows nested models" do

it 'allows nested models' do
model.nested_model.should be_a NestedModel
model.nested_model.yak.should eq 11
end
it "allows associations to be nested within the class" do

it 'allows associations to be nested within the class' do
model.extra_nested_model.should be_a TestModel::ExtraNestedModel
model.extra_nested_model.cats.should eq 'MIAOW'
end
it "allows recursively defined models" do

it 'allows recursively defined models' do
model.test_model.should be_a TestModel
end
it "allows default values" do
model.nested_model_with_default.name.should eq 'Michael'
model.nested_model_with_default.surname.should be_nil

it 'allows default values as a hash' do
model.nested_with_hash_default.name.should eq 'Michael'
model.nested_with_hash_default.surname.should be_nil
end
it "fails if passed a non-hash as default" do

context 'when passing a Proc as a default' do
it 'allows the default' do
model.nested_with_hash_proc_default.name.should eq 'Michael'
model.nested_with_hash_default.surname.should be_nil
end

context 'when the Proc does not return a Hash' do
it 'raises an error' do
expect {
model.nested_with_invalid_proc_default
}.to raise_error(Structural::InvalidDefaultTypeError)
end
end
end

it 'fails if passed a non-hash as default' do
expect {
model.nested_model_with_invalid_default_type
model.nested_with_invalid_default
}.to raise_error(Structural::InvalidDefaultTypeError)
end
it "fails for missing associations without defaults" do

it 'fails for missing associations without defaults' do
expect {
model.missing_nested_model_without_default
model.missing_nested_without_default
}.to raise_error(Structural::MissingAttributeError)
end
end
Expand Down

0 comments on commit 4873b7f

Please sign in to comment.