Permalink
Browse files

Added radiant attributes

  • Loading branch information...
1 parent a07077f commit 0b9120201ea4fefb8613a71f688855e2136779ff Casper Beyer committed Jun 27, 2011
Showing with 32 additions and 4 deletions.
  1. +8 −4 lib/tilt/radius.rb
  2. +24 −0 test/tilt_radiustemplate_test.rb
View
12 lib/tilt/radius.rb
@@ -10,15 +10,19 @@ def self.engine_initialized?
def self.context_class
@context_class ||= Class.new(Radius::Context) do
- attr_accessor :tilt_scope
+ attr_accessor :scope
def tag_missing(name, attributes)
- tilt_scope.__send__(name)
+ if method = scope.method(name)
+ # XXX 1.9 specific.
+ arguments = method.parameters.map(&:last).map { |key| attributes[key.to_s] }
+ method.call(*arguments)
+ end
end
def dup
i = super
- i.tilt_scope = tilt_scope
+ i.scope = scope
i
end
end
@@ -33,7 +37,7 @@ def prepare
def evaluate(scope, locals, &block)
context = self.class.context_class.new
- context.tilt_scope = scope
+ context.scope = scope
context.define_tag("yield") do
block.call
end
View
24 test/tilt_radiustemplate_test.rb
@@ -31,6 +31,14 @@ class RadiusTemplateTest < Test::Unit::TestCase
class ExampleRadiusScope
def beer; 'wet'; end
def whisky; 'wetter'; end
+
+ def name(value)
+ value
+ end
+
+ def combine(a, b)
+ a + b
+ end
end
test "combining scope and locals when scope responds" do
@@ -40,6 +48,22 @@ def whisky; 'wetter'; end
scope = ExampleRadiusScope.new
assert_equal "Beer is wet but Whisky is wetter.", template.render(scope)
end
+
+ test "passing arguments to a scope method" do
+ template = Tilt::RadiusTemplate.new {
+ "Hey <r:name value='Joe'/>!"
+ }
+ scope = ExampleRadiusScope.new
+ assert_equal "Hey Joe!", template.render(scope)
+ end
+
+ test "passing multiple arguments to a scope method order independent" do
+ template = Tilt::RadiusTemplate.new {
+ "<r:combine b='Joe!' a='Hey '/>"
+ }
+ scope = ExampleRadiusScope.new
+ assert_equal "Hey Joe!", template.render(scope)
+ end
test "precedence when locals and scope define same variables" do
template = Tilt::RadiusTemplate.new {

0 comments on commit 0b91202

Please sign in to comment.