musterb 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/musterb/evaluator.rb +2 -0
- data/lib/musterb/musterbifier.rb +10 -1
- data/lib/musterb/template_handler.rb +7 -1
- data/lib/musterb/version.rb +1 -1
- data/lib/musterb.rb +4 -3
- data/spec/musterb/musterbifier_spec.rb +9 -0
- data/spec/musterb_spec.rb +10 -0
- data/spec/performance.rb +13 -11
- metadata +1 -1
data/lib/musterb/evaluator.rb
CHANGED
data/lib/musterb/musterbifier.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
class Musterb::Musterbifier
|
2
|
-
def initialize(template)
|
2
|
+
def initialize(template, render_partial_template = nil)
|
3
3
|
@template = template
|
4
|
+
@render_partial_template = render_partial_template || method(:partials_not_implemented)
|
4
5
|
end
|
5
6
|
|
6
7
|
def fetch(tokens)
|
@@ -30,9 +31,17 @@ class Musterb::Musterbifier
|
|
30
31
|
""
|
31
32
|
when '.'
|
32
33
|
"<%== musterb.current %>"
|
34
|
+
when '='
|
35
|
+
raise NotImplementedError, 'Not able to change the mustache delimiter just yet'
|
36
|
+
when '>'
|
37
|
+
"<%= #{@render_partial_template.call(match[1..-1].strip)} %>"
|
33
38
|
else
|
34
39
|
"<%== #{fetch match} %>"
|
35
40
|
end
|
36
41
|
end
|
37
42
|
end
|
43
|
+
|
44
|
+
def partials_not_implemented(partial)
|
45
|
+
"raise NotImplementedError, 'Don't know how to render partial: #{partial}'"
|
46
|
+
end
|
38
47
|
end
|
@@ -1,8 +1,14 @@
|
|
1
1
|
require 'musterb'
|
2
2
|
|
3
3
|
module Musterb::TemplateHandler
|
4
|
+
def self.render_partial_template(partial)
|
5
|
+
"render :partial => '#{partial}', :locals => {:initial_context => musterb.context}"
|
6
|
+
end
|
7
|
+
|
4
8
|
def self.call(template)
|
5
|
-
|
9
|
+
options = {:render_partial_template => method(:render_partial_template)}
|
10
|
+
options[:initial_context] = "initial_context" if template.locals.include? :initial_context
|
11
|
+
erb = Musterb.to_erb(template.source, options)
|
6
12
|
klass = ActionView::Template::Handlers::ERB
|
7
13
|
klass.erb_implementation.new(erb, :trim => (klass.erb_trim_mode == "-")).src
|
8
14
|
end
|
data/lib/musterb/version.rb
CHANGED
data/lib/musterb.rb
CHANGED
@@ -11,9 +11,10 @@ require "musterb/chain"
|
|
11
11
|
require "erubis"
|
12
12
|
|
13
13
|
module Musterb
|
14
|
-
def self.to_erb(template)
|
15
|
-
musterbifier = Musterbifier.new(template)
|
16
|
-
|
14
|
+
def self.to_erb(template, options = {})
|
15
|
+
musterbifier = Musterbifier.new(template, options[:render_partial_template])
|
16
|
+
initial_context = options[:initial_context] || 'Musterb::BindingExtractor.new binding'
|
17
|
+
"<% Musterb::Evaluator.new(#{initial_context}).tap do |musterb| %>#{musterbifier.to_erb}<% end %>"
|
17
18
|
end
|
18
19
|
|
19
20
|
def self.render(template, values)
|
@@ -34,4 +34,13 @@ describe Musterb::Musterbifier do
|
|
34
34
|
it "replaces foo.bar with a chain" do
|
35
35
|
Musterb::Musterbifier.new("{{foo.bar}}").to_erb.should eq "<%== musterb.chain('foo')['bar'] %>"
|
36
36
|
end
|
37
|
+
|
38
|
+
it "replaces calls for partials with an exception by default" do
|
39
|
+
Musterb::Musterbifier.new("{{> foo}}").to_erb.should eq "<%= raise NotImplementedError, 'Don't know how to render partial: foo' %>"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can be injected with an algorithm for creating partials" do
|
43
|
+
render_partial_template = lambda { |p| "render :partial => '#{p}', :current_context => musterb.context" }
|
44
|
+
Musterb::Musterbifier.new("{{> foo}}", render_partial_template).to_erb.should eq "<%= render :partial => 'foo', :current_context => musterb.context %>"
|
45
|
+
end
|
37
46
|
end
|
data/spec/musterb_spec.rb
CHANGED
@@ -28,4 +28,14 @@ describe Musterb do
|
|
28
28
|
hash = {:foos => [1, 2, 3]}
|
29
29
|
Musterb.render("{{#foos}}{{.}}{{/foos}}", hash).should eq "123"
|
30
30
|
end
|
31
|
+
|
32
|
+
it "escapes html" do
|
33
|
+
hash = {:foo => "<br>" }
|
34
|
+
Musterb.render("{{foo}}", hash).should eq "<br>"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "skips html evaluation with triple stach" do
|
38
|
+
hash = {:foo => "<br>" }
|
39
|
+
Musterb.render("{{{foo}}}", hash).should eq "<br>"
|
40
|
+
end
|
31
41
|
end
|
data/spec/performance.rb
CHANGED
@@ -32,19 +32,21 @@ template = <<-EOF
|
|
32
32
|
{{/products}}
|
33
33
|
EOF
|
34
34
|
|
35
|
+
# Mix of symbols and strings
|
35
36
|
def random_product
|
36
37
|
{
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
38
|
+
:id => rand(10000),
|
39
|
+
:favorite_id => rand(10000),
|
40
|
+
:quick_view_url => "http://foo.bar",
|
41
|
+
:name => "Product name",
|
42
|
+
:url => "http://foo.url",
|
43
|
+
:thumbnail => "http://foo.image",
|
44
|
+
:sku => "sku",
|
45
|
+
:fabrics => "fabrics",
|
46
|
+
:price => "price",
|
47
|
+
:themes => "themes",
|
48
|
+
:designer => "designer",
|
49
|
+
:favorited => "favorited"
|
48
50
|
}
|
49
51
|
end
|
50
52
|
|