Class: LtdTemplate::Value::Array

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

Direct Known Subclasses

Namespace

Instance Attribute Summary collapse

Attributes inherited from Code

#tpl_methods

Instance Method Summary collapse

Methods inherited from Code

#do_method, #do_set, instance, #is_set?

Constructor Details

#initialize(template) ⇒ Array

Returns a new instance of Array.



15
16
17
18
19
20
# File 'lib/ltdtemplate/value/array.rb', line 15

def initialize (template)
	super template
	@sarah = Sarah.new
	@scalar = false
	@template.use :arrays
end

Instance Attribute Details

#sarahObject (readonly)

Returns the value of attribute sarah.



13
14
15
# File 'lib/ltdtemplate/value/array.rb', line 13

def sarah
  @sarah
end

Instance Method Details

#clearObject

Clear all current positional and named values



78
79
80
81
82
# File 'lib/ltdtemplate/value/array.rb', line 78

def clear
	@sarah.clear
	@scalar = false
	self
end

#do_join(opts) ⇒ Object

Combine array element values into a string



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ltdtemplate/value/array.rb', line 116

def do_join (opts)
	two = first = middle = last = ''
	if params = opts[:parameters]
 params = params.positional
 if params.size > 3
		two, first, middle, last =
params[0..3].map { |val| val.get_value.to_text }
 elsif params.size > 0
		two = first = middle = last = params[0].get_value.to_text
 end
	end

	text = @sarah.seq.map { |val| val.get_value.to_text }
	@template.factory :string, case text.size
	when 0 then ''
	when 1 then text[0]
	when 2 then "#{text[0]}#{two}#{text[1]}"
	else "#{text[0]}#{first}" + text[1..-2].join(middle) +
	  "#{last}#{text[-1]}"
	end
end

#do_pop(opts) ⇒ Object



138
139
140
# File 'lib/ltdtemplate/value/array.rb', line 138

def do_pop (opts)
	@sarah.pop || @template.factory(:nil)
end

#do_push(opts) ⇒ Object



142
143
144
145
# File 'lib/ltdtemplate/value/array.rb', line 142

def do_push (opts)
	if params = opts[:parameters] then @sarah.append! params.sarah end
	@template.factory :nil
end

#do_shift(opts) ⇒ Object



147
148
149
# File 'lib/ltdtemplate/value/array.rb', line 147

def do_shift (opts)
	@sarah.shift || @template.factory(:nil)
end

#do_unshift(opts) ⇒ Object



151
152
153
154
# File 'lib/ltdtemplate/value/array.rb', line 151

def do_unshift (opts)
	if params = opts[:parameters] then @sarah.insert! params.sarah end
	@template.factory :nil
end

#get_item(key) ⇒ Object



39
40
41
# File 'lib/ltdtemplate/value/array.rb', line 39

def get_item (key)
	@sarah.has_key?(key) ? @sarah[key] : @template.factory(:nil)
end

#get_value(opts = {}) ⇒ Object



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

def get_value (opts = {})
	case opts[:method]
	when nil, 'call' then self
	when 'class' then @template.factory :string, 'Array'
	when 'join' then do_join opts
	when 'pop', '->' then do_pop opts
	when 'push', '+>' then do_push opts
	when 'rnd_size' then @template.factory :number, @sarah.rnd_size
	when 'seq_size' then @template.factory :number, @sarah.seq_size
	when 'shift', '<-' then do_shift opts
	when 'size' then @template.factory :number, @sarah.size
	when 'type' then @template.factory :string, 'array'
	when 'unshift', '<+' then do_unshift opts
	else do_method opts, 'Array'
	end
end

#has_item?(key) ⇒ Boolean

Implement the subscripting interface. Note that the most recent value is always used. (1 .. 0, 2, 0, 3) is 3. Items set within (or at the end of) the positional range at the time will be positional. Otherwise, they will be named.

Keys must be Ruby-native values; values must be template code or values.

Returns:



38
# File 'lib/ltdtemplate/value/array.rb', line 38

def has_item? (key); @sarah.has_key? key; end

#namedObject



27
# File 'lib/ltdtemplate/value/array.rb', line 27

def named; @sarah.rnd; end

#positionalObject

Access positional (sequential) or named (random-access) parts of the array



26
# File 'lib/ltdtemplate/value/array.rb', line 26

def positional; @sarah.seq; end

#scalar?Boolean

Scalar assignment is used instead of array assignment if the parameter list contains exactly one positional parameter and the “..” operator was not used.

Returns:



73
# File 'lib/ltdtemplate/value/array.rb', line 73

def scalar?; @scalar and @sarah.seq_size == 1; end

#set_from_array(data) ⇒ Object

Set (recursively) from a native array.



98
99
100
101
102
# File 'lib/ltdtemplate/value/array.rb', line 98

def set_from_array (data)
	clear
	data.each_index { |i| set_item i, map_native_value(data[i]) }
	self
end

#set_from_hash(data) ⇒ Object

Set (recursively) from a native hash.



107
108
109
110
111
# File 'lib/ltdtemplate/value/array.rb', line 107

def set_from_hash (data)
	clear
	data.each { |key, val| set_item key, map_native_value(val) }
	self
end

#set_item(key, value) ⇒ Object



42
43
44
45
# File 'lib/ltdtemplate/value/array.rb', line 42

def set_item (key, value)
	@sarah[key] = value
	@template.using :array_size, @sarah.size
end

#set_value(positional, named = {}, scalar = false) ⇒ Object

Set positional and possibly named values. Keys must be ruby-native values; values must be template code or values.



88
89
90
91
92
93
# File 'lib/ltdtemplate/value/array.rb', line 88

def set_value (positional, named = {}, scalar = false)
	clear
	@sarah.merge! positional, named
	@scalar = scalar
	self
end

#to_booleanObject



47
# File 'lib/ltdtemplate/value/array.rb', line 47

def to_boolean; true; end

#to_nativeObject



48
# File 'lib/ltdtemplate/value/array.rb', line 48

def to_native; @sarah.seq.map { |val| val.to_native }; end

#to_textObject



49
# File 'lib/ltdtemplate/value/array.rb', line 49

def to_text; @sarah.seq.map { |val| val.to_text }.join ''; end