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_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
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.
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.
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_boolean ⇒ Object
47 |
# File 'lib/ltdtemplate/value/array.rb', line 47 def to_boolean; true; end |
#to_native ⇒ Object
48 |
# File 'lib/ltdtemplate/value/array.rb', line 48 def to_native; @sarah.seq.map { |val| val.to_native }; end |
#to_text ⇒ Object
49 |
# File 'lib/ltdtemplate/value/array.rb', line 49 def to_text; @sarah.seq.map { |val| val.to_text }.join ''; end |