Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds MergeInitializer extension.

  • Loading branch information...
commit 82295cc65fa3138ef3ada34b18a3d74811c54133 1 parent 8bf5dde
Michael Bleigh authored
View
15 README.markdown
@@ -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
1  lib/hashie.rb
@@ -12,6 +12,7 @@ module Extensions
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
24 lib/hashie/extensions/merge_initializer.rb
@@ -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
20 spec/hashie/extensions/merge_initializer_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.