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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought experiment: is doing |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.