Class: PageMagic::ElementDefinitionBuilder

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

Overview

Builder for creating ElementDefinitions

Constant Summary collapse

INVALID_SELECTOR_MSG =
'Pass a locator/define one on the class'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition_class:, selector:, type:, options: {}, element: nil) ⇒ ElementDefinitionBuilder

Returns a new instance of ElementDefinitionBuilder.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/page_magic/element_definition_builder.rb', line 7

def initialize(definition_class:, selector:, type:, options: {}, element: nil)
  unless element
    selector ||= definition_class.selector
    raise UndefinedSelectorException, INVALID_SELECTOR_MSG if selector.nil? || selector.empty?
  end

  @definition_class = definition_class
  @selector = selector
  @type = type
  @query_builder = Element::QueryBuilder.find(type)

  @options = { multiple_results: false }.merge(options)
  @element = element
end

Instance Attribute Details

#definition_classObject (readonly)

Returns the value of attribute definition_class.



5
6
7
# File 'lib/page_magic/element_definition_builder.rb', line 5

def definition_class
  @definition_class
end

#elementObject (readonly)

Returns the value of attribute element.



5
6
7
# File 'lib/page_magic/element_definition_builder.rb', line 5

def element
  @element
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/page_magic/element_definition_builder.rb', line 5

def options
  @options
end

#query_builderObject (readonly)

Returns the value of attribute query_builder.



5
6
7
# File 'lib/page_magic/element_definition_builder.rb', line 5

def query_builder
  @query_builder
end

#selectorObject (readonly)

Returns the value of attribute selector.



5
6
7
# File 'lib/page_magic/element_definition_builder.rb', line 5

def selector
  @selector
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/page_magic/element_definition_builder.rb', line 5

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



35
36
37
38
39
# File 'lib/page_magic/element_definition_builder.rb', line 35

def ==(other)
  return false unless other.is_a?(ElementDefinitionBuilder)
  this = [options, selector, type, element, definition_class]
  this == [other.options, other.selector, other.type, other.element, other.definition_class]
end

#build(browser_element) ⇒ 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:



31
32
33
# File 'lib/page_magic/element_definition_builder.rb', line 31

def build(browser_element)
  definition_class.new(browser_element)
end

#build_queryCapybara::Query

Returns query to find this element in the browser.

Returns:

  • (Capybara::Query)

    query to find this element in the browser



23
24
25
26
# File 'lib/page_magic/element_definition_builder.rb', line 23

def build_query
  multiple_results = options.delete(:multiple_results)
  query_builder.build(selector, options, multiple_results: multiple_results)
end