Class: Kitchen::Ancestor

Inherits:
Object show all
Defined in:
lib/kitchen/ancestor.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Ancestor

Create a new Ancestor

Parameters:



23
24
25
26
27
# File 'lib/kitchen/ancestor.rb', line 23

def initialize(element)
  @element = element
  @type = element.short_type
  @descendant_counts = {}
end

Instance Attribute Details

#elementElementBase

The ancestor element

Returns:



17
18
19
# File 'lib/kitchen/ancestor.rb', line 17

def element
  @element
end

#typeSymbol (readonly)

The type, e.g. :page, :term

Returns:

  • (Symbol)

    the type



12
13
14
# File 'lib/kitchen/ancestor.rb', line 12

def type
  @type
end

Instance Method Details

#cloneObject

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



61
62
63
64
# 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) ⇒ Object

Decreases the descendant count for the given type by some amount

Parameters:

  • descendant_type (String, Symbol)

    the descendent’s type

  • by (Integer) (defaults to: 1)

    the amount by which to decrement

Raises:

  • (RangeError)

    if descendant count is a negative number



43
44
45
46
47
48
# 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) ⇒ Integer

Returns the descendant count for the given type

Parameters:

  • descendant_type (String, Symbol)

    the descendent’s type

Returns:



55
56
57
# 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) ⇒ Object

Adds 1 to the descendant count for the given type

Parameters:

  • descendant_type (String, Symbol)

    the descendent’s type



33
34
35
# 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