musterb 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,7 @@ module Musterb
19
19
  def self.to_erb(template, options = {})
20
20
  klass = options[:musterbifier_klass] || Musterbifier
21
21
  musterbifier = klass.new(template)
22
- initial_context = options[:initial_context] || 'Musterb::BindingExtractor.new binding'
22
+ initial_context = options[:initial_context] || 'Musterb::BindingExtractor.new(binding, Musterb::NullExtractor.new)'
23
23
  "<% Musterb::Evaluator.new(#{initial_context}).tap do |musterb| %>#{musterbifier.to_erb}<% end %>"
24
24
  end
25
25
 
@@ -1,11 +1,14 @@
1
1
  class Musterb::BindingExtractor
2
- def initialize(_binding)
2
+ attr_reader :value, :parent
3
+
4
+ def initialize(_binding, parent)
3
5
  @binding = _binding
6
+ @parent = parent
4
7
  end
5
8
 
6
9
  def [](symbol)
7
10
  @binding.eval symbol
8
11
  rescue NameError
9
- nil
12
+ parent[symbol]
10
13
  end
11
14
  end
@@ -1,8 +1,8 @@
1
1
  class Musterb::NullExtractor
2
2
  attr_reader :value, :parent
3
3
 
4
- def initialize(parent)
5
- @parent = parent
4
+ def initialize(parent = nil)
5
+ @parent = parent || self
6
6
  end
7
7
 
8
8
  def [](value)
@@ -15,7 +15,7 @@ class Musterb::TemplateHandler < Musterb::Musterbifier
15
15
  end
16
16
 
17
17
  def self.build_initial_context(locals)
18
- "Musterb::RailsLocalsExtractor.new(#{locals.inspect}, binding, Musterb::InstanceVariableExtractor.new(self, Musterb::BindingExtractor.new(binding)))"
18
+ "Musterb::RailsLocalsExtractor.new(#{locals.inspect}, binding, Musterb::InstanceVariableExtractor.new(self, Musterb::NullExtractor.new))"
19
19
  end
20
20
 
21
21
  def self.call(template)
@@ -1,3 +1,3 @@
1
1
  module Musterb
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,12 +1,12 @@
1
1
  describe Musterb::BindingExtractor do
2
2
  it "can pull out local variables from the binding" do
3
3
  foo = "bar"
4
- extractor = Musterb::BindingExtractor.new binding
4
+ extractor = Musterb::BindingExtractor.new binding, nil
5
5
  extractor["foo"].should eq "bar"
6
6
  end
7
7
 
8
8
  it "returns nil if the local variable cannot be found" do
9
- extractor = Musterb::BindingExtractor.new binding
9
+ extractor = Musterb::BindingExtractor.new binding, Musterb::NullExtractor.new
10
10
  extractor["foo"].should be_nil
11
11
  end
12
12
  end
@@ -1,7 +1,7 @@
1
1
  describe Musterb::Evaluator do
2
2
  it "can pull local variables out from the binding" do
3
3
  foo = "bar"
4
- evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding)
4
+ evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding, nil)
5
5
  evaluator["foo"].should eq "bar"
6
6
  end
7
7
 
@@ -72,7 +72,7 @@ describe Musterb::Evaluator do
72
72
  context "switching context" do
73
73
  it "switches inside a hash" do
74
74
  hash = { "foo" => "bar"}
75
- evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding)
75
+ evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding, nil)
76
76
  evaluator.block_if hash do
77
77
  evaluator['foo'].should eq 'bar'
78
78
  end
@@ -80,7 +80,7 @@ describe Musterb::Evaluator do
80
80
 
81
81
  it "resets the context later" do
82
82
  hash = { "foo" => "bar"}
83
- evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding)
83
+ evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding, nil)
84
84
  evaluator.block_if(hash) {}
85
85
  evaluator["hash"].should eq hash
86
86
  end
@@ -88,7 +88,7 @@ describe Musterb::Evaluator do
88
88
  it "cascades the context to the parent" do
89
89
  foo = "bar"
90
90
  hash = { }
91
- evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding)
91
+ evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding, nil)
92
92
  evaluator.block_if hash do
93
93
  evaluator['foo'].should eq 'bar'
94
94
  end
@@ -13,7 +13,7 @@ describe Musterb::TemplateHandler do
13
13
 
14
14
  it "is wired up correctly" do
15
15
  foo = "hi"
16
- evaluate("{{foo}}", binding).should eq "hi"
16
+ evaluate("{{foo}}", binding, :locals => ["foo"]).should eq "hi"
17
17
  end
18
18
 
19
19
  it "renders partials corrects" do
@@ -22,12 +22,12 @@ describe Musterb::TemplateHandler do
22
22
 
23
23
  it "escapes things by default" do
24
24
  foo = "<br>"
25
- evaluate("{{foo}}", binding).should eq "&lt;br&gt;"
25
+ evaluate("{{foo}}", binding, :locals => ["foo"]).should eq "&lt;br&gt;"
26
26
  end
27
27
 
28
28
  it "does not escape things in triple staches" do
29
29
  foo = "<br>"
30
- evaluate("{{{foo}}}", binding).should eq "<br>"
30
+ evaluate("{{{foo}}}", binding, :locals => ["foo"]).should eq "<br>"
31
31
  end
32
32
 
33
33
  it "can read from instance variables (likely on the controller)" do
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.2
4
+ version: 0.2.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-21 00:00:00.000000000 Z
12
+ date: 2012-09-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erubis