Class: C::NodeArray

Inherits:
NodeList show all
Defined in:
lib/cast/node_list.rb,
lib/cast/node_list.rb,
lib/cast/node_list.rb,
lib/cast/node_list.rb

Overview


                      Array methods

Constant Summary

Constants inherited from Node

C::Node::INSPECT_TAB

Instance Attribute Summary

Attributes inherited from Node

#parent, #pos, #subclasses

Instance Method Summary collapse

Methods inherited from NodeList

#==, [], #hash, #inspect, #match?, #size, #to_s

Methods inherited from Node

#==, #=~, abstract, add_field, #attached?, child, #depth_first, #detach, #detached?, #each, #eql?, field, #fields, fields, #hash, inherited, initializer, #insert_next, #insert_prev, #inspect, inspect1, #list_next, #list_prev, #match?, #method_missing, new_at, #next, #postorder, #preorder, #prev, #replace_with, #reverse_depth_first, #reverse_each, #reverse_postorder, #reverse_preorder, subclasses_recursive, #swap_with

Constructor Details

#initializeNodeArray



159
160
161
162
# File 'lib/cast/node_list.rb', line 159

def initialize
  super
  @array = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class C::Node

Instance Method Details

#<<(newnode) ⇒ Object



475
476
477
478
479
480
481
# File 'lib/cast/node_list.rb', line 475

def <<(newnode)
  newnode = add_prep([newnode])
  @array << newnode.first
  added_(newnode)
  adjust_indices_(@array.length - 1)
  return self
end

#[]=(*args) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/cast/node_list.rb', line 482

def []=(*args)
  newnodes = args.pop
  i, n, splat = parse_index(*args)
  oldnodes = @array[i, n] or
    raise IndexError, "index #{i} out of NodeList"
  if splat
    newnodes = add_prep(newnodes, oldnodes)
  else
    # newnodes is a single node (not an Array)
    newnodes = add_prep([newnodes], [oldnodes])
  end
  @array[i, n] = newnodes
  if splat
    removed_(*oldnodes)
    added_(*newnodes)
  else
    removed_(oldnodes)
    added_(newnodes)
  end
  adjust_indices_(i)
  return newnodes
end

#assert_invariants(testcase) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/cast/node_list.rb', line 151

def assert_invariants(testcase)
  super
  testcase.assert_same(::Array, @array)
  @array.each_with_index do |node, i|
    assert_same(i, node.instance_variable_get(:@parent_index))
  end
end

#clearObject



523
524
525
526
527
528
# File 'lib/cast/node_list.rb', line 523

def clear
  nodes = @array.dup
  @array.clear
  removed_(*nodes)
  return self
end

#cloneObject



171
172
173
174
175
176
# File 'lib/cast/node_list.rb', line 171

def clone
  ret = super
  ret.instance_variable_set(:@array, [])
  clones = self.map{|n| n.clone}
  return ret.push(*clones)
end

#concat(other) ⇒ Object



504
505
506
507
508
509
510
511
512
# File 'lib/cast/node_list.rb', line 504

def concat(other)
  other = other.to_a
  other = add_prep(other)
  len = @array.length
  @array.concat(other)
  added_(*other)
  adjust_indices_(len)
  return self
end

#delete_at(index) ⇒ Object



513
514
515
516
517
518
519
520
521
522
# File 'lib/cast/node_list.rb', line 513

def delete_at(index)
  if index < @array.length
    ret = @array.delete_at(index)
    removed_(ret)
    adjust_indices_(index)
    return ret
  else
    return nil
  end
end

#dupObject



164
165
166
167
168
169
# File 'lib/cast/node_list.rb', line 164

def dup
  ret = super
  ret.instance_variable_set(:@array, [])
  dupes = self.map{|n| n.dup}
  return ret.push(*dupes)
end

#insert(i, *newnodes) ⇒ Object



466
467
468
469
470
471
472
473
474
# File 'lib/cast/node_list.rb', line 466

def insert(i, *newnodes)
  (0..@array.length).include? i or
    raise IndexError, "index #{i} out of NodeList (length #{@array.length})"
  newnodes = add_prep(newnodes)
  @array.insert(i, *newnodes)
  added_(*newnodes)
  adjust_indices_(i)
  return self
end

#insert_after(node, *newnodes) ⇒ Object



266
267
268
269
270
271
272
# File 'lib/cast/node_list.rb', line 266

def insert_after(node, *newnodes)
  node.parent.equal? self or
    raise ArgumentError, "node is not a child"
  index = node.instance_variable_get(:@parent_index) + 1
  insert(index, *newnodes)
  return self
end

#insert_before(node, *newnodes) ⇒ Object



273
274
275
276
277
278
279
# File 'lib/cast/node_list.rb', line 273

def insert_before(node, *newnodes)
  node.parent.equal? self or
    raise ArgumentError, "node is not a child"
  index = node.instance_variable_get(:@parent_index)
  insert(index, *newnodes)
  return self
end

#node_after(node) ⇒ Object



238
239
240
241
242
# File 'lib/cast/node_list.rb', line 238

def node_after(node)
  node.parent.equal? self or
    raise ArgumentError, "node is not a child"
  @array[index = node.instance_variable_get(:@parent_index)+1]
end

#node_before(node) ⇒ Object



244
245
246
247
248
249
250
251
252
253
# File 'lib/cast/node_list.rb', line 244

def node_before(node)
  node.parent.equal? self or
    raise ArgumentError, "node is not a child"
  index = node.instance_variable_get(:@parent_index)
  if index.zero?
    return nil
  else
    return @array[index-1]
  end
end

#pop(*args) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/cast/node_list.rb', line 436

def pop(*args)
  if args.empty?
    ret = @array.pop
    removed_(ret)
    return ret
  else
    args.length == 1 or
      raise ArgumentError, "wrong number of arguments (#{args.length} for 0..1)"
    arg = args[0]
    arg = @array.length if @array.length < arg
    ret = @array.slice!(-arg, arg)
    removed_(*ret)
    return ret
  end
end

#push(*nodes) ⇒ Object



421
422
423
424
425
426
427
428
# File 'lib/cast/node_list.rb', line 421

def push(*nodes)
  nodes = add_prep(nodes)
  i = @array.length
  @array.push(*nodes)
  added_(*nodes)
  adjust_indices_(i)
  return self
end

#remove_node(node) ⇒ Object



255
256
257
258
259
260
261
262
263
264
# File 'lib/cast/node_list.rb', line 255

def remove_node(node)
  node.parent.equal? self or
    raise ArgumentError, "node is not a child"
  index = node.instance_variable_get(:@parent_index)
  index.instance_variable_set(:@parent_index, nil)
  removed_(@array[index])
  @array.delete_at(index)
  adjust_indices_(index)
  return self
end

#replace(other) ⇒ Object



529
530
531
532
533
534
535
536
537
538
# File 'lib/cast/node_list.rb', line 529

def replace(other)
  other = other.to_a
  other = add_prep(other)
  oldnodes = @array.dup
  @array.replace(other)
  removed_(*oldnodes)
  added_(*@array)
  adjust_indices_(0)
  return self
end

#replace_node(oldnode, *newnodes) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/cast/node_list.rb', line 280

def replace_node(oldnode, *newnodes)
  oldnode.parent.equal? self or
    raise ArgumentError, "node is not a child"
  index = oldnode.instance_variable_get(:@parent_index)
  self[index, 1] = newnodes
  return self
end

#shift(*args) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/cast/node_list.rb', line 451

def shift(*args)
  if args.empty?
    ret = @array.shift
    removed_ ret
  else
    args.length == 1 or
      raise ArgumentError, "wrong number of arguments (#{args.length} for 0..1)"
    arg = args[0]
    arg = @array.length if @array.length < arg
    ret = @array.slice!(0, arg)
    removed_(*ret)
  end
  adjust_indices_(0)
  return ret
end

#to_aObject



418
419
420
# File 'lib/cast/node_list.rb', line 418

def to_a
  @array.dup
end

#unshift(*nodes) ⇒ Object



429
430
431
432
433
434
435
# File 'lib/cast/node_list.rb', line 429

def unshift(*nodes)
  nodes = add_prep(nodes)
  @array.unshift(*nodes)
  added_(*nodes)
  adjust_indices_(0)
  return self
end