Skip to content

Commit

Permalink
Manually dup Keys instead of marshalling/unmarshalling
Browse files Browse the repository at this point in the history
Marshalling/unmarshalling does not work when definitions cannot be marshalled (Lambda, for example).
  • Loading branch information
jan4843 authored and Goltergaul committed Aug 22, 2024
1 parent 221759f commit ed447b7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/definition/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def _definition
@_definition ||= if superclass == ::Definition::Model
::Definition.Keys {}
else
# Create a deep copy of parent's definition
Marshal.load(Marshal.dump(superclass._definition))
superclass._definition.dup
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/definition/types/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def initialize(name, req: {}, opt: {}, defaults: {}, options: {})
self.ignore_extra_keys = options.fetch(:ignore_extra_keys, false)
end

def initialize_dup(_other)
super
self.required_definitions = required_definitions.dup
self.optional_definitions = optional_definitions.dup
self.defaults = defaults.dup
end

def conform(input_value) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
# input_value is duplicated because we don't want to modify the user object that is passed into this function.
# The following logic will iterate over each definition and delete the key associated with the definition from
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/definition/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe Definition::Model do
let(:test_model_class) do
Class.new(described_class) do
required :name, Definition.Type(String)
required :name, Definition.NonEmptyString
optional :email, Definition.Type(String)
end
end
Expand Down Expand Up @@ -177,7 +177,7 @@
end.to raise_error(Definition::InvalidModelError, /age/)
end

it "doesn't change parent's defintion" do
it "doesn't change parent's definition" do
expect do
Class.new(test_model_class) do
required :required_child_attribute, Definition.Type(Integer)
Expand Down
32 changes: 32 additions & 0 deletions spec/lib/definition/types/keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,36 @@
end
end
end

describe ".dup" do
it "dups definitions and defaults" do
original = described_class.new("person",
req: {
name: Definition.Type(String)
},
opt: {
age: Definition.Type(Integer),
authorized: Definition.Boolean
},
defaults: {
authorized: false
})
copy = original.dup

original.required_definitions.clear
original.optional_definitions.clear
original.defaults.clear

value = {
name: "John",
age: 18
}
expected = {
name: "John",
age: 18,
authorized: false
}
expect(copy.conform(value)).to conform_with(expected)
end
end
end

0 comments on commit ed447b7

Please sign in to comment.