1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Remove unused Haml::Util::def_static_method

This commit is contained in:
Tee Parham 2014-01-15 08:35:41 -07:00 committed by Norman Clarke
parent b73f65c84e
commit a6dac48e31
2 changed files with 0 additions and 72 deletions

View file

@ -182,52 +182,6 @@ MSG
end
end
# This is used for methods in {Haml::Buffer} that need to be very fast,
# and take a lot of boolean parameters
# that are known at compile-time.
# Instead of passing the parameters in normally,
# a separate method is defined for every possible combination of those parameters;
# these are then called using \{#static\_method\_name}.
#
# To define a static method, an ERB template for the method is provided.
# All conditionals based on the static parameters
# are done as embedded Ruby within this template.
# For example:
#
# def_static_method(Foo, :my_static_method, [:foo, :bar], :baz, :bang, <<RUBY)
# <% if baz && bang %>
# return foo + bar
# <% elsif baz || bang %>
# return foo - bar
# <% else %>
# return 17
# <% end %>
# RUBY
#
# \{#static\_method\_name} can be used to call static methods.
#
# @overload def_static_method(klass, name, args, *vars, erb)
# @param klass [Module] The class on which to define the static method
# @param name [#to_s] The (base) name of the static method
# @param args [Array<Symbol>] The names of the arguments to the defined methods
# (**not** to the ERB template)
# @param vars [Array<Symbol>] The names of the static boolean variables
# to be made available to the ERB template
def def_static_method(klass, name, args, *vars)
erb = vars.pop
info = caller_info
powerset(vars).each do |set|
context = StaticConditionalContext.new(set).instance_eval {binding}
method_content = (defined?(Erubis::TinyEruby) && Erubis::TinyEruby || ERB).new(erb).result(context)
klass.class_eval(<<METHOD, info[0], info[1])
def #{static_method_name(name, *vars.map {|v| set.include?(v)})}(#{args.join(', ')})
#{method_content}
end
METHOD
end
end
# Computes the name for a method defined via \{#def\_static\_method}.
#
# @param name [String] The base name of the static method

View file

@ -34,32 +34,6 @@ class UtilTest < MiniTest::Unit::TestCase
assert_equal(["/tmp/foo.rb", 12, "fizzle"], caller_info("/tmp/foo.rb:12: in `fizzle {}'"))
end
def test_def_static_method
klass = Class.new
def_static_method(klass, :static_method, [:arg1, :arg2],
:sarg1, :sarg2, <<RUBY)
s = "Always " + arg1
s << " <% if sarg1 %>and<% else %>but never<% end %> " << arg2
<% if sarg2 %>
s << "."
<% end %>
RUBY
c = klass.new
assert_equal("Always brush your teeth and comb your hair.",
c.send(static_method_name(:static_method, true, true),
"brush your teeth", "comb your hair"))
assert_equal("Always brush your teeth and comb your hair",
c.send(static_method_name(:static_method, true, false),
"brush your teeth", "comb your hair"))
assert_equal("Always brush your teeth but never play with fire.",
c.send(static_method_name(:static_method, false, true),
"brush your teeth", "play with fire"))
assert_equal("Always brush your teeth but never play with fire",
c.send(static_method_name(:static_method, false, false),
"brush your teeth", "play with fire"))
end
def test_check_encoding_does_not_destoy_the_given_string
string_with_bom = File.read(File.dirname(__FILE__) + '/templates/with_bom.haml', :encoding => Encoding::UTF_8)
original = string_with_bom.dup