ltdtemplate 0.2.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +10 -1
- data/Gemfile +2 -1
- data/RESOURCES +18 -32
- data/TEMPLATE_MANUAL.html +126 -47
- data/lib/ltdtemplate.rb +352 -242
- data/lib/ltdtemplate/code.rb +14 -87
- data/lib/ltdtemplate/code/call.rb +20 -16
- data/lib/ltdtemplate/code/parameters.rb +28 -31
- data/lib/ltdtemplate/code/sequence.rb +39 -0
- data/lib/ltdtemplate/code/subscript.rb +57 -50
- data/lib/ltdtemplate/code/variable.rb +22 -39
- data/lib/ltdtemplate/proxy.rb +26 -0
- data/lib/ltdtemplate/proxy/array.rb +258 -0
- data/lib/ltdtemplate/proxy/boolean.rb +74 -0
- data/lib/ltdtemplate/proxy/match.rb +40 -0
- data/lib/ltdtemplate/proxy/nil.rb +27 -0
- data/lib/ltdtemplate/proxy/number.rb +77 -0
- data/lib/ltdtemplate/proxy/regexp.rb +74 -0
- data/lib/ltdtemplate/proxy/string.rb +196 -0
- data/lib/ltdtemplate/value.rb +94 -0
- data/lib/ltdtemplate/value/array_splat.rb +34 -0
- data/lib/ltdtemplate/value/code_block.rb +21 -17
- data/lib/ltdtemplate/value/namespace.rb +77 -79
- data/ltdtemplate.gemspec +2 -2
- data/test/04number.rb +0 -7
- data/test/05string.rb +0 -7
- data/test/06array.rb +0 -9
- data/test/07each.rb +3 -3
- data/test/08interpolate.rb +1 -1
- data/test/10missing_meth.rb +1 -1
- data/test/11classes.rb +9 -9
- metadata +15 -13
- data/lib/ltdtemplate/code/code_block.rb +0 -30
- data/lib/ltdtemplate/value/array.rb +0 -210
- data/lib/ltdtemplate/value/boolean.rb +0 -82
- data/lib/ltdtemplate/value/nil.rb +0 -30
- data/lib/ltdtemplate/value/number.rb +0 -96
- data/lib/ltdtemplate/value/string.rb +0 -215
- data/lib/test.rb +0 -10
- data/test/03tpl_singletons.rb +0 -48
@@ -1,215 +0,0 @@
|
|
1
|
-
# LtdTemplate::Value::String - Represents a string in an LtdTemplate
|
2
|
-
#
|
3
|
-
# @author Brian Katzung <briank@kappacs.com>, Kappa Computer Solutions, LLC
|
4
|
-
# @copyright 2013 Brian Katzung and Kappa Computer Solutions, LLC
|
5
|
-
# @license MIT License
|
6
|
-
|
7
|
-
require 'ltdtemplate/code'
|
8
|
-
|
9
|
-
class LtdTemplate::Value::String < LtdTemplate::Code
|
10
|
-
|
11
|
-
def initialize (template, value)
|
12
|
-
super template
|
13
|
-
template.use :string_total, value.length
|
14
|
-
@value = value
|
15
|
-
end
|
16
|
-
|
17
|
-
def to_boolean; true; end
|
18
|
-
def to_native; @value; end
|
19
|
-
def to_text; @value; end
|
20
|
-
|
21
|
-
def get_value (opts = {})
|
22
|
-
case opts[:method]
|
23
|
-
when nil, 'call', 'str', 'string' then self
|
24
|
-
when 'capcase' then @template.factory :string, @value.capitalize
|
25
|
-
when 'class' then @template.factory :string, 'String'
|
26
|
-
when 'downcase' then @template.factory :string, @value.downcase
|
27
|
-
when 'flt', 'float' then @template.factory :number, @value.to_f
|
28
|
-
when 'html'
|
29
|
-
require 'htmlentities'
|
30
|
-
@template.factory :string, HTMLEntities.new(:html4).
|
31
|
-
encode(@value, :basic, :named, :decimal)
|
32
|
-
when 'idx', 'index', 'ridx', 'rindex' then do_index opts
|
33
|
-
when 'int' then @template.factory :number, @value.to_i
|
34
|
-
when 'join' then do_join opts
|
35
|
-
when 'len', 'length' then @template.factory :number, @value.length
|
36
|
-
when 'pcte' then @template.factory(:string,
|
37
|
-
@value.gsub(/[^a-z0-9]/i) { |c| sprintf "%%%2x", c.ord })
|
38
|
-
when 'regexp' then do_regexp opts
|
39
|
-
when 'rep', 'rep1', 'replace', 'replace1' then do_replace opts
|
40
|
-
when 'rng', 'range', 'slc', 'slice' then do_range_slice opts
|
41
|
-
when 'split' then do_split opts
|
42
|
-
when 'type' then @template.factory :string, 'string'
|
43
|
-
when 'upcase' then @template.factory :string, @value.upcase
|
44
|
-
when '+' then do_add opts
|
45
|
-
when '*' then do_multiply opts
|
46
|
-
when '<', '<=', '==', '!=', '>=', '>' then do_compare opts
|
47
|
-
else do_method opts, 'String'
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
# Type (for :missing_method callback)
|
52
|
-
def type; :string; end
|
53
|
-
|
54
|
-
def do_add (opts)
|
55
|
-
combined = @value
|
56
|
-
if params = opts[:parameters]
|
57
|
-
params.positional.each do |tval|
|
58
|
-
part = tval.to_text
|
59
|
-
@template.using :string_length, (combined.length + part.length)
|
60
|
-
combined += part
|
61
|
-
end
|
62
|
-
end
|
63
|
-
@template.factory :string, combined
|
64
|
-
end
|
65
|
-
|
66
|
-
def do_multiply (opts)
|
67
|
-
str = ''
|
68
|
-
params = params.positional if params = opts[:parameters]
|
69
|
-
if params and params.size > 0
|
70
|
-
times = params[0].to_native
|
71
|
-
if times.is_a? Integer
|
72
|
-
str = @value
|
73
|
-
if times < 0
|
74
|
-
str = str.reverse
|
75
|
-
times = -times
|
76
|
-
end
|
77
|
-
@template.using :string_length, (str.length * times)
|
78
|
-
str = str * times
|
79
|
-
end
|
80
|
-
end
|
81
|
-
@template.factory :string, str
|
82
|
-
end
|
83
|
-
|
84
|
-
# Implement string comparison operators
|
85
|
-
def do_compare (opts)
|
86
|
-
if params = opts[:parameters] and params.positional.size > 0
|
87
|
-
diff = params.positional[0].to_text
|
88
|
-
else
|
89
|
-
diff = ''
|
90
|
-
end
|
91
|
-
|
92
|
-
diff = @value <=> diff
|
93
|
-
@template.factory :boolean, case opts[:method]
|
94
|
-
when '<' then diff < 0
|
95
|
-
when '<=' then diff <= 0
|
96
|
-
when '==' then diff == 0
|
97
|
-
when '!=' then diff != 0
|
98
|
-
when '>=' then diff >= 0
|
99
|
-
when '>' then diff > 0
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
# Index and rindex
|
104
|
-
# str.index(substring[, offset])
|
105
|
-
# str.rindex(substring[, offset]
|
106
|
-
def do_index (opts)
|
107
|
-
substr, offset = '', nil
|
108
|
-
params = params.positional if params = opts[:parameters]
|
109
|
-
substr = params[0].get_value.to_text if params and params.size > 0
|
110
|
-
offset = params[1].get_value.to_native if params and params.size > 1
|
111
|
-
case opts[:method][0]
|
112
|
-
when 'r'
|
113
|
-
offset = -1 unless offset.is_a? Integer
|
114
|
-
@template.factory :number, (@value.rindex(substr, offset) || -1)
|
115
|
-
else
|
116
|
-
offset = 0 unless offset.is_a? Integer
|
117
|
-
@template.factory :number, (@value.index(substr, offset) || -1)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
# String join
|
122
|
-
# str.join(list)
|
123
|
-
def do_join (opts)
|
124
|
-
params = params.positional if params = opts[:parameters]
|
125
|
-
if params and params.size > 0
|
126
|
-
@template.factory(:string, params.map do |val|
|
127
|
-
val.get_value.to_text end.join(@value))
|
128
|
-
else
|
129
|
-
@template.factory :string, ''
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
# Range and slice:
|
134
|
-
# str.range([begin[, end]])
|
135
|
-
# str.slice([begin[, length]])
|
136
|
-
def do_range_slice (opts)
|
137
|
-
op1, op2 = 0, -1
|
138
|
-
params = params.positional if params = opts[:parameters]
|
139
|
-
op1 = params[0].get_value.to_native if params and params.size > 0
|
140
|
-
op2 = params[1].get_value.to_native if params and params.size > 1
|
141
|
-
if opts[:method][0] == 'r' or op2 < 0
|
142
|
-
str = @value[op1..op2]
|
143
|
-
else
|
144
|
-
str = @value[op1, op2]
|
145
|
-
end
|
146
|
-
@template.factory :string, (str || '')
|
147
|
-
end
|
148
|
-
|
149
|
-
# Convert to regexp string
|
150
|
-
def do_regexp (opts)
|
151
|
-
(@template.limits[:regexp] != false) ? self :
|
152
|
-
(LtdTemplate::Value::Regexp.new @template, @value)
|
153
|
-
end
|
154
|
-
|
155
|
-
# Replace and replace one
|
156
|
-
# str.replace(pattern, replacement)
|
157
|
-
# str.replace1(pattern, replacement)
|
158
|
-
def do_replace (opts)
|
159
|
-
params = params.positional if params = opts[:parameters]
|
160
|
-
if params.size > 1
|
161
|
-
pat = params[0].get_value
|
162
|
-
pat = pat.respond_to?(:to_regexp) ? pat.to_regexp : pat.to_native
|
163
|
-
repl = params[1].get_value.to_native
|
164
|
-
if opts[:method][-1] == '1'
|
165
|
-
# replace one
|
166
|
-
@template.factory :string, @value.sub(pat, repl)
|
167
|
-
else
|
168
|
-
# replace all
|
169
|
-
@template.factory :string, @value.gsub(pat, repl)
|
170
|
-
end
|
171
|
-
else
|
172
|
-
self
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
# Split
|
177
|
-
# str.split(pattern[, limit])
|
178
|
-
def do_split (opts)
|
179
|
-
split_opts = []
|
180
|
-
params = params.positional if params = opts[:parameters]
|
181
|
-
if params.size > 0
|
182
|
-
pattern = params[0].get_value
|
183
|
-
split_opts << ((pattern.respond_to? :to_regexp) ?
|
184
|
-
pattern.to_regexp : pattern.to_native)
|
185
|
-
split_opts << params[1].get_value.to_native if params.size > 1
|
186
|
-
end
|
187
|
-
@template.factory(:array).set_from_array @value.split(*split_opts)
|
188
|
-
end
|
189
|
-
|
190
|
-
end
|
191
|
-
|
192
|
-
class LtdTemplate::Value::Regexp < LtdTemplate::Value::String
|
193
|
-
|
194
|
-
def initialize (template, value)
|
195
|
-
super template, value
|
196
|
-
@options = 0
|
197
|
-
end
|
198
|
-
|
199
|
-
def to_regexp
|
200
|
-
@regexp ||= Regexp.new @value, @options
|
201
|
-
end
|
202
|
-
|
203
|
-
def get_value (opts = {})
|
204
|
-
case opts[:method]
|
205
|
-
when 'ci', 'ignorecase' then @options |= Regexp::IGNORECASE; self
|
206
|
-
when 'ext', 'extended' then @options |= Regexp::EXTENDED; self
|
207
|
-
when 'multi', 'multiline' then @options |= Regexp::MULTILINE; self
|
208
|
-
when 'type' then @template.factory :string, 'regexp'
|
209
|
-
else super
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
end
|
214
|
-
|
215
|
-
# END
|
data/lib/test.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
require 'ltdtemplate'
|
2
|
-
t = LtdTemplate.new
|
3
|
-
|
4
|
-
t.parse '<<"".regexp.type>>'
|
5
|
-
print t.render, "\n"
|
6
|
-
t.limits[:regexp] = false
|
7
|
-
print t.render, "\n"
|
8
|
-
|
9
|
-
#t.parse '<<"+".join(1,2,"three")>>'
|
10
|
-
#t.parse '<<"Now IS the time".split("[^a-z]".regexp.ci).join("/")>>'
|
data/test/03tpl_singletons.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'ltdtemplate'
|
3
|
-
|
4
|
-
class TestLtdTemplate_03 < MiniTest::Unit::TestCase
|
5
|
-
|
6
|
-
def setup
|
7
|
-
@tpl1 = LtdTemplate.new
|
8
|
-
@tpl2 = LtdTemplate.new
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_nil
|
12
|
-
nil1a = @tpl1.factory :nil
|
13
|
-
nil1b = @tpl1.factory :nil
|
14
|
-
assert_equal nil1a.object_id, nil1b.object_id, "Shared nil in tpl1"
|
15
|
-
nil2a = @tpl2.factory :nil
|
16
|
-
nil2b = @tpl2.factory :nil
|
17
|
-
assert_equal nil2a.object_id, nil2b.object_id, "Shared nil in tpl2"
|
18
|
-
refute_equal nil1a.object_id, nil2a.object_id,
|
19
|
-
"Different nil in tpl1, tpl2"
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_boolean
|
23
|
-
true1a = @tpl1.factory :boolean, true
|
24
|
-
true1b = @tpl1.factory :boolean, true
|
25
|
-
assert_equal true1a.object_id, true1b.object_id,
|
26
|
-
"Shared true in tpl1"
|
27
|
-
true2a = @tpl2.factory :boolean, true
|
28
|
-
true2b = @tpl2.factory :boolean, true
|
29
|
-
assert_equal true2a.object_id, true2b.object_id,
|
30
|
-
"Shared true in tpl2"
|
31
|
-
refute_equal true1a.object_id, true2a.object_id,
|
32
|
-
"Different true in tpl1, tpl2"
|
33
|
-
|
34
|
-
false1a = @tpl1.factory :boolean, false
|
35
|
-
false1b = @tpl1.factory :boolean, false
|
36
|
-
assert_equal false1a.object_id, false1b.object_id,
|
37
|
-
"Shared false in tpl1"
|
38
|
-
false2a = @tpl2.factory :boolean, false
|
39
|
-
false2b = @tpl2.factory :boolean, false
|
40
|
-
assert_equal false2a.object_id, false2b.object_id
|
41
|
-
"Shared false in tpl2"
|
42
|
-
refute_equal false1a.object_id, false2a.object_id
|
43
|
-
"Different false in tpl1, tpl2"
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
# END
|