class LtdTemplate::Proxy::String
Public Instance Methods
do_add(opts)
click to toggle source
“Add” (concatenate) strings
# File lib/ltdtemplate/proxy/string.rb, line 61 def do_add (opts) combined = @original if params = opts[:parameters] params.each(:seq) do |key, val| val = rubyversed(val).tpl_text @template.using :string_length, (combined.length + val.length) combined += val end end meter combined end
do_compare(opts)
click to toggle source
Implement string comparison operators
# File lib/ltdtemplate/proxy/string.rb, line 103 def do_compare (opts) if (params = opts[:parameters]) && (params.size(:seq) > 0) diff = rubyversed(params[0]).tpl_text else diff = '' end diff = @original <=> diff case opts[:method] when '<' then diff < 0 when '<=' then diff <= 0 when '==' then diff == 0 when '!=' then diff != 0 when '>=' then diff >= 0 when '>' then diff > 0 end end
do_index(opts)
click to toggle source
Index and rindex str.index(substring[, offset]) str.rindex(substring[, offset]
# File lib/ltdtemplate/proxy/string.rb, line 124 def do_index (opts) substr, offset = '', nil params = opts[:parameters] if params && params.size(:seq) > 0 substr = rubyversed(params[0]).tpl_text end offset = params[1] if params && params.size(:seq) > 1 case opts[:method][0] when 'r' offset = -1 unless offset.is_a? Integer @original.rindex(substr, offset) || -1 else offset = 0 unless offset.is_a? Integer @original.index(substr, offset) || -1 end end
do_join(opts)
click to toggle source
String
join str.join(list)
# File lib/ltdtemplate/proxy/string.rb, line 143 def do_join (opts) params = opts[:parameters] if params && params.size(:seq) > 0 meter(params.values(:seq).map { |val| rubyversed(val).tpl_text }. join(@original)) else '' end end
do_match(opts)
click to toggle source
Match a regular expression
# File lib/ltdtemplate/proxy/string.rb, line 74 def do_match (opts) if (params = opts[:parameters]) && params.size(:seq) > 0 && params[0].is_a?(::Regexp) params[0].in_rubyverse(@template).evaluate :method => 'match', :parameters => Sarah[ @original, *params[1..-1].values ] else nil end end
do_multiply(opts)
click to toggle source
“Multiply” (repeat) strings
# File lib/ltdtemplate/proxy/string.rb, line 84 def do_multiply (opts) str = '' if (params = opts[:parameters]) && params.size(:seq) > 0 times = params[0] if times.is_a? Integer str = @original if times < 0 str = str.reverse times = -times end @template.use :string_total, (str.length * times) @template.using :string_length, (str.length * times) str = str * times end end meter str end
do_range_slice(opts)
click to toggle source
Range and slice: str.range([begin[, end]]) str.slice([begin[, length]])
# File lib/ltdtemplate/proxy/string.rb, line 155 def do_range_slice (opts) op1, op2 = 0, -1 params = opts[:parameters] op1 = params[0] if params && params.size(:seq) > 0 op2 = params[1] if params && params.size(:seq) > 1 if opts[:method][0] == 'r' || op2 < 0 str = @original[op1..op2] else str = @original[op1, op2] end meter(str || '') end
do_replace(opts)
click to toggle source
Replace and replace one str.replace(pattern, replacement) str.replace1(pattern, replacement)
# File lib/ltdtemplate/proxy/string.rb, line 170 def do_replace (opts) if (params = opts[:parameters]) && params.size(:seq) > 1 pat, repl = params[0..1] if opts[:method][-1] == '1' # replace one meter @original.sub(pat, repl) else # replace all meter @original.gsub(pat, repl) end else @original end end
do_split(opts)
click to toggle source
Split str.split(pattern[, limit])
# File lib/ltdtemplate/proxy/string.rb, line 186 def do_split (opts) if opts[:parameters] params = opts[:parameters][0..1].values else params = [] end @original.split(*params).tap { |ary| ary.each { |str| meter str } } end
evaluate(opts = {})
click to toggle source
Evaluate supported methods for strings.
Calls superclass method
LtdTemplate::Value#evaluate
# File lib/ltdtemplate/proxy/string.rb, line 13 def evaluate (opts = {}) case opts[:method] when nil, 'call', 'str', 'string' then @original when 'capcase' then meter @original.capitalize when 'class' then 'String' when 'downcase' then meter @original.downcase when 'flt', 'float' then @original.to_f when 'html' require 'htmlentities' meter(HTMLEntities.new(:html4).encode(@original, :basic, :named, :decimal)) when 'idx', 'index', 'ridx', 'rindex' then do_index opts when 'int' then @original.to_i when 'join' then do_join opts when 'len', 'length' then @original.length when 'match' then do_match opts when 'pcte' meter(@original.gsub(/[^a-z0-9]/i) { |c| sprintf "%%%2x", c.ord }) when 'regexp' if @template.options[:regexp] then ::Regexp.new @original else nil end when 'rep', 'rep1', 'replace', 'replace1' then do_replace opts when 'rng', 'range', 'slc', 'slice' then do_range_slice opts when 'split' then do_split opts when 'type' then 'string' when 'upcase' then meter @original.upcase when '+' then do_add opts when '*' then do_multiply opts when '<', '<=', '==', '!=', '>=', '>' then do_compare opts else super opts end end
meter(str)
click to toggle source
Meter string resource usage
# File lib/ltdtemplate/proxy/string.rb, line 48 def meter (str) # RESOURCE string_total: Combined length of computed strings @template.use :string_total, str.size # RESOURCE string_length: Length of longest modified string @template.using :string_length, str.size str end
tpl_text()
click to toggle source
# File lib/ltdtemplate/proxy/string.rb, line 56 def tpl_text; @original; end