snippr 0.15.22 → 0.15.23
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/snippr/processor/dynamics.rb +40 -33
- data/snippr.gemspec +1 -1
- data/spec/snippr/processor/dynamics_spec.rb +34 -11
- 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: 054173010859c69d009993fdcb3c2b3dba1c480e
|
4
|
+
data.tar.gz: 3aff7b7072677405dcd33849f675975daa1a4392
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf7d237b5c1f753bf381b9b16b98981870a52af3e8512cfdf4003f37ba5f321c2863390e28862973104f27249b68067934d109fce5a0ed8a8c21243171e4b48b
|
7
|
+
data.tar.gz: a2f91dd07711ce6fc54172f9f2e7c713c200797addcfd564ae02911501adb9734f415188fdf6434cc674d1f73b4fde696f6fb6e4872c79ad5f7e0f35ec46c225
|
@@ -9,50 +9,57 @@ module Snippr
|
|
9
9
|
class Dynamics
|
10
10
|
|
11
11
|
def process(content, opts = {})
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
matches.each do |match_data|
|
17
|
-
replacement = match_data[:all]
|
18
|
-
placeholder = match_data[:placeholder].strip.to_sym
|
19
|
-
value = opts[placeholder]
|
20
|
-
if match_data[:method] && (value.respond_to?(match_data[:method]) || match_data[:respond_to_check] == "!")
|
21
|
-
params = (match_data[:parameters] || "").gsub(/[\t\r\n]/,"").split("\",\"")
|
22
|
-
replacement = value.send(match_data[:method], *params).to_s
|
23
|
-
elsif match_data[:method]
|
24
|
-
replacement = match_data[:all]
|
25
|
-
elsif value
|
12
|
+
opts.inject(content) do |c, pv|
|
13
|
+
placeholder, value = pv
|
14
|
+
c.gsub(regex(placeholder)) do |match|
|
26
15
|
replacement = value.to_s
|
16
|
+
# remember values since gsub will reset all regex $ matches
|
17
|
+
default = $5
|
18
|
+
method = $3
|
19
|
+
safety = $6
|
20
|
+
if $3 && (value.respond_to?($3) || $2 == "!")
|
21
|
+
params = recursive_process(($4 || "").gsub(/[\t\r\n]/,""), opts).split("\",\"")
|
22
|
+
replacement = value.send(method, *params).to_s
|
23
|
+
elsif $3
|
24
|
+
replacement = match
|
25
|
+
end
|
26
|
+
replacement = default if replacement.empty? && default
|
27
|
+
replacement += safety.html_safe if safety
|
28
|
+
replacement
|
27
29
|
end
|
28
|
-
|
29
|
-
# default set?
|
30
|
-
replacement = match_data[:default_when_empty].strip if replacement.empty? && match_data[:default_when_empty]
|
31
|
-
content.gsub!(match_data[:all], replacement)
|
32
30
|
end
|
33
|
-
content
|
34
31
|
end
|
35
32
|
|
36
33
|
private
|
37
34
|
|
38
|
-
def regex
|
35
|
+
def regex(placeholder)
|
39
36
|
%r{
|
40
|
-
(
|
41
|
-
\{
|
42
|
-
(
|
43
|
-
|
44
|
-
(?:\.(
|
45
|
-
\(
|
46
|
-
["]?
|
47
|
-
(
|
48
|
-
["]?
|
49
|
-
\))?
|
50
|
-
(
|
51
|
-
\}
|
52
|
-
)
|
37
|
+
( # $1: collect all but safety guard at the end
|
38
|
+
\{ # start of dynamic value
|
39
|
+
(!?) # $2: use ! to call the method on an object even if :respond_to fails
|
40
|
+
#{placeholder} # variable holding value or object
|
41
|
+
(?:\.(.*?) # $3: about to call an method on the 'placeholder'
|
42
|
+
\( # non-optional bracket to merk method call
|
43
|
+
["]? # optional opening double quote
|
44
|
+
(.*?) # $4: parameters for method call
|
45
|
+
["]? # optional closing double quote
|
46
|
+
\))? # mandatory closing bracket and group end
|
47
|
+
(?:\|(.*?))? # $5: optional default value when snippet content empty
|
48
|
+
\} # and thats it
|
49
|
+
) # end capture of variable area
|
50
|
+
([^"]|$) # $6: this allows he capture of method calls in method calls
|
53
51
|
}xm
|
54
52
|
end
|
55
53
|
|
54
|
+
def recursive_process(parameter_string, opts)
|
55
|
+
# simple check if there is something to do
|
56
|
+
if parameter_string.include?("{")
|
57
|
+
process(parameter_string, opts)
|
58
|
+
else
|
59
|
+
parameter_string
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
56
63
|
end
|
57
64
|
|
58
65
|
end
|
data/snippr.gemspec
CHANGED
@@ -43,8 +43,14 @@ describe Snippr::Processor::Dynamics do
|
|
43
43
|
expect(subject.process(tpl, :var => Klass.new)).to eq("An instance METHOD WITH PARAMETER1 AND PARAMETER2")
|
44
44
|
end
|
45
45
|
|
46
|
+
it "allows usage of snippr substitution inside method call parameters" do
|
47
|
+
pending 'fix it or use version 0.15.19'
|
48
|
+
tpl = 'An instance {var.method3("{var2.method()}","PARAMETER2")}'
|
49
|
+
expect(subject.process(tpl, :var => Klass.new, :var2 => Klass.new)).to eq('An instance METHOD WITH METHOD AND PARAMETER2')
|
50
|
+
end
|
51
|
+
|
46
52
|
it "keeps the {snip} if calling a method but the method is not defined" do
|
47
|
-
|
53
|
+
expect(subject.process("An instance {var.method_not_exist()}", :var => Klass.new)).to eq("An instance {var.method_not_exist()}")
|
48
54
|
end
|
49
55
|
|
50
56
|
it "calls a bang(!) method even if the receiver does not respond_to the method" do
|
@@ -52,16 +58,6 @@ describe Snippr::Processor::Dynamics do
|
|
52
58
|
expect { subject.process(tpl, :var => Klass.new) }.to raise_error(NoMethodError)
|
53
59
|
end
|
54
60
|
|
55
|
-
it "defaults the value if the content is empty" do
|
56
|
-
tpl = "{empty|default}"
|
57
|
-
expect(subject.process(tpl, empty: "")).to eq "default"
|
58
|
-
end
|
59
|
-
|
60
|
-
it "defaults the value if the content is present" do
|
61
|
-
tpl = "{var.method4()|default2}"
|
62
|
-
expect(subject.process(tpl, var: Klass.new )).to eq "default2"
|
63
|
-
end
|
64
|
-
|
65
61
|
it "leaves the dynamic vslue untouched if no replacement and default exists" do
|
66
62
|
tpl = <<-HEREDOC
|
67
63
|
.clazz {
|
@@ -73,4 +69,31 @@ describe Snippr::Processor::Dynamics do
|
|
73
69
|
expect(subject.process(tpl)).to eq " .clazz {\n }\n\n .clazz {}\n </style>\n"
|
74
70
|
end
|
75
71
|
|
72
|
+
context "default handling" do
|
73
|
+
it "defaults the value if the content is empty" do
|
74
|
+
expect(subject.process("{empty|default}", empty: "")).to eq "default"
|
75
|
+
end
|
76
|
+
|
77
|
+
it "defaults the value if the method calls return value is empty" do
|
78
|
+
expect(subject.process("{var.method4()|default2}", var: Klass.new )).to eq "default2"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "nested placeholders" do
|
83
|
+
it "allows nested placeholders when calling a method" do
|
84
|
+
tpl = 'An instance {var.method3("{var2}","PARAMETER2")}'
|
85
|
+
expect(subject.process(tpl, :var => Klass.new, :var2 => "VAR")).to eq("An instance METHOD WITH VAR AND PARAMETER2")
|
86
|
+
end
|
87
|
+
|
88
|
+
it "allows nested placeholders with method calls when calling a method" do
|
89
|
+
tpl = 'An instance {var.method3("{var2.method()}","PARAMETER2")}'
|
90
|
+
expect(subject.process(tpl, :var => Klass.new, :var2 => Klass.new)).to eq("An instance METHOD WITH METHOD AND PARAMETER2")
|
91
|
+
end
|
92
|
+
|
93
|
+
it "allows nested placeholders with method calls on the same object when calling a method" do
|
94
|
+
tpl = 'An instance {var.method2("{var.method2("{var.method3("A","B")}")}")}'
|
95
|
+
expect(subject.process(tpl, :var => Klass.new)).to eq("An instance METHOD WITH METHOD WITH METHOD WITH A AND B")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
76
99
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snippr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.15.
|
4
|
+
version: 0.15.23
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Harrington
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-06-
|
13
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: i18n
|