lib/csl/treelike.rb in csl-1.0.2 vs lib/csl/treelike.rb in csl-1.1.0
- old
+ new
@@ -30,11 +30,11 @@
nodes.each do |node|
delete_child node
end
end
alias delete delete_children
-
+
# Deletes child nodes that are equal to the passed-in node. Returns all
# deleted children. If no children were deleted, returns nil. If the
# optional block is given, returns the result block if no children were
# deleted.
def delete_child(child)
@@ -112,10 +112,21 @@
child.match?(name, conditions)
end
end
alias >> find_children
+ def closest(name, conditions = {})
+ case
+ when root?
+ nil
+ when parent.match?(name, conditions)
+ parent
+ else
+ parent.closest(name, conditions)
+ end
+ end
+
# @return [Boolean] true if this node has child nodes; false otherwise.
def has_children?
!empty?
end
@@ -285,11 +296,11 @@
# used internally by the Node to manage its children. The Struct
# will be automatically initialized and is used similarly to the
# standard Array that normally holds the child nodes. The benefit of
# using the Struct is that all child nodes are accessible by name and
# need not be looked up; this improves performance, however, note that
- # a node defining it's children that way can only contain nodes of the
+ # a node defining its children that way can only contain nodes of the
# given types.
#
# This method also generates accessors for each child. The writer
# method will try to coerce the passed-in value into the correct
# node type automatically.
@@ -322,11 +333,11 @@
rescue => e
raise ArgumentError, "failed to convert #{value.inspect} to node: #{e.message}"
end unless value.respond_to?(:nodename)
- children << value
+ add_child value
value
end
alias_method :"#{reader}=", writer unless method_defined?(:"#{reader}=")
end
@@ -435,11 +446,11 @@
end
private
def resolve(nodename)
- keys.include?(nodename.to_sym) && send(nodename)
+ keys.include?(nodename.to_sym) && self[nodename]
end
})
end
def alias_child(new_name, old_name)
@@ -472,6 +483,6 @@
end
end
end
-end
\ No newline at end of file
+end