diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 1db4505e..749c1c6e 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -254,13 +254,13 @@ 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 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| @@ -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 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 df487a7c..f0c34092 100644 --- a/lib/haml/util.rb +++ b/lib/haml/util.rb @@ -5,6 +5,8 @@ module Haml module Util class << self; include Haml::Util; end + RUBY_VERSION = ::RUBY_VERSION.split(".").map {|s| s.to_i} + def to_hash(arr) arr.compact.inject({}) {|h, (k, v)| h[k] = v; h} end @@ -32,6 +34,14 @@ module Haml end end + 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 + class StaticConditionalContext def initialize(set) @set = set 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