musterb 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ require "musterb/musterbifier"
3
3
  require "musterb/binding_extractor"
4
4
  require "musterb/hash_extractor"
5
5
  require "musterb/object_extractor"
6
+ require "musterb/null_extractor"
6
7
  require "musterb/evaluator"
7
8
 
8
9
  module Musterb
@@ -4,7 +4,11 @@ class Musterb::Evaluator
4
4
  end
5
5
 
6
6
  def [](symbol)
7
- @context[symbol]
7
+ return @context.value if symbol == "."
8
+ final_context = symbol.split(".").inject(@context) do |con, symbol|
9
+ new_context con[symbol], con
10
+ end
11
+ final_context.value
8
12
  end
9
13
 
10
14
  def block(symbol)
@@ -38,12 +42,14 @@ class Musterb::Evaluator
38
42
  end
39
43
  end
40
44
 
41
- def new_context(value)
45
+ def new_context(value, old_context = @context)
42
46
  case value
43
47
  when Hash
44
- HashExtractor.new(value, @context)
48
+ Musterb::HashExtractor.new(value, old_context)
49
+ when nil
50
+ Musterb::NullExtractor.new(old_context)
45
51
  else
46
- ObjectExtractor.new(value, @context)
52
+ Musterb::ObjectExtractor.new(value, old_context)
47
53
  end
48
54
  end
49
55
 
@@ -1,7 +1,7 @@
1
1
  require 'hashie'
2
2
 
3
- class HashExtractor
4
- attr_reader :parent
3
+ class Musterb::HashExtractor
4
+ attr_reader :parent, :value
5
5
 
6
6
  def initialize(value, parent)
7
7
  @value = to_string_access(value)
@@ -8,15 +8,19 @@ class Musterb::Musterbifier
8
8
  match = $1
9
9
  case match[0]
10
10
  when '#'
11
- "<% musterb.block '#{match[1..-1]}' do %>"
11
+ "<% musterb.block '#{match[1..-1].strip}' do %>"
12
12
  when '^'
13
- "<% musterb.block_unless '#{match[1..-1]}' do %>"
13
+ "<% musterb.block_unless '#{match[1..-1].strip}' do %>"
14
14
  when "/"
15
15
  "<% end %>"
16
16
  when '{'
17
- "<%= musterb['#{match[1..-2]}'] %>"
17
+ "<%= musterb['#{match[1..-2].strip}'] %>"
18
+ when '&'
19
+ "<%= musterb['#{match[1..-1].strip}'] %>"
20
+ when '!'
21
+ ""
18
22
  else
19
- "<%== musterb['#{match}'] %>"
23
+ "<%== musterb['#{match.strip}'] %>"
20
24
  end
21
25
  end
22
26
  end
@@ -0,0 +1,11 @@
1
+ class Musterb::NullExtractor
2
+ attr_reader :value, :parent
3
+
4
+ def initialize(parent)
5
+ @parent = parent
6
+ end
7
+
8
+ def [](value)
9
+ nil
10
+ end
11
+ end
@@ -1,12 +1,12 @@
1
- class ObjectExtractor
2
- attr_reader :parent
1
+ class Musterb::ObjectExtractor
2
+ attr_reader :parent, :value
3
3
 
4
4
  def initialize(value, parent)
5
5
  @value = value
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
@@ -1,3 +1,3 @@
1
1
  module Musterb
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -5,6 +5,21 @@ describe Musterb::Evaluator do
5
5
  evaluator["foo"].should eq "bar"
6
6
  end
7
7
 
8
+ it "pulls out values from ." do
9
+ evaluator = Musterb::Evaluator.new Musterb::ObjectExtractor.new(2, nil)
10
+ evaluator["."].should eq 2
11
+ end
12
+
13
+ it "can do nested values" do
14
+ evaluator = Musterb::Evaluator.new Musterb::ObjectExtractor.new(2, nil)
15
+ evaluator["next.to_s"].should eq "3"
16
+ end
17
+
18
+ it "does not barf when pulling out a value on nil" do
19
+ evaluator = Musterb::Evaluator.new Musterb::HashExtractor.new({:foo => nil}, nil)
20
+ evaluator["foo.bar"].should eq nil
21
+ end
22
+
8
23
  context "block" do
9
24
  it "yields to the block if a value is set" do
10
25
  foo = "bar"
@@ -1,16 +1,16 @@
1
- describe HashExtractor do
1
+ describe Musterb::HashExtractor do
2
2
  it "can pull values out from a hash" do
3
- extractor = HashExtractor.new({"foo" => "bar"}, nil)
3
+ extractor = Musterb::HashExtractor.new({"foo" => "bar"}, nil)
4
4
  extractor["foo"].should eq "bar"
5
5
  end
6
6
 
7
7
  it "can pull values out from a symbol" do
8
- extractor = HashExtractor.new({:foo => "bar"}, nil)
8
+ extractor = Musterb::HashExtractor.new({:foo => "bar"}, nil)
9
9
  extractor["foo"].should eq "bar"
10
10
  end
11
11
 
12
12
  it "delegates to the parent if there is no match" do
13
- extractor = HashExtractor.new({}, HashExtractor.new({:foo => "bar"}, nil))
13
+ extractor = Musterb::HashExtractor.new({}, Musterb::HashExtractor.new({:foo => "bar"}, nil))
14
14
  extractor["foo"].should eq "bar"
15
15
  end
16
16
  end
@@ -11,6 +11,10 @@ describe Musterb::Musterbifier do
11
11
  Musterb::Musterbifier.new("Hello, {{{world}}}!").to_erb.should eq "Hello, <%= musterb['world'] %>!"
12
12
  end
13
13
 
14
+ it "does not escape if it starts with &" do
15
+ Musterb::Musterbifier.new("Hello, {{& world}}!").to_erb.should eq "Hello, <%= musterb['world'] %>!"
16
+ end
17
+
14
18
  it "replaces blocks correctly" do
15
19
  Musterb::Musterbifier.new("{{#cond}}foo{{/cond}}").to_erb.should eq "<% musterb.block 'cond' do %>foo<% end %>"
16
20
  end
@@ -18,4 +22,8 @@ describe Musterb::Musterbifier do
18
22
  it "replaces carrot correctly" do
19
23
  Musterb::Musterbifier.new("{{^cond}}foo{{/cond}}").to_erb.should eq "<% musterb.block_unless 'cond' do %>foo<% end %>"
20
24
  end
25
+
26
+ it "replaces comments with nothing" do
27
+ Musterb::Musterbifier.new("{{! foo\n bar}}").to_erb.should eq ""
28
+ end
21
29
  end
@@ -1,11 +1,11 @@
1
- describe ObjectExtractor do
1
+ describe Musterb::ObjectExtractor do
2
2
  it "calls methods on the object" do
3
- extractor = ObjectExtractor.new(2, nil)
3
+ extractor = Musterb::ObjectExtractor.new(2, nil)
4
4
  extractor["to_s"].should eq "2"
5
5
  end
6
6
 
7
7
  it "delegates to the parent if there it doesn't respnd to something" do
8
- extractor = ObjectExtractor.new(2, ObjectExtractor.new("string", nil))
8
+ extractor = Musterb::ObjectExtractor.new(2, Musterb::ObjectExtractor.new("string", nil))
9
9
  extractor["upcase"].should eq "STRING"
10
10
  end
11
11
  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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -89,6 +89,8 @@ files:
89
89
  bGliL211c3RlcmIvaGFzaF9leHRyYWN0b3IucmI=
90
90
  - !binary |-
91
91
  bGliL211c3RlcmIvbXVzdGVyYmlmaWVyLnJi
92
+ - !binary |-
93
+ bGliL211c3RlcmIvbnVsbF9leHRyYWN0b3IucmI=
92
94
  - !binary |-
93
95
  bGliL211c3RlcmIvb2JqZWN0X2V4dHJhY3Rvci5yYg==
94
96
  - !binary |-