musterb 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,19 @@
1
1
  require "erubis"
2
2
 
3
3
  module Musterb
4
- autoload :VERSION, "musterb/version"
4
+ autoload :VERSION, "musterb/version"
5
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"
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 :InstanceVariableExtractor, "musterb/instance_variable_extractor"
12
+ autoload :NullExtractor, "musterb/null_extractor"
13
+ autoload :Evaluator, "musterb/evaluator"
14
+ autoload :Chain, "musterb/chain"
14
15
 
15
- autoload :TemplateHandler, "musterb/template_handler"
16
+ autoload :TemplateHandler, "musterb/template_handler"
16
17
 
17
18
  def self.to_erb(template, options = {})
18
19
  klass = options[:musterbifier_klass] || Musterbifier
@@ -0,0 +1,17 @@
1
+ class Musterb::InstanceVariableExtractor
2
+ attr_reader :parent, :value
3
+
4
+ def initialize(value, parent)
5
+ @value = value
6
+ @parent = parent
7
+ end
8
+
9
+ def [](symbol)
10
+ puts "looking for #{symbol}"
11
+ if value.instance_variable_defined?("@#{symbol}")
12
+ value.instance_variable_get("@#{symbol}")
13
+ else
14
+ @parent[symbol]
15
+ end
16
+ end
17
+ end
@@ -15,14 +15,13 @@ class Musterb::TemplateHandler < Musterb::Musterbifier
15
15
  end
16
16
 
17
17
  def self.compile_mustache(source, options = {})
18
- erb = Musterb.to_erb(source, options.merge(:musterbifier_klass => self))
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))
19
20
  klass = ActionView::Template::Handlers::ERB
20
21
  klass.erb_implementation.new(erb, :trim => (klass.erb_trim_mode == "-")).src
21
22
  end
22
23
 
23
24
  def self.call(template)
24
- options = {}
25
- options[:initial_context] = "initial_context" if template.locals.include? "initial_context"
26
- compile_mustache(template.source, options)
25
+ compile_mustache(template.source, :start_with_existing_context => template.locals.include?("initial_context"))
27
26
  end
28
27
  end
@@ -1,3 +1,3 @@
1
1
  module Musterb
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,15 @@
1
+ describe Musterb::InstanceVariableExtractor do
2
+ class TestClass
3
+ def initialize
4
+ @foo = "bar"
5
+ end
6
+ end
7
+
8
+ it "reads instance variables" do
9
+ Musterb::InstanceVariableExtractor.new(TestClass.new, nil)["foo"].should eq "bar"
10
+ end
11
+
12
+ it "delegates to the parent if it's not found" do
13
+ Musterb::InstanceVariableExtractor.new(TestClass.new, Musterb::ObjectExtractor.new(2, nil))["to_s"].should eq "2"
14
+ end
15
+ end
@@ -1,8 +1,8 @@
1
1
  require 'musterb/template_handler'
2
2
 
3
3
  describe Musterb::TemplateHandler do
4
- def evaluate(template, binding)
5
- compiled = "output_buffer = nil; " + Musterb::TemplateHandler::compile_mustache(template)
4
+ def evaluate(template, binding, options = {})
5
+ compiled = "output_buffer = nil; " + Musterb::TemplateHandler::compile_mustache(template, options)
6
6
  binding.eval compiled
7
7
  end
8
8
 
@@ -24,4 +24,14 @@ describe Musterb::TemplateHandler do
24
24
  foo = "<br>"
25
25
  evaluate("{{{foo}}}", binding).should eq "<br>"
26
26
  end
27
+
28
+ it "can read from instance variables (likely on the controller)" do
29
+ @foo = "hello"
30
+ evaluate("{{foo}}", binding).should eq "hello"
31
+ end
32
+
33
+ it "can be bootstrapped from an initial_context" do
34
+ initial_context = Musterb::ObjectExtractor.new(2, nil)
35
+ evaluate("{{to_s}}", binding, :start_with_existing_context => true).should eq "2"
36
+ end
27
37
  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.0
4
+ version: 0.1.1
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-16 00:00:00.000000000 Z
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erubis
@@ -123,6 +123,8 @@ files:
123
123
  bGliL211c3RlcmIvZXh0cmFjdF92YWx1ZXMucmI=
124
124
  - !binary |-
125
125
  bGliL211c3RlcmIvaGFzaF9leHRyYWN0b3IucmI=
126
+ - !binary |-
127
+ bGliL211c3RlcmIvaW5zdGFuY2VfdmFyaWFibGVfZXh0cmFjdG9yLnJi
126
128
  - !binary |-
127
129
  bGliL211c3RlcmIvbXVzdGVyYmlmaWVyLnJi
128
130
  - !binary |-
@@ -141,6 +143,9 @@ files:
141
143
  c3BlYy9tdXN0ZXJiL2V2YWx1YXRvcl9zcGVjLnJi
142
144
  - !binary |-
143
145
  c3BlYy9tdXN0ZXJiL2hhc2hfZXh0cmFjdG9yX3NwZWMucmI=
146
+ - !binary |-
147
+ c3BlYy9tdXN0ZXJiL2luc3RhbmNlX3ZhcmlhYmxlX2V4dHJhY3Rvcl9zcGVj
148
+ LnJi
144
149
  - !binary |-
145
150
  c3BlYy9tdXN0ZXJiL211c3RlcmJpZmllcl9zcGVjLnJi
146
151
  - !binary |-
@@ -184,6 +189,9 @@ test_files:
184
189
  c3BlYy9tdXN0ZXJiL2V2YWx1YXRvcl9zcGVjLnJi
185
190
  - !binary |-
186
191
  c3BlYy9tdXN0ZXJiL2hhc2hfZXh0cmFjdG9yX3NwZWMucmI=
192
+ - !binary |-
193
+ c3BlYy9tdXN0ZXJiL2luc3RhbmNlX3ZhcmlhYmxlX2V4dHJhY3Rvcl9zcGVj
194
+ LnJi
187
195
  - !binary |-
188
196
  c3BlYy9tdXN0ZXJiL211c3RlcmJpZmllcl9zcGVjLnJi
189
197
  - !binary |-