class Kitchen::Ancestor

A wrapper for an element representing an ancestor (up the DOM tree) of another element; keeps track of the number of descendants it has of a particular type

Attributes

element[RW]

The ancestor element @return [ElementBase] the ancestor element

type[R]

The type, e.g. :page, :term @return [Symbol] the type

Public Class Methods

new(element) click to toggle source

Create a new Ancestor

@param element [ElementBase] the ancestor element

# File lib/kitchen/ancestor.rb, line 23
def initialize(element)
  @element = element
  @type = element.short_type
  @descendant_counts = {}
end

Public Instance Methods

clone() click to toggle source

Makes a new Ancestor around the same element, with new counts

# File lib/kitchen/ancestor.rb, line 61
def clone
  # @todo Delete later if not used
  Ancestor.new(element)
end
decrement_descendant_count(descendant_type, by: 1) click to toggle source

Decreases the descendant count for the given type by some amount

@param descendant_type [String, Symbol] the descendent's type @param by [Integer] the amount by which to decrement @raise [RangeError] if descendant count is a negative number

# File lib/kitchen/ancestor.rb, line 43
def decrement_descendant_count(descendant_type, by: 1)
  raise(RangeError, 'An element cannot have negative descendants') \
    if (get_descendant_count(descendant_type) - by).negative?

  @descendant_counts[descendant_type.to_sym] = get_descendant_count(descendant_type) - by
end
get_descendant_count(descendant_type) click to toggle source

Returns the descendant count for the given type

@param descendant_type [String, Symbol] the descendent's type @return [Integer] the count

# File lib/kitchen/ancestor.rb, line 55
def get_descendant_count(descendant_type)
  @descendant_counts[descendant_type.to_sym] || 0
end
increment_descendant_count(descendant_type) click to toggle source

Adds 1 to the descendant count for the given type

@param descendant_type [String, Symbol] the descendent's type

# File lib/kitchen/ancestor.rb, line 33
def increment_descendant_count(descendant_type)
  @descendant_counts[descendant_type.to_sym] = get_descendant_count(descendant_type) + 1
end