musterb 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/musterb/evaluator.rb +4 -0
- data/lib/musterb/version.rb +1 -1
- data/lib/musterb.rb +6 -0
- data/spec/musterb_spec.rb +18 -14
- metadata +1 -1
data/lib/musterb/evaluator.rb
CHANGED
data/lib/musterb/version.rb
CHANGED
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
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|