Sha256: c646dc79d83a0dc01668a8a73049e2b3c86fe62c1079e884768cb1e4c7542ebb

Contents?: true

Size: 1.94 KB

Versions: 30

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Kitchen
  # 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
  #
  class Ancestor

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

    # The ancestor element
    # @return [ElementBase] the ancestor element
    #
    attr_accessor :element

    # Create a new Ancestor
    #
    # @param element [ElementBase] the ancestor element
    #
    def initialize(element)
      @element = element
      @type = element.short_type
      @descendant_counts = {}
    end

    # Adds 1 to the descendant count for the given type
    #
    # @param descendant_type [String, Symbol] the descendent's type
    #
    def increment_descendant_count(descendant_type)
      @descendant_counts[descendant_type.to_sym] = get_descendant_count(descendant_type) + 1
    end

    # 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
    #
    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

    # Returns the descendant count for the given type
    #
    # @param descendant_type [String, Symbol] the descendent's type
    # @return [Integer] the count
    #
    def get_descendant_count(descendant_type)
      @descendant_counts[descendant_type.to_sym] || 0
    end

    # Makes a new Ancestor around the same element, with new counts
    #
    def clone
      # @todo Delete later if not used
      Ancestor.new(element)
    end

  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
openstax_kitchen-19.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-18.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-17.1.0 lib/kitchen/ancestor.rb
openstax_kitchen-17.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-16.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-15.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-14.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-13.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-12.2.0 lib/kitchen/ancestor.rb
openstax_kitchen-12.1.0 lib/kitchen/ancestor.rb
openstax_kitchen-12.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-11.2.0 lib/kitchen/ancestor.rb
openstax_kitchen-11.1.0 lib/kitchen/ancestor.rb
openstax_kitchen-11.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-10.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-9.2.0 lib/kitchen/ancestor.rb
openstax_kitchen-9.1.0 lib/kitchen/ancestor.rb
openstax_kitchen-9.0.0 lib/kitchen/ancestor.rb
openstax_kitchen-8.0.1 lib/kitchen/ancestor.rb
openstax_kitchen-8.0.0 lib/kitchen/ancestor.rb