musterb 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/musterb/chain.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Musterb::Chain
2
- include ExtractValues
2
+ include Musterb::ExtractValues
3
3
 
4
4
  def initialize(value)
5
5
  @context = new_context(value, Musterb::NullExtractor.new(nil))
@@ -1,5 +1,5 @@
1
1
  class Musterb::Evaluator
2
- include ExtractValues
2
+ include Musterb::ExtractValues
3
3
 
4
4
  attr_reader :context
5
5
 
@@ -1,4 +1,4 @@
1
- module ExtractValues
1
+ module Musterb::ExtractValues
2
2
  def value
3
3
  @context.value
4
4
  end
@@ -1,11 +1,12 @@
1
1
  class Musterb::Musterbifier
2
- def initialize(template, render_partial_template = nil)
2
+ def initialize(template)
3
3
  @template = template
4
- @render_partial_template = render_partial_template || method(:partials_not_implemented)
5
4
  end
6
5
 
7
- def fetch(tokens)
8
- tokens = tokens.strip.split(".")
6
+ def fetch(match)
7
+ match = match.strip
8
+ return "musterb.current" if match == '.'
9
+ tokens = match.split(".")
9
10
  last_token = tokens.pop
10
11
  fetch_command = tokens.inject("musterb") do |str, token|
11
12
  "#{str}.chain('#{token}')"
@@ -13,35 +14,61 @@ class Musterb::Musterbifier
13
14
  "#{fetch_command}['#{last_token}']"
14
15
  end
15
16
 
17
+ def block_if(tokens)
18
+ "<% musterb.block_if #{tokens} do %>"
19
+ end
20
+
21
+ def block_unless(tokens)
22
+ "<% musterb.block_unless #{tokens} do %>"
23
+ end
24
+
25
+ def block_end
26
+ "<% end %>"
27
+ end
28
+
29
+ def text_without_escaping(tokens)
30
+ "<%= #{tokens} %>"
31
+ end
32
+
33
+ def text_with_escaping(tokens)
34
+ "<%== #{tokens} %>"
35
+ end
36
+
37
+ def comment
38
+ ""
39
+ end
40
+
41
+ def change_token
42
+ raise NotImplementedError, 'Not able to change the mustache delimiter just yet'
43
+ end
44
+
45
+ def render_partial(partial)
46
+ raise NotImplementedError, "Override render_partial in Musterbifier to render partials"
47
+ end
48
+
16
49
  def to_erb
17
50
  @template.gsub(/\{\{(\{?[^\}]*\}?)\}\}/) do |match|
18
51
  match = $1
19
52
  case match[0]
20
53
  when '#'
21
- "<% musterb.block_if #{fetch match[1..-1]} do %>"
54
+ block_if fetch match[1..-1]
22
55
  when '^'
23
- "<% musterb.block_unless #{fetch match[1..-1]} do %>"
56
+ block_unless fetch match[1..-1]
24
57
  when "/"
25
- "<% end %>"
58
+ block_end
26
59
  when '{'
27
- "<%= #{fetch match[1..-2]} %>"
60
+ text_without_escaping fetch match[1..-2]
28
61
  when '&'
29
- "<%= #{fetch match[1..-1]} %>"
62
+ text_without_escaping fetch match[1..-1]
30
63
  when '!'
31
- ""
32
- when '.'
33
- "<%== musterb.current %>"
64
+ comment
34
65
  when '='
35
- raise NotImplementedError, 'Not able to change the mustache delimiter just yet'
66
+ change_token
36
67
  when '>'
37
- "<%= #{@render_partial_template.call(match[1..-1].strip)} %>"
68
+ render_partial match[1..-1].strip
38
69
  else
39
- "<%== #{fetch match} %>"
70
+ text_with_escaping fetch match
40
71
  end
41
72
  end
42
73
  end
43
-
44
- def partials_not_implemented(partial)
45
- "raise NotImplementedError, 'Don't know how to render partial: #{partial}'"
46
- end
47
74
  end
@@ -1,15 +1,28 @@
1
1
  require 'musterb'
2
+ require 'action_view'
2
3
 
3
- module Musterb::TemplateHandler
4
- def self.render_partial_template(partial)
5
- "render :partial => '#{partial}', :locals => {:initial_context => musterb.context}"
4
+ class Musterb::TemplateHandler < Musterb::Musterbifier
5
+ def render_partial(partial)
6
+ "<%= render :partial => '#{partial}', :locals => {:initial_context => musterb.context} %>"
7
+ end
8
+
9
+ def text_without_escaping(tokens)
10
+ "<%= #{tokens}.html_safe %>"
11
+ end
12
+
13
+ def text_with_escaping(tokens)
14
+ "<%= #{tokens} %>"
15
+ end
16
+
17
+ def self.compile_mustache(source, options = {})
18
+ erb = Musterb.to_erb(source, options.merge(:musterbifier_klass => self))
19
+ klass = ActionView::Template::Handlers::ERB
20
+ klass.erb_implementation.new(erb, :trim => (klass.erb_trim_mode == "-")).src
6
21
  end
7
22
 
8
23
  def self.call(template)
9
- options = {:render_partial_template => method(:render_partial_template)}
24
+ options = {}
10
25
  options[:initial_context] = "initial_context" if template.locals.include? "initial_context"
11
- erb = Musterb.to_erb(template.source, options)
12
- klass = ActionView::Template::Handlers::ERB
13
- klass.erb_implementation.new(erb, :trim => (klass.erb_trim_mode == "-")).src
26
+ compile_mustache(template.source, options)
14
27
  end
15
28
  end
@@ -1,3 +1,3 @@
1
1
  module Musterb
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/musterb.rb CHANGED
@@ -1,18 +1,22 @@
1
- require "musterb/version"
2
- require "musterb/extract_values"
3
- require "musterb/musterbifier"
4
- require "musterb/binding_extractor"
5
- require "musterb/hash_extractor"
6
- require "musterb/object_extractor"
7
- require "musterb/null_extractor"
8
- require "musterb/evaluator"
9
- require "musterb/chain"
10
-
11
1
  require "erubis"
12
2
 
13
3
  module Musterb
4
+ autoload :VERSION, "musterb/version"
5
+
6
+ autoload :ExtractValues, "musterb/extract_values"
7
+ autoload :Musterbifier, "musterb/musterbifier"
8
+ autoload :BindingExtractor, "musterb/binding_extractor"
9
+ autoload :HashExtractor, "musterb/hash_extractor"
10
+ autoload :ObjectExtractor, "musterb/object_extractor"
11
+ autoload :NullExtractor, "musterb/null_extractor"
12
+ autoload :Evaluator, "musterb/evaluator"
13
+ autoload :Chain, "musterb/chain"
14
+
15
+ autoload :TemplateHandler, "musterb/template_handler"
16
+
14
17
  def self.to_erb(template, options = {})
15
- musterbifier = Musterbifier.new(template, options[:render_partial_template])
18
+ klass = options[:musterbifier_klass] || Musterbifier
19
+ musterbifier = klass.new(template)
16
20
  initial_context = options[:initial_context] || 'Musterb::BindingExtractor.new binding'
17
21
  "<% Musterb::Evaluator.new(#{initial_context}).tap do |musterb| %>#{musterbifier.to_erb}<% end %>"
18
22
  end
data/musterb.gemspec CHANGED
@@ -21,4 +21,5 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency "hashie"
22
22
  gem.add_development_dependency "rspec"
23
23
  gem.add_development_dependency "mustache" # for performance
24
+ gem.add_development_dependency "actionpack"
24
25
  end
@@ -19,6 +19,10 @@ describe Musterb::Musterbifier do
19
19
  Musterb::Musterbifier.new("{{#cond}}foo{{/cond}}").to_erb.should eq "<% musterb.block_if musterb['cond'] do %>foo<% end %>"
20
20
  end
21
21
 
22
+ it "can do #." do
23
+ Musterb::Musterbifier.new("{{#.}}foo{{/.}}").to_erb.should eq "<% musterb.block_if musterb.current do %>foo<% end %>"
24
+ end
25
+
22
26
  it "replaces carrot correctly" do
23
27
  Musterb::Musterbifier.new("{{^cond}}foo{{/cond}}").to_erb.should eq "<% musterb.block_unless musterb['cond'] do %>foo<% end %>"
24
28
  end
@@ -36,11 +40,6 @@ describe Musterb::Musterbifier do
36
40
  end
37
41
 
38
42
  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 %>"
43
+ lambda { Musterb::Musterbifier.new("{{> foo}}").to_erb }.should raise_error NotImplementedError
45
44
  end
46
45
  end
@@ -0,0 +1,27 @@
1
+ require 'musterb/template_handler'
2
+
3
+ describe Musterb::TemplateHandler do
4
+ def evaluate(template, binding)
5
+ compiled = "output_buffer = nil; " + Musterb::TemplateHandler::compile_mustache(template)
6
+ binding.eval compiled
7
+ end
8
+
9
+ it "is wired up correctly" do
10
+ foo = "hi"
11
+ evaluate("{{foo}}", binding).should eq "hi"
12
+ end
13
+
14
+ it "renders partials corrects" do
15
+ Musterb::TemplateHandler::compile_mustache("{{>foo}}").should include "render :partial => 'foo', :locals => {:initial_context => musterb.context}"
16
+ end
17
+
18
+ it "escapes things by default" do
19
+ foo = "<br>"
20
+ evaluate("{{foo}}", binding).should eq "&lt;br&gt;"
21
+ end
22
+
23
+ it "does not escape things in triple staches" do
24
+ foo = "<br>"
25
+ evaluate("{{{foo}}}", binding).should eq "<br>"
26
+ end
27
+ end
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.7
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-15 00:00:00.000000000 Z
12
+ date: 2012-09-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erubis
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: actionpack
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: The ability to compile mustache templates to erubis erb templates so
79
95
  that we can make use of the performance of compiling templates
80
96
  email:
@@ -129,6 +145,8 @@ files:
129
145
  c3BlYy9tdXN0ZXJiL211c3RlcmJpZmllcl9zcGVjLnJi
130
146
  - !binary |-
131
147
  c3BlYy9tdXN0ZXJiL29iamVjdF9leHRyYWN0b3Jfc3BlYy5yYg==
148
+ - !binary |-
149
+ c3BlYy9tdXN0ZXJiL3RlbXBsYXRlX2hhbmRsZXJfc3BlYy5yYg==
132
150
  - !binary |-
133
151
  c3BlYy9tdXN0ZXJiX3NwZWMucmI=
134
152
  - !binary |-
@@ -170,6 +188,8 @@ test_files:
170
188
  c3BlYy9tdXN0ZXJiL211c3RlcmJpZmllcl9zcGVjLnJi
171
189
  - !binary |-
172
190
  c3BlYy9tdXN0ZXJiL29iamVjdF9leHRyYWN0b3Jfc3BlYy5yYg==
191
+ - !binary |-
192
+ c3BlYy9tdXN0ZXJiL3RlbXBsYXRlX2hhbmRsZXJfc3BlYy5yYg==
173
193
  - !binary |-
174
194
  c3BlYy9tdXN0ZXJiX3NwZWMucmI=
175
195
  - !binary |-