Class: LtdTemplate::Value::String

Inherits:
Code
  • Object
show all
Defined in:
lib/ltdtemplate/value/string.rb

Direct Known Subclasses

Regexp

Instance Attribute Summary

Attributes inherited from Code

#tpl_methods

Instance Method Summary collapse

Methods inherited from Code

#do_method, #do_set, #get_item, #has_item?, instance, #is_set?, #set_item, #set_value

Constructor Details

#initialize(template, value) ⇒ String

Returns a new instance of String.



11
12
13
14
15
# File 'lib/ltdtemplate/value/string.rb', line 11

def initialize (template, value)
  super template
  template.use :string_total, value.length
  @value = value
end

Instance Method Details

#do_add(opts) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ltdtemplate/value/string.rb', line 54

def do_add (opts)
  combined = @value
  if params = opts[:parameters]
 params.positional.each do |tval|
    part = tval.to_text
    @template.using :string_length, (combined.length + part.length)
    combined += part
 end
  end
  @template.factory :string, combined
end

#do_compare(opts) ⇒ Object

Implement string comparison operators



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ltdtemplate/value/string.rb', line 85

def do_compare (opts)
  if params = opts[:parameters] and params.positional.size > 0
 diff = params.positional[0].to_text
  else
 diff = ''
  end

  diff = @value <=> diff
  @template.factory :boolean, 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]



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ltdtemplate/value/string.rb', line 106

def do_index (opts)
  substr, offset = '', nil
  params = params.positional if params = opts[:parameters]
  substr = params[0].get_value.to_text if params and params.size > 0
  offset = params[1].get_value.to_native if params and params.size > 1
  case opts[:method][0]
  when 'r'
 offset = -1 unless offset.is_a? Integer
 @template.factory :number, (@value.rindex(substr, offset) || -1)
  else
 offset = 0 unless offset.is_a? Integer
 @template.factory :number, (@value.index(substr, offset) || -1)
  end
end

#do_join(opts) ⇒ Object

String join str.join(list)



123
124
125
126
127
128
129
130
131
# File 'lib/ltdtemplate/value/string.rb', line 123

def do_join (opts)
  params = params.positional if params = opts[:parameters]
  if params and params.size > 0
 @template.factory(:string, params.map do |val|
   val.get_value.to_text end.join(@value))
  else
 @template.factory :string, ''
  end
end

#do_multiply(opts) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ltdtemplate/value/string.rb', line 66

def do_multiply (opts)
  str = ''
  params = params.positional if params = opts[:parameters]
  if params and params.size > 0
 times = params[0].to_native
 if times.is_a? Integer
    str = @value
    if times < 0
  str = str.reverse
  times = -times
    end
    @template.using :string_length, (str.length * times)
    str = str * times
 end
  end
  @template.factory :string, str
end

#do_range_slice(opts) ⇒ Object

Range and slice: str.range([begin[, end]]) str.slice([begin[, length]])



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ltdtemplate/value/string.rb', line 136

def do_range_slice (opts)
  op1, op2 = 0, -1
  params = params.positional if params = opts[:parameters]
  op1 = params[0].get_value.to_native if params and params.size > 0
  op2 = params[1].get_value.to_native if params and params.size > 1
  if opts[:method][0] == 'r' or op2 < 0
 str = @value[op1..op2]
  else
 str = @value[op1, op2]
  end
  @template.factory :string, (str || '')
end

#do_regexp(opts) ⇒ Object

Convert to regexp string



150
151
152
153
# File 'lib/ltdtemplate/value/string.rb', line 150

def do_regexp (opts)
  (@template.limits[:regexp] != false) ? self :
  (LtdTemplate::Value::Regexp.new @template, @value)
end

#do_replace(opts) ⇒ Object

Replace and replace one str.replace(pattern, replacement) str.replace1(pattern, replacement)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ltdtemplate/value/string.rb', line 158

def do_replace (opts)
  params = params.positional if params = opts[:parameters]
  if params.size > 1
 pat = params[0].get_value
 pat = pat.respond_to?(:to_regexp) ? pat.to_regexp : pat.to_native
 repl = params[1].get_value.to_native
 if opts[:method][-1] == '1'
    # replace one
    @template.factory :string, @value.sub(pat, repl)
 else
    # replace all
    @template.factory :string, @value.gsub(pat, repl)
 end
  else
 self
  end
end

#do_split(opts) ⇒ Object

Split str.split(pattern[, limit])



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ltdtemplate/value/string.rb', line 178

def do_split (opts)
  split_opts = []
  params = params.positional if params = opts[:parameters]
  if params.size > 0
 pattern = params[0].get_value
 split_opts << ((pattern.respond_to? :to_regexp) ?
   pattern.to_regexp : pattern.to_native)
 split_opts << params[1].get_value.to_native if params.size > 1
  end
  @template.factory(:array).set_from_array @value.split(*split_opts)
end

#get_value(opts = {}) ⇒ Object



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
46
47
48
49
# File 'lib/ltdtemplate/value/string.rb', line 21

def get_value (opts = {})
  case opts[:method]
  when nil, 'call', 'str', 'string' then self
  when 'capcase' then @template.factory :string, @value.capitalize
  when 'class' then @template.factory :string, 'String'
  when 'downcase' then @template.factory :string, @value.downcase
  when 'flt', 'float' then @template.factory :number, @value.to_f
  when 'html'
 require 'htmlentities'
 @template.factory :string, HTMLEntities.new(:html4).
   encode(@value, :basic, :named, :decimal)
  when 'idx', 'index', 'ridx', 'rindex' then do_index opts
  when 'int' then @template.factory :number, @value.to_i
  when 'join' then do_join opts
  when 'len', 'length' then @template.factory :number, @value.length
  when 'pcte' then @template.factory(:string,
    @value.gsub(/[^a-z0-9]/i) { |c| sprintf "%%%2x", c.ord })
  when 'regexp' then do_regexp opts
  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 @template.factory :string, 'string'
  when 'upcase' then @template.factory :string, @value.upcase
  when '+' then do_add opts
  when '*' then do_multiply opts
  when '<', '<=', '==', '!=', '>=', '>' then do_compare opts
  else do_method opts, 'String'
  end
end

#to_booleanObject



17
# File 'lib/ltdtemplate/value/string.rb', line 17

def to_boolean; true; end

#to_nativeObject



18
# File 'lib/ltdtemplate/value/string.rb', line 18

def to_native; @value; end

#to_textObject



19
# File 'lib/ltdtemplate/value/string.rb', line 19

def to_text; @value; end

#typeObject

Type (for :missing_method callback)



52
# File 'lib/ltdtemplate/value/string.rb', line 52

def type; :string; end