From ffd01534271f54e59a009cd93948c78bae797a1e Mon Sep 17 00:00:00 2001 From: Benedikt Deicke Date: Thu, 2 Apr 2020 11:48:11 +0200 Subject: [PATCH] Allow overrides to be `nil` This fixes a problem where attribute methods would be treated as missing when their override value was set to `nil`. Instead of relying on the value of the attribute, this commit changes the behavior to use `fetch` on the attributes hash. Only a truly missing attribute will now trigger the `super` call in `method_missing`. --- lib/fabrication/generator/base.rb | 2 +- spec/fabrication/generator/base_spec.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/fabrication/generator/base.rb b/lib/fabrication/generator/base.rb index 0bea2d55..d06eeef7 100644 --- a/lib/fabrication/generator/base.rb +++ b/lib/fabrication/generator/base.rb @@ -76,7 +76,7 @@ def initialize(klass) end def method_missing(method_name, *args, &block) - _attributes[method_name] || super + _attributes.fetch(method_name) { super } end protected diff --git a/spec/fabrication/generator/base_spec.rb b/spec/fabrication/generator/base_spec.rb index fa1356ed..7d04157e 100644 --- a/spec/fabrication/generator/base_spec.rb +++ b/spec/fabrication/generator/base_spec.rb @@ -85,7 +85,7 @@ let(:schematic) do Fabrication::Schematic::Definition.new('ClassWithInit') do arg1 10 - initialize_with { Struct.new(:arg1, :arg2).new(arg1, arg1 + 10) } + initialize_with { Struct.new(:arg1, :arg2).new(arg1, arg1.to_i + 10) } end end @@ -104,6 +104,13 @@ end end + context "with nil override" do + subject { schematic.fabricate(arg1: nil) } + it "saves the return value of the block as instance" do + expect(subject.arg1).to eq(nil) + expect(subject.arg2).to eq(10) + end + end end end