lib/csl/treelike.rb in csl-1.1.0 vs lib/csl/treelike.rb in csl-1.2.0

- old
+ new

@@ -352,35 +352,36 @@ attr_reader :keys end def initialize(attrs = {}) super(*attrs.symbolize_keys.values_at(*keys)) + @order = [] end + def index(node, &block) + @order.index(node, &block) + end + # @return [<Symbol>] a list of symbols representing the names/keys # of the attribute variables. def keys __class__.keys end - alias original_each each def count - values.reject { |c| c.nil? || c.empty? }.length + values.reject { |c| c.nil? || c.is_a?(Array) && c.empty? }.length end + alias original_each each + # Iterates through all children. Nil values are skipped and Arrays # expanded. def each if block_given? - original_each do |node| - if node.kind_of?(Array) - node.select { |n| !n.nil? }.each(&Proc.new) - else - yield node unless node.nil? - end - end + order.each(&Proc.new) + self else to_enum end end @@ -405,10 +406,13 @@ self[node.nodename] = node else self[node.nodename] = [current, node] end + # Add to @order to keep track of node ordering + @order << node + self end alias << push @@ -428,10 +432,13 @@ else deleted = nil end end + # Delete node from ordered list as well + @order.delete(node) + if deleted.nil? && block_given? yield else deleted end @@ -442,9 +449,13 @@ resolve(name) || yield(key) else resolve(name) || default end end + + protected + + attr_reader :order private def resolve(nodename) keys.include?(nodename.to_sym) && self[nodename]