musterb 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,6 @@ class Musterb::InstanceVariableExtractor
7
7
  end
8
8
 
9
9
  def [](symbol)
10
- puts "looking for #{symbol}"
11
10
  if value.instance_variable_defined?("@#{symbol}")
12
11
  value.instance_variable_get("@#{symbol}")
13
12
  else
@@ -0,0 +1,17 @@
1
+ class Musterb::RailsLocalsExtractor
2
+ attr_reader :parent, :value
3
+
4
+ def initialize(locals, binding, parent)
5
+ @locals = locals
6
+ @parent = parent
7
+ @binding = binding
8
+ end
9
+
10
+ def [](symbol)
11
+ if @locals.include? symbol
12
+ @binding.eval symbol
13
+ else
14
+ parent[symbol]
15
+ end
16
+ end
17
+ end
@@ -14,14 +14,14 @@ class Musterb::TemplateHandler < Musterb::Musterbifier
14
14
  "<%= #{tokens} %>"
15
15
  end
16
16
 
17
- def self.compile_mustache(source, options = {})
18
- initial_context = options[:start_with_existing_context] ? "initial_context" : 'Musterb::InstanceVariableExtractor.new(self, Musterb::BindingExtractor.new(binding))'
19
- erb = Musterb.to_erb(source, options.merge(:musterbifier_klass => self, :initial_context => initial_context))
20
- klass = ActionView::Template::Handlers::ERB
21
- klass.erb_implementation.new(erb, :trim => (klass.erb_trim_mode == "-")).src
17
+ def self.build_initial_context(locals)
18
+ "Musterb::RailsLocalsExtractor.new(#{locals.inspect}, binding, Musterb::InstanceVariableExtractor.new(self, Musterb::BindingExtractor.new(binding)))"
22
19
  end
23
20
 
24
21
  def self.call(template)
25
- compile_mustache(template.source, :start_with_existing_context => template.locals.include?("initial_context"))
22
+ initial_context = template.locals.include?("initial_context") ? "initial_context" : build_initial_context(template.locals.map(&:to_s) - ["initial_context"])
23
+ erb = Musterb.to_erb(template.source, :musterbifier_klass => self, :initial_context => initial_context)
24
+ klass = ActionView::Template::Handlers::ERB
25
+ klass.erb_implementation.new(erb, :trim => (klass.erb_trim_mode == "-")).src
26
26
  end
27
27
  end
@@ -1,3 +1,3 @@
1
1
  module Musterb
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/musterb.rb CHANGED
@@ -13,6 +13,7 @@ module Musterb
13
13
  autoload :Evaluator, "musterb/evaluator"
14
14
  autoload :Chain, "musterb/chain"
15
15
 
16
+ autoload :RailsLocalsExtractor, "musterb/rails_locals_extractor"
16
17
  autoload :TemplateHandler, "musterb/template_handler"
17
18
 
18
19
  def self.to_erb(template, options = {})
@@ -1,9 +1,14 @@
1
1
  require 'musterb/template_handler'
2
+ require 'ostruct'
2
3
 
3
4
  describe Musterb::TemplateHandler do
4
- def evaluate(template, binding, options = {})
5
- compiled = "output_buffer = nil; " + Musterb::TemplateHandler::compile_mustache(template, options)
6
- binding.eval compiled
5
+ def compile_template(template, options = {})
6
+ template = OpenStruct.new({:source => template, :locals => []}.merge(options))
7
+ "output_buffer = nil; " + Musterb::TemplateHandler::call(template)
8
+ end
9
+
10
+ def evaluate(template, binding, options = {})
11
+ binding.eval compile_template(template, options)
7
12
  end
8
13
 
9
14
  it "is wired up correctly" do
@@ -12,7 +17,7 @@ describe Musterb::TemplateHandler do
12
17
  end
13
18
 
14
19
  it "renders partials corrects" do
15
- Musterb::TemplateHandler::compile_mustache("{{>foo}}").should include "render :partial => 'foo', :locals => {:initial_context => musterb.context}"
20
+ compile_template("{{>foo}}").should include "render :partial => 'foo', :locals => {:initial_context => musterb.context}"
16
21
  end
17
22
 
18
23
  it "escapes things by default" do
@@ -32,6 +37,12 @@ describe Musterb::TemplateHandler do
32
37
 
33
38
  it "can be bootstrapped from an initial_context" do
34
39
  initial_context = Musterb::ObjectExtractor.new(2, nil)
35
- evaluate("{{to_s}}", binding, :start_with_existing_context => true).should eq "2"
40
+ evaluate("{{to_s}}", binding, :locals => ["initial_context"]).should eq "2"
41
+ end
42
+
43
+ it "favors locals over instance variables if passed into locals" do
44
+ @foo = "hello"
45
+ foo = "bye"
46
+ evaluate("{{foo}}", binding, :locals => ["foo"]).should eq "bye"
36
47
  end
37
48
  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.1.1
4
+ version: 0.1.2
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-17 00:00:00.000000000 Z
12
+ date: 2012-09-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erubis
@@ -131,6 +131,8 @@ files:
131
131
  bGliL211c3RlcmIvbnVsbF9leHRyYWN0b3IucmI=
132
132
  - !binary |-
133
133
  bGliL211c3RlcmIvb2JqZWN0X2V4dHJhY3Rvci5yYg==
134
+ - !binary |-
135
+ bGliL211c3RlcmIvcmFpbHNfbG9jYWxzX2V4dHJhY3Rvci5yYg==
134
136
  - !binary |-
135
137
  bGliL211c3RlcmIvdGVtcGxhdGVfaGFuZGxlci5yYg==
136
138
  - !binary |-