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
Showing
9 changed files
with
91 additions
and
87 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,44 @@ | ||
module Temple | ||
class ImmutableHash | ||
include Enumerable | ||
|
||
def initialize(*hash) | ||
@hash = hash.compact | ||
end | ||
|
||
def include?(key) | ||
@hash.any? {|h| h.include?(key) } | ||
end | ||
|
||
def [](key) | ||
@hash.each {|h| return h[key] if h.include?(key) } | ||
nil | ||
end | ||
|
||
def each | ||
keys.each {|k| yield(k, self[k]) } | ||
end | ||
|
||
def keys | ||
@hash.inject([]) {|keys, h| keys += h.keys }.uniq | ||
end | ||
|
||
def values | ||
keys.map {|k| self[k] } | ||
end | ||
end | ||
|
||
class MutableHash < ImmutableHash | ||
def initialize(*hash) | ||
super({}, *hash) | ||
end | ||
|
||
def []=(key, value) | ||
@hash.first[key] = value | ||
end | ||
|
||
def update(hash) | ||
@hash.first.update(hash) | ||
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
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,39 @@ | ||
require 'helper' | ||
|
||
describe Temple::ImmutableHash do | ||
it 'has read accessor' do | ||
hash = Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}) | ||
hash[:a].should.equal 1 | ||
hash[:b].should.equal 2 | ||
end | ||
|
||
it 'has include?' do | ||
hash = Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}) | ||
hash.should.include :a | ||
hash.should.include :b | ||
hash.should.not.include :c | ||
end | ||
|
||
it 'has values' do | ||
Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).values.sort.should.equal [1,2] | ||
end | ||
|
||
it 'has keys' do | ||
Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).keys.should.equal [:a,:b] | ||
end | ||
|
||
it 'has to_a' do | ||
Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).to_a.should.equal [[:a, 1], [:b, 2]] | ||
end | ||
end | ||
|
||
describe Temple::MutableHash do | ||
it 'has write accessor' do | ||
parent = {:a => 1} | ||
hash = Temple::MutableHash.new(parent) | ||
hash[:a].should.equal 1 | ||
hash[:a] = 2 | ||
hash[:a].should.equal 2 | ||
parent[:a].should.equal 1 | ||
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