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

Deep Merge Initializer Extension #382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -13,7 +13,7 @@ scheme are considered to be bugs.
### Added

* [#381](https://github.com/intridea/hashie/pull/381): Add a logging layer that lets us report potential issues to our users. As the first logged issue, report when a `Hashie::Mash` is attempting to overwrite a built-in method, since that is one of our number one questions - [@michaelherold](https://github.com/michaelherold).
* Your contribution here.
* [#382](https://github.com/intridea/hashie/pull/382): `DeepMergeInitializer` extension - [@sazor](https://github.com/sazor).
Copy link
Member

Choose a reason for hiding this comment

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

Please make sure to restore the "Your contribution here." line.


### Changed

Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -206,6 +206,10 @@ Hashie.stringify_keys hash # => Returns a copy of hash with keys stringified.

The MergeInitializer extension simply makes it possible to initialize a Hash subclass with another Hash, giving you a quick short-hand.

### DeepMergeInitializer

The DeepMergeInitializer acts the same as MergeInitializer but also cast all nested hashes to extendable class.
Copy link
Member

Choose a reason for hiding this comment

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

Please place the class names in backticks.


### MethodAccess

The MethodAccess extension allows you to quickly build method-based reading, writing, and querying into your Hash descendant. It can also be included as individual modules, i.e. `Hashie::Extensions::MethodReader`, `Hashie::Extensions::MethodWriter` and `Hashie::Extensions::MethodQuery`.
Expand Down
1 change: 1 addition & 0 deletions lib/hashie.rb
Expand Up @@ -47,6 +47,7 @@ module Extensions
autoload :StrictKeyAccess, 'hashie/extensions/strict_key_access'
autoload :RubyVersion, 'hashie/extensions/ruby_version'
autoload :RubyVersionCheck, 'hashie/extensions/ruby_version_check'
autoload :DeepMergeInitializer, 'hashie/extensions/deep_merge_initializer'

module Parsers
autoload :YamlErbParser, 'hashie/extensions/parsers/yaml_erb_parser'
Expand Down
37 changes: 37 additions & 0 deletions lib/hashie/extensions/deep_merge_initializer.rb
@@ -0,0 +1,37 @@
module Hashie
module Extensions
# The DeepMergeInitializer is a super-simple mixin that allows
# you to initialize a subclass of Hash with another Hash
# to give you faster startup time for Hash subclasses.
# It's almost the same as MergeInitializer but nested hashes
# are same type as main object. Note
# that you can still provide a default value as a second
# argument to the initializer.
#
# @example
# class MyHash < Hash
# include Hashie::Extensions::DeepMergeInitializer
# end
#
# h = MyHash.new(abc: 'def', hashy: { abc: 'def' })
# h[:abc] # => 'def'
# h[:hashy].class # => MyHash
#
module DeepMergeInitializer
def self.included(base)
base.class_eval do
Copy link
Member

Choose a reason for hiding this comment

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

Thought experiment: is doing class_eval the right thing here? Would it be better to have initialize be an instance method that gets included?

def initialize(hash = {}, default = nil, &block)
default ? super(default) : super(&block)
hash.each do |key, value|
self[key] = if value.is_a?(::Hash)
self.class.new(value, default, &block)
else
value
end
end
end
end
end
end
end
end
15 changes: 15 additions & 0 deletions spec/hashie/extensions/deep_merge_initializer_spec.rb
@@ -0,0 +1,15 @@
require 'spec_helper'

describe Hashie::Extensions::DeepMergeInitializer do
class DeepInitializerHash < Hash
include Hashie::Extensions::DeepMergeInitializer
end

subject { DeepInitializerHash }

it 'creates nested hash with the same type as parent hash' do
s = subject.new({ a: :b, hash: { c: :d } })[:hash]
pp s.class

Choose a reason for hiding this comment

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

pp ?

expect(subject.new({ a: :b, hash: { c: :d } })[:hash]).to be_a(subject)
end
end