Class: Trivet::Childset
- Inherits:
-
Object
- Object
- Trivet::Childset
- 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
-
#node ⇒ Object
readonly
Returns the Trivet::Node object that this object is attached to.
Instance Method Summary collapse
-
#<<(new_child) ⇒ Object
Shortcut for append without any options.
-
#append(new_child, opts = {}) ⇒ Object
Adds a child to the end of the array.
-
#clear ⇒ Object
Removes all children.
-
#have_object?(potential) ⇒ Boolean
Returns true if the @children contains the given object.
-
#initialize(node) ⇒ Childset
constructor
Accepts a Trivet::Node object to which this object will be attached.
-
#insert(index, new_child, opts = {}) ⇒ Object
Inserts the child at the position indicated by index.
-
#pop ⇒ Object
Unlinks and returns the last child.
-
#push(new_child, opts = {}) ⇒ Object
Adds a child to the end of the array.
-
#reject! ⇒ Object
Acts like Array#reject!.
-
#remove_object(child, opts = {}) ⇒ Object
Removes the given child from the array of children.
-
#shift ⇒ Object
Unlinks and returns the first child.
-
#to_a ⇒ Object
Returns an array of the children.
-
#unshift(new_child, opts = {}) ⇒ Object
Adds a child to the beginning of the array.
Constructor Details
#initialize(node) ⇒ Childset
Accepts a Trivet::Node object to which this object will be attached.
1109 1110 1111 1112 |
# File 'lib/trivet.rb', line 1109 def initialize(node) @node = node @children = [] end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the Trivet::Node object that this object is attached to.
1123 1124 1125 |
# File 'lib/trivet.rb', line 1123 def node @node end |
Instance Method Details
#<<(new_child) ⇒ Object
Shortcut for append without any options.
1186 1187 1188 |
# File 'lib/trivet.rb', line 1186 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.
1181 1182 1183 |
# File 'lib/trivet.rb', line 1181 def append(new_child, opts={}) return add_child(new_child, 'last', opts) end |
#clear ⇒ Object
Removes all children.
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 |
# File 'lib/trivet.rb', line 1223 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_object?(potential) ⇒ Boolean
Returns true if the @children contains the given object. This method is not quite the same as include?. This method returns true only if @children contains the actual object given, not just an object that matches ==. So, for example, the following use of have_object? would return false, even though it cohtains a string identical to the string in @children.
@children.push 'whatever'
@children.include? 'whatever' # true
@children.have_object? 'whatever' # false
1339 1340 1341 1342 1343 1344 1345 1346 1347 |
# File 'lib/trivet.rb', line 1339 def have_object?(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.
1200 1201 1202 |
# File 'lib/trivet.rb', line 1200 def insert(index, new_child, opts={}) return add_child(new_child, index, opts) end |
#pop ⇒ Object
Unlinks and returns the last child. Returns nil if there are no children.
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 |
# File 'lib/trivet.rb', line 1266 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.
1174 1175 1176 |
# File 'lib/trivet.rb', line 1174 def push(new_child, opts={}) return add_child(new_child, 'last', opts) end |
#reject! ⇒ Object
Acts like Array#reject!. Rejected children are unlinked.
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 |
# File 'lib/trivet.rb', line 1288 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_object(child) end # return return self end |
#remove_object(child, opts = {}) ⇒ Object
Removes the given child from the array of children. Does nothing if the child isn’t present.
1214 1215 1216 1217 1218 1219 1220 |
# File 'lib/trivet.rb', line 1214 def remove_object(child, opts={}) # opts = {'recurse'=>true}.merge(opts) @children.reject!() do |el| el.equal? child end end |
#shift ⇒ Object
Unlinks and returns the first child. Returns nil if there are no children.
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 |
# File 'lib/trivet.rb', line 1244 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_a ⇒ Object
Returns an array of the children.
1317 1318 1319 |
# File 'lib/trivet.rb', line 1317 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.
1193 1194 1195 |
# File 'lib/trivet.rb', line 1193 def unshift(new_child, opts={}) return add_child(new_child, 'first', opts) end |