musterb 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- class Musterb::BindingExtractor
1
+ class Musterb::BindingExtractor < Musterb::Extractor
2
2
  attr_reader :value, :parent
3
3
 
4
4
  def initialize(_binding, parent)
@@ -11,9 +11,10 @@ module Musterb::ExtractValues
11
11
  Musterb::Chain.new self[symbol]
12
12
  end
13
13
 
14
- private
15
- def new_context(value, old_context = @context)
14
+ def self.new_context(value, old_context = @context)
16
15
  case value
16
+ when Musterb::Extractor
17
+ value
17
18
  when Hash
18
19
  Musterb::HashExtractor.new(value, old_context)
19
20
  when nil
@@ -22,4 +23,9 @@ module Musterb::ExtractValues
22
23
  Musterb::ObjectExtractor.new(value, old_context)
23
24
  end
24
25
  end
25
- end
26
+
27
+ private
28
+ def new_context(value, old_context = @context)
29
+ Musterb::ExtractValues.new_context(value, old_context)
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ # Does nothing, but is a super class for other extractors
2
+ class Musterb::Extractor
3
+
4
+ end
@@ -1,9 +1,9 @@
1
1
  require 'hashie'
2
2
 
3
- class Musterb::HashExtractor
3
+ class Musterb::HashExtractor < Musterb::Extractor
4
4
  attr_reader :parent, :value
5
5
 
6
- def initialize(value, parent)
6
+ def initialize(value, parent)
7
7
  @value = to_string_access(value)
8
8
  @parent = parent
9
9
  end
@@ -23,4 +23,4 @@ class Musterb::HashExtractor
23
23
  hash.hashie_stringify_keys!
24
24
  end
25
25
  end
26
- end
26
+ end
@@ -1,4 +1,4 @@
1
- class Musterb::InstanceVariableExtractor
1
+ class Musterb::InstanceVariableExtractor < Musterb::Extractor
2
2
  attr_reader :parent, :value
3
3
 
4
4
  def initialize(value, parent)
@@ -13,4 +13,4 @@ class Musterb::InstanceVariableExtractor
13
13
  @parent[symbol]
14
14
  end
15
15
  end
16
- end
16
+ end
@@ -1,4 +1,4 @@
1
- class Musterb::NullExtractor
1
+ class Musterb::NullExtractor < Musterb::Extractor
2
2
  attr_reader :value, :parent
3
3
 
4
4
  def initialize(parent = nil)
@@ -8,4 +8,4 @@ class Musterb::NullExtractor
8
8
  def [](value)
9
9
  nil
10
10
  end
11
- end
11
+ end
@@ -1,4 +1,4 @@
1
- class Musterb::ObjectExtractor
1
+ class Musterb::ObjectExtractor < Musterb::Extractor
2
2
  attr_reader :parent, :value
3
3
 
4
4
  def initialize(value, parent)
@@ -6,11 +6,11 @@ class Musterb::ObjectExtractor
6
6
  @parent = parent
7
7
  end
8
8
 
9
- def [](symbol)
9
+ def [](symbol)
10
10
  if @value.respond_to? symbol
11
11
  @value.send(symbol)
12
12
  else
13
13
  @parent[symbol]
14
14
  end
15
15
  end
16
- end
16
+ end
@@ -1,4 +1,4 @@
1
- class Musterb::RailsLocalsExtractor
1
+ class Musterb::RailsLocalsExtractor < Musterb::Extractor
2
2
  attr_reader :parent, :value
3
3
 
4
4
  def initialize(locals, binding, parent)
@@ -14,4 +14,4 @@ class Musterb::RailsLocalsExtractor
14
14
  parent[symbol]
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -18,10 +18,14 @@ class Musterb::TemplateHandler < Musterb::Musterbifier
18
18
  "Musterb::RailsLocalsExtractor.new(#{locals.inspect}, binding, Musterb::InstanceVariableExtractor.new(self, Musterb::NullExtractor.new))"
19
19
  end
20
20
 
21
+ def self.initial_context(initial_context)
22
+ Musterb::ExtractValues.new_context(initial_context)
23
+ end
24
+
21
25
  def self.call(template)
22
- initial_context = template.locals.include?("initial_context") ? "initial_context" : build_initial_context(template.locals.map(&:to_s) - ["initial_context"])
26
+ initial_context = template.locals.include?("initial_context") ? "Musterb::TemplateHandler.initial_context(initial_context)" : build_initial_context(template.locals.map(&:to_s))
23
27
  erb = Musterb.to_erb(template.source, :musterbifier_klass => self, :initial_context => initial_context)
24
28
  klass = ActionView::Template::Handlers::ERB
25
29
  klass.erb_implementation.new(erb, :trim => (klass.erb_trim_mode == "-")).src
26
30
  end
27
- end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Musterb
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/musterb.rb CHANGED
@@ -5,6 +5,7 @@ module Musterb
5
5
 
6
6
  autoload :ExtractValues, "musterb/extract_values"
7
7
  autoload :Musterbifier, "musterb/musterbifier"
8
+ autoload :Extractor, "musterb/extractor"
8
9
  autoload :BindingExtractor, "musterb/binding_extractor"
9
10
  autoload :HashExtractor, "musterb/hash_extractor"
10
11
  autoload :ObjectExtractor, "musterb/object_extractor"
@@ -7,7 +7,7 @@ describe Musterb::TemplateHandler do
7
7
  "output_buffer = nil; " + Musterb::TemplateHandler::call(template)
8
8
  end
9
9
 
10
- def evaluate(template, binding, options = {})
10
+ def evaluate(template, binding, options = {})
11
11
  binding.eval compile_template(template, options)
12
12
  end
13
13
 
@@ -25,7 +25,7 @@ describe Musterb::TemplateHandler do
25
25
  evaluate("{{foo}}", binding, :locals => ["foo"]).should eq "&lt;br&gt;"
26
26
  end
27
27
 
28
- it "does not escape things in triple staches" do
28
+ it "does not escape things in triple staches" do
29
29
  foo = "<br>"
30
30
  evaluate("{{{foo}}}", binding, :locals => ["foo"]).should eq "<br>"
31
31
  end
@@ -45,4 +45,9 @@ describe Musterb::TemplateHandler do
45
45
  foo = "bye"
46
46
  evaluate("{{foo}}", binding, :locals => ["foo"]).should eq "bye"
47
47
  end
48
- end
48
+
49
+ it "can set the initial context to a hash" do
50
+ initial_context = 2
51
+ evaluate("{{to_s}}", binding, :locals => ["initial_context"]).should eq "2"
52
+ end
53
+ 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.2.0
4
+ version: 1.0.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-23 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erubis
@@ -121,6 +121,8 @@ files:
121
121
  bGliL211c3RlcmIvZXZhbHVhdG9yLnJi
122
122
  - !binary |-
123
123
  bGliL211c3RlcmIvZXh0cmFjdF92YWx1ZXMucmI=
124
+ - !binary |-
125
+ bGliL211c3RlcmIvZXh0cmFjdG9yLnJi
124
126
  - !binary |-
125
127
  bGliL211c3RlcmIvaGFzaF9leHRyYWN0b3IucmI=
126
128
  - !binary |-