Skip to content

Commit

Permalink
move hashes out of Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Apr 30, 2011
1 parent c850ad5 commit 58232f1
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 87 deletions.
2 changes: 2 additions & 0 deletions lib/temple.rb
Expand Up @@ -9,6 +9,8 @@ module Temple
autoload :Filter, 'temple/filter'
autoload :Templates, 'temple/templates'
autoload :Grammar, 'temple/grammar'
autoload :ImmutableHash, 'temple/hash'
autoload :MutableHash, 'temple/hash'

module Mixins
autoload :Dispatcher, 'temple/mixins/dispatcher'
Expand Down
2 changes: 1 addition & 1 deletion lib/temple/engine.rb
Expand Up @@ -57,7 +57,7 @@ def build_chain
case filter
when Class
filtered_options = Hash[*option_filter.select {|k| options.include?(k) }.map {|k| [k, options[k]] }.flatten]
filter.new(Utils::ImmutableHash.new(local_options, filtered_options))
filter.new(ImmutableHash.new(local_options, filtered_options))
when UnboundMethod
filter.bind(self)
else
Expand Down
44 changes: 44 additions & 0 deletions lib/temple/hash.rb
@@ -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
6 changes: 3 additions & 3 deletions lib/temple/mixins/options.rb
Expand Up @@ -6,8 +6,8 @@ def set_default_options(options)
end

def default_options
@default_options ||= Utils::MutableHash.new(superclass.respond_to?(:default_options) ?
superclass.default_options : nil)
@default_options ||= MutableHash.new(superclass.respond_to?(:default_options) ?
superclass.default_options : nil)
end
end

Expand All @@ -19,7 +19,7 @@ def self.included(base)
attr_reader :options

def initialize(options = {})
@options = Utils::ImmutableHash.new(options, self.class.default_options)
@options = ImmutableHash.new(options, self.class.default_options)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/temple/mixins/template.rb
Expand Up @@ -11,7 +11,7 @@ def engine(engine = nil)
def build_engine(*options)
raise 'No engine configured' unless engine
options << default_options
engine.new(Utils::ImmutableHash.new(*options)) do |e|
engine.new(ImmutableHash.new(*options)) do |e|
chain.each {|block| e.instance_eval(&block) }
end
end
Expand Down
43 changes: 0 additions & 43 deletions lib/temple/utils.rb
Expand Up @@ -2,49 +2,6 @@ module Temple
module Utils
extend self

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

# Returns an escaped copy of `html`.
# Strings which are declared as html_safe are not escaped.
#
Expand Down
2 changes: 1 addition & 1 deletion test/test_filter.rb
Expand Up @@ -10,7 +10,7 @@ def on_test(arg)
it 'should support options' do
Temple::Filter.should.respond_to :default_options
Temple::Filter.should.respond_to :set_default_options
Temple::Filter.new.options.should.be.instance_of Temple::Utils::ImmutableHash
Temple::Filter.new.options.should.be.instance_of Temple::ImmutableHash
Temple::Filter.new(:key => 3).options[:key].should.equal 3
end

Expand Down
39 changes: 39 additions & 0 deletions test/test_hash.rb
@@ -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
38 changes: 0 additions & 38 deletions test/test_utils.rb
Expand Up @@ -37,41 +37,3 @@ class UniqueTest
end
end
end

describe Temple::Utils::ImmutableHash do
it 'has read accessor' do
hash = Temple::Utils::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::Utils::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::Utils::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).values.sort.should.equal [1,2]
end

it 'has keys' do
Temple::Utils::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).keys.should.equal [:a,:b]
end

it 'has to_a' do
Temple::Utils::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).to_a.should.equal [[:a, 1], [:b, 2]]
end
end

describe Temple::Utils::MutableHash do
it 'has write accessor' do
parent = {:a => 1}
hash = Temple::Utils::MutableHash.new(parent)
hash[:a].should.equal 1
hash[:a] = 2
hash[:a].should.equal 2
parent[:a].should.equal 1
end
end

0 comments on commit 58232f1

Please sign in to comment.