Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unmarshallable keys #52

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.2] - 2024-08-22
### Fixed
- Fixed "TypeError: no _dump_data is defined for class Proc" error that ocurred when a definition model inherits from another model that uses lambda based definitions

## [1.1.1] - 2024-05-21
### Fixed
- Fixed Definition::Model inheritance
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.1)
definition (1.1.2)
activesupport
i18n

Expand Down
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
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.1"
VERSION = "1.1.2"
end
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
required :name, Definition.NonEmptyString
required :name, Definition.Lambda { |value| conform_with(value.trim) }

to have a test case that would cause the error that this pr fixes

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
Loading