Skip to content

Commit 9c77bd4

Browse files
authored
Merge pull request #51 from Goltergaul/fix-model-inheritance
Fix model inheritance
2 parents ed349dd + 9c9d701 commit 9c77bd4

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9+
## [1.1.1] - 2024-05-21
10+
### Fixed
11+
- Fixed Definition::Model inheritance
12+
913
## [1.1.0] - 2023-11-22
1014
### Changes
1115
- Improved performance

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
definition (1.1.0)
4+
definition (1.1.1)
55
activesupport
66
i18n
77

lib/definition/model.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ def _define_attr_accessor(key)
3737
end
3838

3939
def _definition
40-
@_definition ||= ::Definition.Keys {}
40+
@_definition ||= if superclass == ::Definition::Model
41+
::Definition.Keys {}
42+
else
43+
# Create a deep copy of parent's definition
44+
Marshal.load(Marshal.dump(superclass._definition))
45+
end
4146
end
4247
end
4348

lib/definition/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Definition
4-
VERSION = "1.1.0"
4+
VERSION = "1.1.1"
55
end

spec/lib/definition/model_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,61 @@
131131
end.to raise_error(Definition::InvalidModelError, /bar/)
132132
end
133133
end
134+
135+
context "with inheritance" do
136+
subject(:new) { child_test_model_class.new(**kwargs) }
137+
138+
let(:child_test_model_class) do
139+
ParentModel = test_model_class
140+
Class.new(ParentModel) do
141+
required :age, Definition.Type(Integer)
142+
optional :phone, Definition.Type(String)
143+
end
144+
end
145+
146+
context "with required keywords only" do
147+
let(:kwargs) { { name: "John", age: 24 } }
148+
149+
it "instantiates the model" do
150+
expect(new.name).to eq("John")
151+
expect(new.email).to be_nil
152+
expect(new.age).to eq(24)
153+
expect(new.phone).to be_nil
154+
end
155+
end
156+
157+
context "with required and optional keywords" do
158+
let(:kwargs) { { name: "John", email: "test@test.com", age: 24, phone: "+4312345" } }
159+
160+
it "instantiates the model" do
161+
expect(new.name).to eq("John")
162+
expect(new.email).to eq("test@test.com")
163+
expect(new.age).to eq(24)
164+
expect(new.phone).to eq("+4312345")
165+
end
166+
end
167+
168+
it "throws error when parent's required attributes are missing" do
169+
expect do
170+
child_test_model_class.new(age: 24)
171+
end.to raise_error(Definition::InvalidModelError, /name/)
172+
end
173+
174+
it "throws error when child's required attributes are missing" do
175+
expect do
176+
child_test_model_class.new(name: "John")
177+
end.to raise_error(Definition::InvalidModelError, /age/)
178+
end
179+
180+
it "doesn't change parent's defintion" do
181+
expect do
182+
Class.new(test_model_class) do
183+
required :required_child_attribute, Definition.Type(Integer)
184+
optional :optional_child_attribute, Definition.Type(String)
185+
end
186+
end.not_to(change { test_model_class._definition.keys })
187+
end
188+
end
134189
end
135190

136191
describe ".to_h" do

0 commit comments

Comments
 (0)