Class: Trivet::Childset

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/trivet.rb

Overview

Objects of this class act like an array of the children of a node. However, unlike an array, attempts to add children result in calling Trivet::Node#allow_child? to check if the child is allowed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Childset

Accepts a Trivet::Node object to which this object will be attached.



1093
1094
1095
1096
# File 'lib/trivet.rb', line 1093

def initialize(node)
	@node = node
	@children = []
end

Instance Attribute Details

#nodeObject (readonly)

Returns the Trivet::Node object that this object is attached to.



1107
1108
1109
# File 'lib/trivet.rb', line 1107

def node
  @node
end

Instance Method Details

#<<(new_child) ⇒ Object

Shortcut for append without any options.



1170
1171
1172
# File 'lib/trivet.rb', line 1170

def <<(new_child)
	return append(new_child)
end

#append(new_child, opts = {}) ⇒ Object

Adds a child to the end of the array. Calls Trivet::Node#allow_child? to check if the child can be added. Does nothing if the node is already a child.



1165
1166
1167
# File 'lib/trivet.rb', line 1165

def append(new_child, opts={})
	return add_child(new_child, 'last', opts)
end

#clearObject

Removes all children.



1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/trivet.rb', line 1207

def clear()
	# explicitly unlink childen
	@children.clone.each do |child|
		if child.is_a?(Trivet::Node)
			child.unlink
		end
	end
	
	# remove all other child objects
	@children.clear
end

#have_child?(potential) ⇒ Boolean


have_child?

Returns:

  • (Boolean)


1312
1313
1314
1315
1316
1317
1318
1319
1320
# File 'lib/trivet.rb', line 1312

def have_child?(potential)
	@children.each_with_index do |child, idx|
		if child.equal?(potential)
			return idx
		end
	end
	
	return false
end

#insert(index, new_child, opts = {}) ⇒ Object

Inserts the child at the position indicated by index. Calls Trivet::Node#allow_child? to check if the child can be added. Does nothing if the node is already a child.



1184
1185
1186
# File 'lib/trivet.rb', line 1184

def insert(index, new_child, opts={})
	return add_child(new_child, index, opts)
end

#popObject

Unlinks and returns the last child. Returns nil if there are no children.



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
# File 'lib/trivet.rb', line 1250

def pop()
	# remove first element
	removed = @children.pop
	
	# if a node was removed, unlink it
	if removed.is_a?(Trivet::Node)
		removed.unlink 'recurse'=>false
	end
	
	# return
	return removed
end

#push(new_child, opts = {}) ⇒ Object

Adds a child to the end of the array. Calls Trivet::Node#allow_child? to check if the child can be added. Does nothing if the node is already a child.



1158
1159
1160
# File 'lib/trivet.rb', line 1158

def push(new_child, opts={})
	return add_child(new_child, 'last', opts)
end

#reject!Object

Acts like Array#reject!. Rejected children are unlinked.



1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
# File 'lib/trivet.rb', line 1272

def reject!()
	# If no block, just return an enumerator. This mimics the behavior of
	# Array#reject!.
	if not block_given?
		return enum_for(:each)
	end
	
	# $tm.hrm
	all = @children.clone
	
	# loop through all
	all.each do |child|
		bool = yield(child)
		bool or remove_child(child)
	end
	
	# return
	return self
end

#remove_child(child, opts = {}) ⇒ Object

Removes the given child from the array of children. Does nothing if the child isn’t present.



1198
1199
1200
1201
1202
1203
1204
# File 'lib/trivet.rb', line 1198

def remove_child(child, opts={})
	# opts = {'recurse'=>true}.merge(opts)
	
	@children.reject!() do |el|
		el.equal? child
	end
end

#shiftObject

Unlinks and returns the first child. Returns nil if there are no children.



1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
# File 'lib/trivet.rb', line 1228

def shift()
	# remove first element
	removed = @children.shift
	
	# if a node was removed, unlink it
	if removed.is_a?(Trivet::Node)
		removed.unlink 'recurse'=>false
	end
	
	# return
	return removed
end

#to_aObject

Returns an array of the children.



1301
1302
1303
# File 'lib/trivet.rb', line 1301

def to_a
	return @children.clone
end

#unshift(new_child, opts = {}) ⇒ Object

Adds a child to the beginning of the array. Calls Trivet::Node#allow_child? to check if the child can be added. Does nothing if the node is already a child.



1177
1178
1179
# File 'lib/trivet.rb', line 1177

def unshift(new_child, opts={})
	return add_child(new_child, 'first', opts)
end