sablon 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/sablon/operations.rb +12 -7
- data/lib/sablon/version.rb +1 -1
- data/test/expression_test.rb +29 -4
- data/test/fixtures/sablon_sample.docx +0 -0
- data/test/fixtures/sablon_template.docx +0 -0
- data/test/fixtures/shopping_list_context.json +6 -0
- data/test/fixtures/shopping_list_sample.docx +0 -0
- data/test/fixtures/shopping_list_template.docx +0 -0
- data/test/sablon_test.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f09a55daf98da6c52eb49e49e19291cdea0d1b11
|
4
|
+
data.tar.gz: 2234c60fb99580f059f4f93ec46009b1b9af3727
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3bfbb49b514729e964717b14797c9af60a491e21f0abed165460c9c9f9987ad4da9040795369b0bd7ba8a1a299eaf279100c2c57135d3c91191b47bce9b65ec
|
7
|
+
data.tar.gz: d88dd5dea55a11d726025b23709c112d979bd119df1524195f7f728c8eb5f64174b89a1ab96d6d9fa4b1d6bd85d634a536b7a477583c78de976386cbce02a637
|
data/README.md
CHANGED
@@ -55,6 +55,11 @@ NOTE: The dot operator can also be used to perform a hash lookup.
|
|
55
55
|
This means that it's not possible to call methods on a hash instance.
|
56
56
|
Sablon will always try to make a lookup instead.
|
57
57
|
|
58
|
+
This works for chained method calls and nested hash lookup as well:
|
59
|
+
|
60
|
+
```
|
61
|
+
«=buyer.address.street»
|
62
|
+
```
|
58
63
|
|
59
64
|
#### Conditionals
|
60
65
|
|
data/lib/sablon/operations.rb
CHANGED
@@ -52,24 +52,29 @@ module Sablon
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
class LookupOrMethodCall < Struct.new(:receiver_expr, :
|
55
|
+
class LookupOrMethodCall < Struct.new(:receiver_expr, :expression)
|
56
56
|
def evaluate(context)
|
57
|
-
receiver = receiver_expr.evaluate(context)
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
if receiver = receiver_expr.evaluate(context)
|
58
|
+
expression.split(".").inject(receiver) do |local, m|
|
59
|
+
case local
|
60
|
+
when Hash
|
61
|
+
local[m]
|
62
|
+
else
|
63
|
+
local.public_send m if local.respond_to?(m)
|
64
|
+
end
|
65
|
+
end
|
61
66
|
end
|
62
67
|
end
|
63
68
|
|
64
69
|
def inspect
|
65
|
-
"«#{receiver_expr.name}.#{
|
70
|
+
"«#{receiver_expr.name}.#{expression}»"
|
66
71
|
end
|
67
72
|
end
|
68
73
|
|
69
74
|
def self.parse(expression)
|
70
75
|
if expression.include?(".")
|
71
76
|
parts = expression.split(".")
|
72
|
-
LookupOrMethodCall.new(Variable.new(parts.
|
77
|
+
LookupOrMethodCall.new(Variable.new(parts.shift), parts.join("."))
|
73
78
|
else
|
74
79
|
Variable.new(expression)
|
75
80
|
end
|
data/lib/sablon/version.rb
CHANGED
data/test/expression_test.rb
CHANGED
@@ -7,7 +7,7 @@ end
|
|
7
7
|
class VariableExpressionTest < Sablon::TestCase
|
8
8
|
def test_lookup_the_variable_in_the_context
|
9
9
|
expr = Sablon::Expression.parse("first_name")
|
10
|
-
assert_equal "Jane", expr.evaluate(
|
10
|
+
assert_equal "Jane", expr.evaluate("first_name" => "Jane", "last_name" => "Doe")
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_inspect
|
@@ -20,24 +20,49 @@ class LookupOrMethodCallTest < Sablon::TestCase
|
|
20
20
|
def test_calls_method_on_object
|
21
21
|
user = OpenStruct.new(first_name: "Jack")
|
22
22
|
expr = Sablon::Expression.parse("user.first_name")
|
23
|
-
assert_equal "Jack", expr.evaluate(
|
23
|
+
assert_equal "Jack", expr.evaluate("user" => user)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_calls_perform_lookup_on_hash_with_string_keys
|
27
27
|
user = {"first_name" => "Jack"}
|
28
28
|
expr = Sablon::Expression.parse("user.first_name")
|
29
|
-
assert_equal "Jack", expr.evaluate(
|
29
|
+
assert_equal "Jack", expr.evaluate("user" => user)
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_calls_perform_lookup_on_hash_with_symbol_keys
|
33
33
|
skip
|
34
34
|
user = {first_name: "Jack"}
|
35
35
|
expr = Sablon::Expression.parse("user.first_name")
|
36
|
-
assert_equal "Jack", expr.evaluate(
|
36
|
+
assert_equal "Jack", expr.evaluate("user" => user)
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_inspect
|
40
40
|
expr = Sablon::Expression.parse("user.first_name")
|
41
41
|
assert_equal "«user.first_name»", expr.inspect
|
42
42
|
end
|
43
|
+
|
44
|
+
def test_calls_chained_methods
|
45
|
+
user = OpenStruct.new(first_name: "Jack", address: OpenStruct.new(line_1: "55A"))
|
46
|
+
expr = Sablon::Expression.parse("user.address.line_1")
|
47
|
+
assert_equal "55A", expr.evaluate("user" => user)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_nested_hash_lookup
|
51
|
+
user = {"address" => {"line_1" => "55A"}}
|
52
|
+
expr = Sablon::Expression.parse("user.address.line_1")
|
53
|
+
assert_equal "55A", expr.evaluate("user" => user)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_mix_hash_lookup_and_method_calls
|
57
|
+
user = OpenStruct.new(address: {"country" => OpenStruct.new(name: "Switzerland")})
|
58
|
+
expr = Sablon::Expression.parse("user.address.country.name")
|
59
|
+
assert_equal "Switzerland", expr.evaluate("user" => user)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_missing_receiver
|
63
|
+
user = OpenStruct.new(first_name: "Jack")
|
64
|
+
expr = Sablon::Expression.parse("user.address.line_1")
|
65
|
+
assert_equal nil, expr.evaluate("user" => user)
|
66
|
+
assert_equal nil, expr.evaluate({})
|
67
|
+
end
|
43
68
|
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/test/sablon_test.rb
CHANGED
@@ -12,7 +12,7 @@ class SablonTest < Sablon::TestCase
|
|
12
12
|
|
13
13
|
def test_generate_document_from_template
|
14
14
|
template = Sablon.template(@base_path + "fixtures/sablon_template.docx")
|
15
|
-
person = OpenStruct.new "first_name" => "Ronald", "last_name" => "Anderson"
|
15
|
+
person = OpenStruct.new "first_name" => "Ronald", "last_name" => "Anderson", "address" => {"street" => "Panda Bay 4A"}
|
16
16
|
item = Struct.new(:index, :label, :rating)
|
17
17
|
position = Struct.new(:duration, :label, :description)
|
18
18
|
language = Struct.new(:name, :skill)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sablon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yves Senn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|