diff --git a/lib/temple.rb b/lib/temple.rb index caf54e5..4312794 100644 --- a/lib/temple.rb +++ b/lib/temple.rb @@ -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' diff --git a/lib/temple/engine.rb b/lib/temple/engine.rb index 4c6d135..2b041ce 100644 --- a/lib/temple/engine.rb +++ b/lib/temple/engine.rb @@ -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 diff --git a/lib/temple/hash.rb b/lib/temple/hash.rb new file mode 100644 index 0000000..d666a14 --- /dev/null +++ b/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 diff --git a/lib/temple/mixins/options.rb b/lib/temple/mixins/options.rb index 06ebe4c..8f554a3 100644 --- a/lib/temple/mixins/options.rb +++ b/lib/temple/mixins/options.rb @@ -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 @@ -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 diff --git a/lib/temple/mixins/template.rb b/lib/temple/mixins/template.rb index 0d7c80b..53deda5 100644 --- a/lib/temple/mixins/template.rb +++ b/lib/temple/mixins/template.rb @@ -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 diff --git a/lib/temple/utils.rb b/lib/temple/utils.rb index 968a7f9..350eac3 100644 --- a/lib/temple/utils.rb +++ b/lib/temple/utils.rb @@ -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. # diff --git a/test/test_filter.rb b/test/test_filter.rb index 5287afd..b9a57d2 100644 --- a/test/test_filter.rb +++ b/test/test_filter.rb @@ -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 diff --git a/test/test_hash.rb b/test/test_hash.rb new file mode 100644 index 0000000..690a371 --- /dev/null +++ b/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 diff --git a/test/test_utils.rb b/test/test_utils.rb index 9a3e293..f822ccb 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -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