Class: Kitchen::Ancestor
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
-
#element ⇒ ElementBase
The ancestor element.
-
#type ⇒ Symbol
readonly
The type, e.g.
Instance Method Summary collapse
-
#clone ⇒ Object
Makes a new Ancestor around the same element, with new counts.
-
#decrement_descendant_count(descendant_type, by: 1) ⇒ Object
Decreases the descendant count for the given type by some amount.
-
#get_descendant_count(descendant_type) ⇒ Integer
Returns the descendant count for the given type.
-
#increment_descendant_count(descendant_type) ⇒ Object
Adds 1 to the descendant count for the given type.
-
#initialize(element) ⇒ Ancestor
constructor
Create a new Ancestor.
Constructor Details
#initialize(element) ⇒ Ancestor
Create a new Ancestor
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
#element ⇒ ElementBase
The ancestor element
17 18 19 |
# File 'lib/kitchen/ancestor.rb', line 17 def element @element end |
#type ⇒ Symbol (readonly)
The type, e.g. :page
, :term
12 13 14 |
# File 'lib/kitchen/ancestor.rb', line 12 def type @type end |
Instance Method Details
#clone ⇒ Object
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
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
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
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 |