Class: LtdTemplate::Value::Array
- Defined in:
- lib/ltdtemplate/value/array.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#sarah ⇒ Object
readonly
Returns the value of attribute sarah.
Attributes inherited from Code
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all current positional and named values.
-
#do_each(opts) ⇒ Object
Loop over each key, value.
-
#do_join(opts) ⇒ Object
Combine array element values into a string.
- #do_pop(opts) ⇒ Object
- #do_push(opts) ⇒ Object
- #do_shift(opts) ⇒ Object
- #do_unshift(opts) ⇒ Object
- #get_item(key) ⇒ Object
- #get_value(opts = {}) ⇒ Object
-
#has_item?(key) ⇒ Boolean
Implement the subscripting interface.
-
#initialize(template) ⇒ Array
constructor
A new instance of Array.
- #named ⇒ Object
-
#positional ⇒ Object
Access positional (sequential) or named (random-access) parts of the array.
-
#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.
-
#set_from_array(data) ⇒ Object
Set (recursively) from a native array.
-
#set_from_hash(data) ⇒ Object
Set (recursively) from a native hash.
- #set_item(key, value) ⇒ Object
-
#set_value(positional, named = {}, scalar = false) ⇒ Object
Set positional and possibly named values.
- #to_boolean ⇒ Object
- #to_native ⇒ Object
- #to_text ⇒ Object
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
#sarah ⇒ Object (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
#clear ⇒ Object
Clear all current positional and named values
88 89 90 91 92 |
# File 'lib/ltdtemplate/value/array.rb', line 88 def clear @sarah.clear @scalar = false self end |
#do_each(opts) ⇒ Object
Loop over each key, value
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/ltdtemplate/value/array.rb', line 126 def do_each (opts) results = @template.factory :array if params = opts[:parameters] and params.positional.size > 0 body = params.positional[0] if opts[:method] != 'each_rnd' @sarah.seq.each_index do |idx| @template.use :iterations body_params = @template.factory :parameters, [@template.factory(:number, idx), @sarah.seq[idx]] results.sarah.push body.get_value(:method => 'each_seq', :parameters => body_params) end end if opts[:method] != 'each_seq' @sarah.rnd.each do |key, val| @template.use :iterations body_params = @template.factory :parameters, [map_native_value(key), val] results.sarah.push body.get_value(:method => 'each_rnd', :parameters => body_params) end end end results end |
#do_join(opts) ⇒ Object
Combine array element values into a string
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ltdtemplate/value/array.rb', line 155 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
177 178 179 |
# File 'lib/ltdtemplate/value/array.rb', line 177 def do_pop (opts) @sarah.pop || @template.factory(:nil) end |
#do_push(opts) ⇒ Object
181 182 183 184 |
# File 'lib/ltdtemplate/value/array.rb', line 181 def do_push (opts) if params = opts[:parameters] then @sarah.append! params.sarah end @template.factory :nil end |
#do_shift(opts) ⇒ Object
186 187 188 |
# File 'lib/ltdtemplate/value/array.rb', line 186 def do_shift (opts) @sarah.shift || @template.factory(:nil) end |
#do_unshift(opts) ⇒ Object
190 191 192 193 |
# File 'lib/ltdtemplate/value/array.rb', line 190 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ltdtemplate/value/array.rb', line 58 def get_value (opts = {}) case opts[:method] when nil, 'call' then self when 'class' then @template.factory :string, 'Array' when 'each', 'each_rnd', 'each_seq' then do_each opts 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 when '/' then @template.factory :parameters, @sarah.seq, @sarah.rnd when '%' then @template.factory :parameters, [], @sarah.seq 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.
38 |
# File 'lib/ltdtemplate/value/array.rb', line 38 def has_item? (key); @sarah.has_key? key; end |
#named ⇒ Object
27 |
# File 'lib/ltdtemplate/value/array.rb', line 27 def named; @sarah.rnd; end |
#positional ⇒ Object
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.
83 |
# File 'lib/ltdtemplate/value/array.rb', line 83 def scalar?; @scalar and @sarah.seq_size == 1; end |
#set_from_array(data) ⇒ Object
Set (recursively) from a native array.
108 109 110 111 112 |
# File 'lib/ltdtemplate/value/array.rb', line 108 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.
117 118 119 120 121 |
# File 'lib/ltdtemplate/value/array.rb', line 117 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.
98 99 100 101 102 103 |
# File 'lib/ltdtemplate/value/array.rb', line 98 def set_value (positional, named = {}, scalar = false) clear @sarah.merge! positional, named @scalar = scalar self end |
#to_boolean ⇒ Object
47 |
# File 'lib/ltdtemplate/value/array.rb', line 47 def to_boolean; true; end |
#to_native ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/ltdtemplate/value/array.rb', line 48 def to_native if @sarah.rnd_size == 0 then native = [] elsif @sarah.seq_size == 0 then native = {} else native = Sarah.new end @sarah.each { |key, val| native[key] = val.to_native } native end |
#to_text ⇒ Object
56 |
# File 'lib/ltdtemplate/value/array.rb', line 56 def to_text; @sarah.seq.map { |val| val.to_text }.join ''; end |