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.
-
#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_child(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.
965 966 967 968 |
# File 'lib/trivet.rb', line 965 def initialize(node) @node = node @children = [] end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the Trivet::Node object that this object is attached to.
979 980 981 |
# File 'lib/trivet.rb', line 979 def node @node end |
Instance Method Details
#<<(new_child) ⇒ Object
Shortcut for append without any options.
1025 1026 1027 |
# File 'lib/trivet.rb', line 1025 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.
1020 1021 1022 |
# File 'lib/trivet.rb', line 1020 def append(new_child, opts={}) return add_child new_child, 'last', opts end |
#clear ⇒ Object
Removes all children.
1062 1063 1064 1065 1066 |
# File 'lib/trivet.rb', line 1062 def clear() @children.clone.each do |child| child.unlink end 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.
1039 1040 1041 |
# File 'lib/trivet.rb', line 1039 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.
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'lib/trivet.rb', line 1099 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.
1013 1014 1015 |
# File 'lib/trivet.rb', line 1013 def push(new_child, opts={}) return add_child new_child, 'last', opts end |
#reject! ⇒ Object
Acts like Array#reject!. Rejected children are unlinked.
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/trivet.rb', line 1121 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.
1053 1054 1055 1056 1057 1058 1059 |
# File 'lib/trivet.rb', line 1053 def remove_child(child, opts={}) opts = {'recurse'=>true}.merge(opts) @children.reject!() do |el| el == child end end |
#shift ⇒ Object
Unlinks and returns the first child. Returns nil if there are no children.
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 |
# File 'lib/trivet.rb', line 1077 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.
1150 1151 1152 |
# File 'lib/trivet.rb', line 1150 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.
1032 1033 1034 |
# File 'lib/trivet.rb', line 1032 def unshift(new_child, opts={}) return add_child new_child, 'first', opts end |