Class: LtdTemplate::Value::String

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

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ltdtemplate/value/string.rb', line 38

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



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

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]



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ltdtemplate/value/string.rb', line 90

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_multiply(opts) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ltdtemplate/value/string.rb', line 50

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]])



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

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

#get_value(opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ltdtemplate/value/string.rb', line 21

def get_value (opts = {})
  case opts[:method]
  when nil, 'call', 'str', 'string' then self
  when 'class' then @template.factory :string, 'String'
  when 'flt', 'float' then @template.factory :number, @value.to_f
  when 'int' then @template.factory :number, @value.to_i
  when 'len', 'length' then @template.factory :number, @value.length
  when 'rng', 'range', 'slc', 'slice' then do_range_slice opts
  when 'type' then @template.factory :string, 'string'
  when '+' then do_add opts
  when '*' then do_multiply opts
  when 'idx', 'index', 'ridx', 'rindex' then do_index 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