Class: Kitchen::ElementEnumeratorBase
- Includes:
- Mixins::BlockErrorIf
- Defined in:
- lib/kitchen/element_enumerator_base.rb
Overview
Base class for all element enumerators
Direct Known Subclasses
BookElementEnumerator, ChapterElementEnumerator, CompositeChapterElementEnumerator, CompositePageElementEnumerator, ElementEnumerator, ExampleElementEnumerator, ExerciseElementEnumerator, FigureElementEnumerator, InjectedExerciseElementEnumerator, InjectedQuestionElementEnumerator, MetadataElementEnumerator, NoteElementEnumerator, PageElementEnumerator, ReferenceElementEnumerator, SectionElementEnumerator, SolutionElementEnumerator, TableElementEnumerator, TermElementEnumerator, TypeCastingElementEnumerator, UnitElementEnumerator
Instance Attribute Summary collapse
-
#search_query ⇒ String
readonly
Return the selectors or other search strings that this enumerator uses to search through the document.
Instance Method Summary collapse
-
#[](index) ⇒ Element
Returns the element at the provided index.
-
#chain_to(enumerator_class, default_css_or_xpath: nil, css_or_xpath: nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through elements within the scope of this enumerator.
-
#chapters(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through chapters within the scope of this enumerator.
-
#composite_chapters(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through composite chapters within the scope of this enumerator.
-
#composite_pages(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through composite pages within the scope of this enumerator.
-
#copy(to: nil) ⇒ Clipboard
Makes a copy of the enumerated elements and places them on the specified clipboard.
-
#cut(to: nil) ⇒ Clipboard
Removes enumerated elements from their parent and places them on the specified clipboard.
-
#examples(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through examples within the scope of this enumerator.
-
#exercises(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through exercises within the scope of this enumerator.
-
#figures(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through figures within the scope of this enumerator.
-
#first!(missing_message: 'Could not return a first result') ⇒ Element
Returns the first element in this enumerator.
-
#initialize(size = nil, search_query: nil, upstream_enumerator: nil) ⇒ ElementEnumeratorBase
constructor
Creates a new instance.
-
#injected_exercises(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through injected exercises within the scope of this enumerator.
-
#injected_questions(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through injected questions within the scope of this enumerator.
-
#metadatas(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through metadata within the scope of this enumerator.
-
#non_introduction_pages(only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through pages that arent the introduction page within the scope of this enumerator.
-
#notes(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through notes within the scope of this enumerator.
-
#pages(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through pages within the scope of this enumerator.
-
#references(css_or_xpath = nil) ⇒ Object
Returns an enumerator that iterates through references within the scope of this enumerator.
-
#search(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates within the scope of this enumerator.
-
#search_history ⇒ SearchHistory
Return the search history based on this enumerator and any upstream enumerators.
-
#search_with(*enumerator_classes) ⇒ TypeCastingElementEnumerator
Searches for elements handled by a list of enumerator classes.
-
#sections(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through sections within the scope of this enumerator.
-
#solutions(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through solutions within the scope of this enumerator.
-
#tables(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through tables within the scope of this enumerator.
-
#terms(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through terms within the scope of this enumerator.
-
#titles(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through titles within the scope of this enumerator.
-
#to_s ⇒ String
Returns a concatenation of
to_s
for all elements in the enumerator. -
#trash ⇒ Object
Removes all matching elements from the document.
-
#units(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through units within the scope of this enumerator.
Methods included from Mixins::BlockErrorIf
Constructor Details
#initialize(size = nil, search_query: nil, upstream_enumerator: nil) ⇒ ElementEnumeratorBase
Creates a new instance
25 26 27 28 29 |
# File 'lib/kitchen/element_enumerator_base.rb', line 25 def initialize(size=nil, search_query: nil, upstream_enumerator: nil) @search_query = search_query @upstream_enumerator = upstream_enumerator super(size) end |
Instance Attribute Details
#search_query ⇒ String (readonly)
Return the selectors or other search strings that this enumerator uses to search through the document
14 15 16 |
# File 'lib/kitchen/element_enumerator_base.rb', line 14 def search_query @search_query end |
Instance Method Details
#[](index) ⇒ Element
Returns the element at the provided index
468 469 470 |
# File 'lib/kitchen/element_enumerator_base.rb', line 468 def [](index) to_a[index] end |
#chain_to(enumerator_class, default_css_or_xpath: nil, css_or_xpath: nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through elements within the scope of this enumerator
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/kitchen/element_enumerator_base.rb', line 399 def chain_to(enumerator_class, default_css_or_xpath: nil, css_or_xpath: nil, only: nil, except: nil) block_error_if(block_given?) search_query = SearchQuery.new( css_or_xpath: css_or_xpath, only: only, except: except ) if default_css_or_xpath search_query.apply_default_css_or_xpath_and_normalize(default_css_or_xpath) end enumerator_class.factory.build_within(self, search_query: search_query) end |
#chapters(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through chapters within the scope of this enumerator
139 140 141 142 |
# File 'lib/kitchen/element_enumerator_base.rb', line 139 def chapters(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(ChapterElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#composite_chapters(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through composite chapters within the scope of this enumerator
105 106 107 108 109 110 111 |
# File 'lib/kitchen/element_enumerator_base.rb', line 105 def composite_chapters(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(CompositeChapterElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#composite_pages(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through composite pages within the scope of this enumerator
85 86 87 88 89 90 91 |
# File 'lib/kitchen/element_enumerator_base.rb', line 85 def composite_pages(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(CompositePageElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#copy(to: nil) ⇒ Clipboard
Makes a copy of the enumerated elements and places them on the specified clipboard.
449 450 451 452 453 454 455 |
# File 'lib/kitchen/element_enumerator_base.rb', line 449 def copy(to: nil) to ||= Clipboard.new each do |element| element.copy(to: to) end to end |
#cut(to: nil) ⇒ Clipboard
Removes enumerated elements from their parent and places them on the specified clipboard
434 435 436 437 438 439 440 |
# File 'lib/kitchen/element_enumerator_base.rb', line 434 def cut(to: nil) to ||= Clipboard.new each do |element| element.cut(to: to) end to end |
#examples(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through examples within the scope of this enumerator
218 219 220 221 |
# File 'lib/kitchen/element_enumerator_base.rb', line 218 def examples(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(ExampleElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#exercises(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through exercises within the scope of this enumerator
264 265 266 267 |
# File 'lib/kitchen/element_enumerator_base.rb', line 264 def exercises(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(ExerciseElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#figures(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through figures within the scope of this enumerator
167 168 169 170 |
# File 'lib/kitchen/element_enumerator_base.rb', line 167 def figures(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(FigureElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#first!(missing_message: 'Could not return a first result') ⇒ Element
Returns the first element in this enumerator
422 423 424 425 |
# File 'lib/kitchen/element_enumerator_base.rb', line 422 def first!(missing_message: 'Could not return a first result') first || raise(RecipeError, "#{} matching #{search_history.latest} " \ "inside [#{search_history.upstream}]") end |
#injected_exercises(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through injected exercises within the scope of this enumerator
351 352 353 354 355 |
# File 'lib/kitchen/element_enumerator_base.rb', line 351 def injected_exercises(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(InjectedExerciseElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#injected_questions(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through injected questions within the scope of this enumerator
315 316 317 318 319 |
# File 'lib/kitchen/element_enumerator_base.rb', line 315 def injected_questions(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(InjectedQuestionElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#metadatas(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through metadata within the scope of this enumerator
281 282 283 284 |
# File 'lib/kitchen/element_enumerator_base.rb', line 281 def (css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(MetadataElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#non_introduction_pages(only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through pages that arent the introduction page within the scope of this enumerator
119 120 121 122 123 124 125 |
# File 'lib/kitchen/element_enumerator_base.rb', line 119 def non_introduction_pages(only: nil, except: nil) block_error_if(block_given?) chain_to(PageElementEnumerator, css_or_xpath: '$:not(.introduction)', only: only, except: except) end |
#notes(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through notes within the scope of this enumerator
184 185 186 187 |
# File 'lib/kitchen/element_enumerator_base.rb', line 184 def notes(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(NoteElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#pages(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through pages within the scope of this enumerator
68 69 70 71 |
# File 'lib/kitchen/element_enumerator_base.rb', line 68 def pages(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(PageElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#references(css_or_xpath = nil) ⇒ Object
Returns an enumerator that iterates through references within the scope of this enumerator
250 251 252 253 |
# File 'lib/kitchen/element_enumerator_base.rb', line 250 def references(css_or_xpath=nil) block_error_if(block_given?) chain_to(ReferenceElementEnumerator, css_or_xpath: css_or_xpath) end |
#search(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates within the scope of this enumerator
361 362 363 364 |
# File 'lib/kitchen/element_enumerator_base.rb', line 361 def search(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(ElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#search_history ⇒ SearchHistory
Return the search history based on this enumerator and any upstream enumerators
35 36 37 |
# File 'lib/kitchen/element_enumerator_base.rb', line 35 def search_history (@upstream_enumerator&.search_history || SearchHistory.empty).add(@search_query) end |
#search_with(*enumerator_classes) ⇒ TypeCastingElementEnumerator
Searches for elements handled by a list of enumerator classes. All element that matches one of those enumerator classes are iterated over.
372 373 374 375 376 377 378 379 380 381 |
# File 'lib/kitchen/element_enumerator_base.rb', line 372 def search_with(*enumerator_classes) block_error_if(block_given?) raise 'must supply at least one enumerator class' if enumerator_classes.empty? factory = enumerator_classes[0].factory enumerator_classes[1..-1].each do |enumerator_class| factory = factory.or_with(enumerator_class.factory) end factory.build_within(self) end |
#sections(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through sections within the scope of this enumerator
333 334 335 336 337 |
# File 'lib/kitchen/element_enumerator_base.rb', line 333 def sections(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(SectionElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#solutions(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through solutions within the scope of this enumerator
298 299 300 301 |
# File 'lib/kitchen/element_enumerator_base.rb', line 298 def solutions(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(SolutionElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#tables(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through tables within the scope of this enumerator
201 202 203 204 |
# File 'lib/kitchen/element_enumerator_base.rb', line 201 def tables(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(TableElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#terms(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through terms within the scope of this enumerator
51 52 53 54 |
# File 'lib/kitchen/element_enumerator_base.rb', line 51 def terms(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(TermElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |
#titles(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through titles within the scope of this enumerator
235 236 237 238 239 240 241 242 |
# File 'lib/kitchen/element_enumerator_base.rb', line 235 def titles(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(ElementEnumerator, default_css_or_xpath: '[data-type="title"]', css_or_xpath: css_or_xpath, only: only, except: except) end |
#to_s ⇒ String
Returns a concatenation of to_s
for all elements in the enumerator
476 477 478 |
# File 'lib/kitchen/element_enumerator_base.rb', line 476 def to_s map(&:to_s).join('') end |
#trash ⇒ Object
Removes all matching elements from the document
459 460 461 |
# File 'lib/kitchen/element_enumerator_base.rb', line 459 def trash each(&:trash) end |
#units(css_or_xpath = nil, only: nil, except: nil) ⇒ Object
Returns an enumerator that iterates through units within the scope of this enumerator
150 151 152 153 |
# File 'lib/kitchen/element_enumerator_base.rb', line 150 def units(css_or_xpath=nil, only: nil, except: nil) block_error_if(block_given?) chain_to(UnitElementEnumerator, css_or_xpath: css_or_xpath, only: only, except: except) end |