musterb 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,10 @@ class Musterb::Evaluator
5
5
  @context = context
6
6
  end
7
7
 
8
+ def current
9
+ @context.value
10
+ end
11
+
8
12
  def block_if(value)
9
13
  return if is_falsy? value
10
14
 
@@ -1,3 +1,3 @@
1
1
  module Musterb
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/musterb.rb CHANGED
@@ -8,9 +8,15 @@ require "musterb/null_extractor"
8
8
  require "musterb/evaluator"
9
9
  require "musterb/chain"
10
10
 
11
+ require "erubis"
12
+
11
13
  module Musterb
12
14
  def self.to_erb(template)
13
15
  musterbifier = Musterbifier.new(template)
14
16
  "<% Musterb::Evaluator.new(Musterb::BindingExtractor.new binding).tap do |musterb| %>#{musterbifier.to_erb}<% end %>"
15
17
  end
18
+
19
+ def self.render(template, values)
20
+ Erubis::Eruby.new(to_erb template).result(values)
21
+ end
16
22
  end
data/spec/musterb_spec.rb CHANGED
@@ -1,27 +1,31 @@
1
1
  require 'erubis'
2
2
 
3
3
  describe Musterb do
4
- def evaluate(template, _binding)
5
- erb = Musterb.to_erb(template)
6
- Erubis::Eruby.new(erb).result(_binding)
7
- end
8
-
9
4
  it "correctly replaces variables" do
10
5
  planet = "World"
11
- evaluate("Hello, {{planet}}!", binding).should eq "Hello, World!"
6
+ erb = Musterb.to_erb("Hello, {{planet}}!")
7
+ Erubis::Eruby.new(erb).result(binding).should eq "Hello, World!"
12
8
  end
13
9
 
14
10
  it "correctly evaluates if a variable is set" do
15
- set_var = "set"
16
- unset_var = nil
17
- evaluate("{{#set_var}}foo{{/set_var}}", binding).should eq "foo"
18
- evaluate("{{#unset_var}}foo{{/unset_var}}", binding).should eq ""
11
+ hash = {:set_var => "set"}
12
+ Musterb.render("{{#set_var}}foo{{/set_var}}", hash).should eq "foo"
13
+ Musterb.render("{{#unset_var}}foo{{/unset_var}}", hash).should eq ""
19
14
  end
20
15
 
21
16
  it "skips blocks for truthy values" do
22
- set_var = "set"
23
- unset_var = nil
24
- evaluate("{{^set_var}}foo{{/set_var}}", binding).should eq ""
25
- evaluate("{{^unset_var}}foo{{/unset_var}}", binding).should eq "foo"
17
+ hash = {:set_var => "set"}
18
+ Musterb.render("{{^set_var}}foo{{/set_var}}", hash).should eq ""
19
+ Musterb.render("{{^unset_var}}foo{{/unset_var}}", hash).should eq "foo"
20
+ end
21
+
22
+ it "can pull out deeply nested values" do
23
+ hash = {:foo => {:bar => {:baz => 42}}}
24
+ Musterb.render("{{foo.bar.baz}}", hash).should eq "42"
25
+ end
26
+
27
+ it "can iterate over values" do
28
+ hash = {:foos => [1, 2, 3]}
29
+ Musterb.render("{{#foos}}{{.}}{{/foos}}", hash).should eq "123"
26
30
  end
27
31
  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.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: