Class: LtdTemplate::Proxy::String
- Inherits:
-
LtdTemplate::Proxy
- Object
- LtdTemplate::Proxy
- LtdTemplate::Proxy::String
- Defined in:
- lib/ltdtemplate/proxy/string.rb
Instance Attribute Summary
Attributes included from Value
Instance Method Summary collapse
-
#do_add(opts) ⇒ Object
“Add” (concatenate) strings.
-
#do_compare(opts) ⇒ Object
Implement string comparison operators.
-
#do_index(opts) ⇒ Object
Index and rindex str.index(substring[, offset]) str.rindex(substring[, offset].
-
#do_join(opts) ⇒ Object
String join str.join(list).
-
#do_match(opts) ⇒ Object
Match a regular expression.
-
#do_multiply(opts) ⇒ Object
“Multiply” (repeat) strings.
-
#do_range_slice(opts) ⇒ Object
Range and slice: str.range([begin[, end]]) str.slice([begin[, length]]).
-
#do_replace(opts) ⇒ Object
Replace and replace one str.replace(pattern, replacement) str.replace1(pattern, replacement).
-
#do_split(opts) ⇒ Object
Split str.split(pattern[, limit]).
-
#evaluate(opts = {}) ⇒ Object
Evaluate supported methods for strings.
-
#meter(str) ⇒ Object
Meter string resource usage.
- #tpl_text ⇒ Object
Methods inherited from LtdTemplate::Proxy
#initialize, #rubyverse_original, #rubyversed
Methods included from Value
#do_methods, #do_run_method, included, #initialize, #inspect, #rubyversed, #tpl_boolean
Constructor Details
This class inherits a constructor from LtdTemplate::Proxy
Instance Method Details
#do_add(opts) ⇒ Object
“Add” (concatenate) strings
61 62 63 64 65 66 67 68 69 70 71 |
# 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) ⇒ Object
Implement string comparison operators
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# 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) ⇒ Object
Index and rindex str.index(substring[, offset]) str.rindex(substring[, offset]
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# 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) ⇒ Object
String join str.join(list)
143 144 145 146 147 148 149 150 |
# 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) ⇒ Object
Match a regular expression
74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
“Multiply” (repeat) strings
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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) ⇒ Object
Range and slice: str.range([begin[, end]]) str.slice([begin[, length]])
155 156 157 158 159 160 161 162 163 164 165 |
# 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) ⇒ Object
Replace and replace one str.replace(pattern, replacement) str.replace1(pattern, replacement)
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# 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) ⇒ Object
Split str.split(pattern[, limit])
186 187 188 189 190 191 192 |
# 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 = {}) ⇒ Object
Evaluate supported methods for strings.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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.[: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) ⇒ Object
Meter string resource usage
48 49 50 51 52 53 54 |
# 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 ⇒ Object
56 |
# File 'lib/ltdtemplate/proxy/string.rb', line 56 def tpl_text; @original; end |