musterb 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
1
  class Musterb::Evaluator
2
2
  include ExtractValues
3
3
 
4
+ attr_reader :context
5
+
4
6
  def initialize(context)
5
7
  @context = context
6
8
  end
@@ -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
- erb = Musterb.to_erb(template.source)
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
@@ -1,3 +1,3 @@
1
1
  module Musterb
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
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
- "<% Musterb::Evaluator.new(Musterb::BindingExtractor.new binding).tap do |musterb| %>#{musterbifier.to_erb}<% end %>"
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 "&lt;br&gt;"
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
- "id" => rand(10000),
38
- "favorite_id" => rand(10000),
39
- "quick_view_url" => "http://foo.bar",
40
- "name" => "Product name",
41
- "url" => "http://foo.url",
42
- "thumbnail" => "http://foo.image",
43
- "sku" => "sku",
44
- "fabrics" => "fabrics",
45
- "themes" => "themes",
46
- "designer" => "designer",
47
- "favorited" => "favorited"
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: musterb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: