Skip to content

Commit

Permalink
Merge pull request #149 from dry-rb/rollback-to_hash-deprecation
Browse files Browse the repository at this point in the history
Rollback to_hash deprecation
  • Loading branch information
flash-gordon committed Feb 2, 2020
2 parents 2bcc99a + 2cbbf72 commit 65f8d35
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/dry/struct.rb
Expand Up @@ -159,7 +159,8 @@ def to_h
result[key.name] = Hashify[self[key.name]] if attributes.key?(key.name)
end
end
deprecate :to_hash, :to_h, message: "Implicit convertion structs to hashes is deprecated. Use .to_h"
alias_method :to_hash, :to_h
# deprecate :to_hash, :to_h, message: "Implicit convertion structs to hashes is deprecated. Use .to_h"

# Create a copy of {Dry::Struct} with overriden attributes
#
Expand Down
4 changes: 2 additions & 2 deletions lib/dry/struct/class_interface.rb
Expand Up @@ -247,7 +247,7 @@ def check_schema_duplication(new_keys)

# @param [Hash{Symbol => Object},Dry::Struct] attributes
# @raise [Struct::Error] if the given attributes don't conform {#schema}
def new(attributes = default_attributes, safe = false)
def new(attributes = default_attributes, safe = false, &block)
if attributes.is_a?(Struct)
if equal?(attributes.class)
attributes
Expand All @@ -257,7 +257,7 @@ def new(attributes = default_attributes, safe = false)
# User.new(super_user)
#
# We may deprecate this behavior in future forcing people to be explicit
new(attributes.to_h, safe)
new(attributes.to_h, safe, &block)
end
elsif safe
load(schema.call_safe(attributes) { |output = attributes| return yield output })
Expand Down
9 changes: 9 additions & 0 deletions spec/integration/struct_spec.rb
Expand Up @@ -56,6 +56,15 @@ def construct_user(attributes)
expect(construct_user(user)).to be_instance_of(user_type)
end

it 'supports safe call when a struct is given' do
subtype = Class.new(root_type) { attribute :age, 'string' }
user = subtype[
name: :Jane, age: 'twenty-one', root: true, address: { city: 'NYC', zipcode: 123 }
]

expect(root_type.new(user, true) { :fallback }).to be(:fallback)
end

context 'with default' do
it 'resolves missing values with defaults' do
struct = Class.new(Dry::Struct) do
Expand Down

0 comments on commit 65f8d35

Please sign in to comment.