Adds MergeInitializer extension.

This commit is contained in:
Michael Bleigh 2011-08-02 10:44:23 -05:00
parent 8bf5ddeec8
commit 82295cc65f
4 changed files with 55 additions and 5 deletions

View File

@ -20,7 +20,10 @@ below. This provides maximum flexibility for users to mix and match
functionality while maintaining feature parity with earlier versions of
Hashie.
### Hashie::Extensions::Coercion
Any of the extensions listed below can be mixed into a class by
`include`-ing `Hashie::Extensions::ExtensionName`.
### Coercion
Coercions allow you to set up "coercion rules" based either on the key
or the value type to massage data as it's being inserted into the Hash.
@ -53,14 +56,16 @@ Hash-like class that is self-propagating.
end
end
### Hashie::Extensions::KeyConversion
### KeyConversion
The KeyConversion extension gives you the convenience methods of
`symbolize_keys` and `stringify_keys` along with their bang
counterparts. You can also include just stringify or just symbolize with
`Hashie::Extensions::StringifyKeys` or `Hashie::Extensions::SymbolizeKeys`.
### Hashie::Extensions::MethodAccess
### MergeInitializer
### MethodAccess
The MethodAccess extension allows you to quickly build method-based
reading, writing, and querying into your Hash descendant. It can also be
@ -76,12 +81,12 @@ included as individual modules, i.e. `Hashie::Extensions::MethodReader`,
h.abc # => 'def'
h.abc? # => true
### Hashie::Extensions::DeepMerge (Unimplemented)
### DeepMerge (Unimplemented)
This extension *will* allow you to easily include a recursive merging
system to any Hash descendant.
### Hashie::Extensions::IndifferentAccess (Unimplemented)
### IndifferentAccess (Unimplemented)
This extension *will* allow you to easily give a hash rules for
normalizing keys, for instance to allow symbol or string keys both to

View File

@ -12,6 +12,7 @@ module Hashie
autoload :DeepMerge, 'hashie/extensions/deep_merge'
autoload :KeyConversion, 'hashie/extensions/key_conversion'
autoload :IndifferentAccess, 'hashie/extensions/indifferent_access'
autoload :MergeInitializer, 'hashie/extensions/merge_initializer'
autoload :MethodAccess, 'hashie/extensions/method_access'
autoload :MethodQuery, 'hashie/extensions/method_access'
autoload :MethodReader, 'hashie/extensions/method_access'

View File

@ -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

View File

@ -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