Skip to content

Commit

Permalink
Merge pull request #51 from Goltergaul/fix-model-inheritance
Browse files Browse the repository at this point in the history
Fix model inheritance
  • Loading branch information
erikpaperik committed May 21, 2024
2 parents ed349dd + 9c9d701 commit 9c77bd4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [1.1.1] - 2024-05-21
### Fixed
- Fixed Definition::Model inheritance

## [1.1.0] - 2023-11-22
### Changes
- Improved performance
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
definition (1.1.0)
definition (1.1.1)
activesupport
i18n

Expand Down
7 changes: 6 additions & 1 deletion lib/definition/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ def _define_attr_accessor(key)
end

def _definition
@_definition ||= ::Definition.Keys {}
@_definition ||= if superclass == ::Definition::Model
::Definition.Keys {}
else
# Create a deep copy of parent's definition
Marshal.load(Marshal.dump(superclass._definition))
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/definition/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Definition
VERSION = "1.1.0"
VERSION = "1.1.1"
end
55 changes: 55 additions & 0 deletions spec/lib/definition/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,61 @@
end.to raise_error(Definition::InvalidModelError, /bar/)
end
end

context "with inheritance" do
subject(:new) { child_test_model_class.new(**kwargs) }

let(:child_test_model_class) do
ParentModel = test_model_class
Class.new(ParentModel) do
required :age, Definition.Type(Integer)
optional :phone, Definition.Type(String)
end
end

context "with required keywords only" do
let(:kwargs) { { name: "John", age: 24 } }

it "instantiates the model" do
expect(new.name).to eq("John")
expect(new.email).to be_nil
expect(new.age).to eq(24)
expect(new.phone).to be_nil
end
end

context "with required and optional keywords" do
let(:kwargs) { { name: "John", email: "test@test.com", age: 24, phone: "+4312345" } }

it "instantiates the model" do
expect(new.name).to eq("John")
expect(new.email).to eq("test@test.com")
expect(new.age).to eq(24)
expect(new.phone).to eq("+4312345")
end
end

it "throws error when parent's required attributes are missing" do
expect do
child_test_model_class.new(age: 24)
end.to raise_error(Definition::InvalidModelError, /name/)
end

it "throws error when child's required attributes are missing" do
expect do
child_test_model_class.new(name: "John")
end.to raise_error(Definition::InvalidModelError, /age/)
end

it "doesn't change parent's defintion" do
expect do
Class.new(test_model_class) do
required :required_child_attribute, Definition.Type(Integer)
optional :optional_child_attribute, Definition.Type(String)
end
end.not_to(change { test_model_class._definition.keys })
end
end
end

describe ".to_h" do
Expand Down

0 comments on commit 9c77bd4

Please sign in to comment.