Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collection Hash class for giving collection attributes more intelligence
- Loading branch information
Ethan Rowe
committed
May 4, 2010
1 parent
57e27db
commit e576056
Showing
5 changed files
with
88 additions
and
3 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,16 @@ | ||
require 'delegate' | ||
module SimpleMapper | ||
module Collection | ||
class Hash < DelegateClass(::Hash) | ||
attr_accessor :attribute | ||
|
||
def initialize(hash = {}) | ||
super(hash) | ||
end | ||
|
||
def build(*args) | ||
attribute.mapper.new(*args) | ||
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
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,52 @@ | ||
require 'test_helper' | ||
|
||
class CollectionHashTest < Test::Unit::TestCase | ||
context 'A SimpleMapper::Collection::Hash' do | ||
setup do | ||
@class = SimpleMapper::Collection::Hash | ||
end | ||
|
||
should 'be equivalent to a hash passed to the constructor' do | ||
hash = {:a => 'A', :b => 'B'} | ||
instance = @class.new(hash) | ||
assert_equal hash, instance | ||
assert_equal @class, instance.class | ||
end | ||
|
||
should 'allow hash-style lookups, assignments, etc.' do | ||
instance = @class.new | ||
assert_equal nil, instance[:foo] | ||
assert_equal :bar, instance[:foo] = :bar | ||
assert_equal :bar, instance[:foo] | ||
assert_equal true, instance.has_key?(:foo) | ||
assert_equal false, instance.has_key?(:blah) | ||
assert_equal :blee, instance[:boo] = :blee | ||
assert_equal :blee, instance[:boo] | ||
assert_equal([:boo, :foo], instance.keys.sort_by {|k| k.to_s}) | ||
end | ||
|
||
should 'have an :attribute attribute' do | ||
instance = @class.new | ||
assert_equal nil, instance.attribute | ||
attrib = stub('attribute_object') | ||
assert_equal attrib, instance.attribute = attrib | ||
assert_equal attrib, instance.attribute | ||
end | ||
|
||
should 'allow construction of new mapper instances through :build' do | ||
mapper = stub('mapper') | ||
attrib = stub('attribute_object', :mapper => mapper) | ||
seq = sequence('build invocations') | ||
mapper.expects(:new).with.in_sequence(seq).returns(:one) | ||
mapper.expects(:new).with(:a => 'A').in_sequence(seq).returns(:two) | ||
mapper.expects(:new).with(:foo, :bar, :a => 'A', :b => 'B').in_sequence(seq).returns(:three) | ||
instance = @class.new | ||
instance.attribute = attrib | ||
result = [] | ||
result << instance.build | ||
result << instance.build(:a => 'A') | ||
result << instance.build(:foo, :bar, :a => 'A', :b => 'B') | ||
assert_equal [:one, :two, :three], result | ||
end | ||
end | ||
end |