Class: PageMagic::ElementDefinitionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/page_magic/element_definition_builder.rb

Overview

Builder for creating ElementDefinitions

Instance Method Summary collapse

Constructor Details

#initialize(definition_class:, selector:, query_class: PageMagic::Element::Query::SingleResult, element: nil) ⇒ ElementDefinitionBuilder

Returns a new instance of ElementDefinitionBuilder.



6
7
8
9
10
11
12
13
14
# File 'lib/page_magic/element_definition_builder.rb', line 6

def initialize(definition_class:, selector:, query_class: PageMagic::Element::Query::SingleResult, element: nil)
  @definition_class = definition_class

  @query = if element
             PageMagic::Element::Query::PrefetchedResult.new(element)
           else
             query_class.new(*selector.args, options: selector.options)
           end
end

Instance Method Details

#==(other) ⇒ Object



26
27
28
29
30
31
# File 'lib/page_magic/element_definition_builder.rb', line 26

def ==(other)
  return false unless other.is_a?(ElementDefinitionBuilder)

  this = [query, definition_class]
  this == [other.send(:query), other.send(:definition_class)]
end

#build(browser_element) ⇒ Capybara::Node::Element+

Create new instance of the ElementDefinition modeled by this builder

Parameters:

  • browser_element (Object)

    capybara browser element corresponding to the element modelled by this builder

Returns:

  • (Capybara::Node::Element)
  • (Array<Capybara::Node::Element>)


20
21
22
23
24
# File 'lib/page_magic/element_definition_builder.rb', line 20

def build(browser_element)
  query.execute(browser_element) do |result|
    definition_class.new(result)
  end
end