From d404d955e8e135aef822b16d10b2c5e81a1bb224 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Thu, 22 Jan 2009 16:25:45 -0800 Subject: [PATCH 1/4] Add a Haml::Util module to stable. --- lib/haml.rb | 1 + lib/haml/util.rb | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 lib/haml/util.rb diff --git a/lib/haml.rb b/lib/haml.rb index e983bd21..64c20ac6 100644 --- a/lib/haml.rb +++ b/lib/haml.rb @@ -1053,4 +1053,5 @@ module Haml end end +require 'haml/util' require 'haml/engine' diff --git a/lib/haml/util.rb b/lib/haml/util.rb new file mode 100644 index 00000000..999c588a --- /dev/null +++ b/lib/haml/util.rb @@ -0,0 +1,5 @@ +module Haml + module Util + class << self; include Haml::Util; end + end +end From 8e1b40e31ff9a2aee9f146256cd97a51a3d2c226 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Thu, 22 Jan 2009 16:29:02 -0800 Subject: [PATCH 2/4] Compatibility with 1.9 *_methods methods which return symbols. --- lib/haml/helpers/action_view_mods.rb | 4 ++-- lib/haml/util.rb | 10 ++++++++++ test/benchmark.rb | 2 +- test/haml/template_test.rb | 4 ++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/haml/helpers/action_view_mods.rb b/lib/haml/helpers/action_view_mods.rb index b9c89d10..38630fe1 100644 --- a/lib/haml/helpers/action_view_mods.rb +++ b/lib/haml/helpers/action_view_mods.rb @@ -17,7 +17,7 @@ if defined?(ActionView) and not defined?(Merb::Plugins) alias_method :render, :render_with_haml # Rails >2.1 - if instance_methods.include?('output_buffer') + if Haml::Util.has?(:instance_method, self, :output_buffer) def output_buffer_with_haml return haml_buffer.buffer if is_haml? output_buffer_without_haml @@ -44,7 +44,7 @@ if defined?(ActionView) and not defined?(Merb::Plugins) # In Rails <=2.1, we've got to override considerable capturing infrastructure. # In Rails >2.1, we can make do with only overriding #capture # (which no longer behaves differently in helper contexts). - unless ActionView::Base.instance_methods.include?('output_buffer') + unless Haml::Util.has?(:instance_method, ActionView::Base, :output_buffer) module CaptureHelper def capture_with_haml(*args, &block) # Rails' #capture helper will just return the value of the block diff --git a/lib/haml/util.rb b/lib/haml/util.rb index 999c588a..5a134b42 100644 --- a/lib/haml/util.rb +++ b/lib/haml/util.rb @@ -1,5 +1,15 @@ module Haml module Util class << self; include Haml::Util; end + + RUBY_VERSION = ::RUBY_VERSION.split(".").map {|s| s.to_i} + + def ruby1_8? + Haml::Util::RUBY_VERSION[0] == 1 && Haml::Util::RUBY_VERSION[1] < 9 + end + + def has?(attr, klass, method) + klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym) + end end end diff --git a/test/benchmark.rb b/test/benchmark.rb index 2bb8160d..de9ed36e 100755 --- a/test/benchmark.rb +++ b/test/benchmark.rb @@ -17,7 +17,7 @@ require File.dirname(__FILE__) + '/linked_rails' action_view action_pack haml/template rbench].each {|dep| require(dep)} def view - unless ActionView::Base.instance_methods.include? 'finder' + unless Haml::Util.has?(:instance_method, ActionView::Base, :finder) return ActionView::Base.new(File.dirname(__FILE__), {}) end diff --git a/test/haml/template_test.rb b/test/haml/template_test.rb index fd31c905..b756b5c5 100644 --- a/test/haml/template_test.rb +++ b/test/haml/template_test.rb @@ -38,7 +38,7 @@ class TemplateTest < Test::Unit::TestCase def setup vars = { 'article' => Article.new, 'foo' => 'value one' } - unless ActionView::Base.instance_methods.include? 'finder' + unless Haml::Util.has?(:instance_method, ActionView::Base, :finder) @base = ActionView::Base.new(TEMPLATE_PATH, vars) else # Rails 2.1.0 @@ -46,7 +46,7 @@ class TemplateTest < Test::Unit::TestCase @base.finder.append_view_path(TEMPLATE_PATH) end - if @base.private_methods.include?('evaluate_assigns') + if Haml::Util.has?(:private_method, @base, :evaluate_assigns) @base.send(:evaluate_assigns) else # Rails 2.2 From 530d226ff67606e29e259c5be3c7a36ab266ddf6 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Thu, 22 Jan 2009 14:42:10 -0800 Subject: [PATCH 3/4] Eval using block.binding rather than just block for 1.9 compatibility. --- lib/haml/helpers.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 1db4505e..b48dc2b7 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -254,7 +254,7 @@ module Haml # the local variable foo would be assigned to "

13

\n". # def capture_haml(*args, &block) - buffer = eval('_hamlout', block) rescue haml_buffer + buffer = eval('_hamlout', block.binding) rescue haml_buffer with_haml_buffer(buffer) do position = haml_buffer.buffer.length @@ -412,7 +412,7 @@ END # Returns whether or not +block+ is defined directly in a Haml template. def block_is_haml?(block) - eval('_hamlout', block) + eval('_hamlout', block.binding) true rescue false From dd8901ada292164e24bae69fc56d1f7faa0cd167 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Thu, 22 Jan 2009 14:44:37 -0800 Subject: [PATCH 4/4] Strings aren't enumerable in 1.9. --- lib/haml/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index b48dc2b7..749c1c6e 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -260,7 +260,7 @@ module Haml block.call(*args) - captured = haml_buffer.buffer.slice!(position..-1) + captured = haml_buffer.buffer.slice!(position..-1).split(/^/) min_tabs = nil captured.each do |line|