Skip to content

Commit

Permalink
Adds MergeInitializer extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Aug 2, 2011
1 parent 8bf5dde commit 82295cc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
15 changes: 10 additions & 5 deletions README.markdown
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/hashie.rb
Expand Up @@ -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'
Expand Down
24 changes: 24 additions & 0 deletions 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
20 changes: 20 additions & 0 deletions 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

0 comments on commit 82295cc

Please sign in to comment.