Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Bleigh
committed
Aug 2, 2011
1 parent
8bf5dde
commit 82295cc
Showing
4 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Hashie | ||
module Extensions | ||
# The MergeInitializer 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. Note | ||
# that you can still provide a default value as a second | ||
# argument to the initializer. | ||
# | ||
# @example | ||
# class MyHash < Hash | ||
# include Hashie::Extensions::MergeInitializer | ||
# end | ||
# | ||
# h = MyHash.new(:abc => 'def') | ||
# h[:abc] # => 'def' | ||
# | ||
module MergeInitializer | ||
def initialize(hash = {}, default = nil, &block) | ||
super(default, &block) | ||
update(hash) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'spec_helper' | ||
|
||
describe Hashie::Extensions::MergeInitializer do | ||
class MergeInitializerHash < Hash; include Hashie::Extensions::MergeInitializer end | ||
subject{ MergeInitializerHash } | ||
|
||
it 'should initialize fine with no arguments' do | ||
subject.new.should == {} | ||
end | ||
|
||
it 'should initialize with a hash' do | ||
subject.new(:abc => 'def').should == {:abc => 'def'} | ||
end | ||
|
||
it 'should initialize with a hash and a default' do | ||
h = subject.new({:abc => 'def'}, 'bar') | ||
h[:foo].should == 'bar' | ||
h[:abc].should == 'def' | ||
end | ||
end |