Class: PageMagic::Elements::Config

Inherits:
CONFIG_STRUCT show all
Defined in:
lib/page_magic/elements/config.rb

Overview

class Config - use to validate input to #elment

Constant Summary collapse

INVALID_SELECTOR_MSG =
'Pass a locator/define one on the class'
INVALID_ELEMENT_CLASS_MSG =
'Element class must be of type `PageMagic::Element`'
TYPE_REQUIRED_MESSAGE =
'element type required'

Instance Attribute Summary

Attributes inherited from CONFIG_STRUCT

#definition_class, #element, #element_class, #name, #options, #type

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(args, type) ⇒ Config

Create Config used to build instances PageMagic::Element see Page::Elements#element for details

Parameters:

  • args (Args<Object>)

    arguments passed to Page::Elements#element

Returns:



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/page_magic/elements/config.rb', line 24

def build(args, type)
  element_class = remove_argument(args, Class) || Element
  new(
    name: compute_name(args, element_class),
    type: type_for(type),
    selector: compute_selector(args, element_class),
    options: compute_argument(args, Hash),
    element: args.delete_at(0),
    element_class: element_class
  )
end

Instance Method Details

#element_optionsHash<Symbol,Object>

Options for the building of PageMagic::Element via PageMagic::ElementDefinitionBuilder#new

Returns:



69
70
71
# File 'lib/page_magic/elements/config.rb', line 69

def element_options
  to_h.except(:element_class, :name, :type, :options).update(selector: selector)
end

#selectorPageMagic::Element::Selector::Model

Selector built using supplied configuration



75
76
77
78
79
80
# File 'lib/page_magic/elements/config.rb', line 75

def selector
  selector = self[:selector] || definition_class.selector
  raise PageMagic::InvalidConfigurationException, INVALID_SELECTOR_MSG unless validate_selector?(selector)

  Element::Selector.find(selector.keys.first).build(type, selector.values.first, options: options)
end

#validate!PageMagic::Elements::Config

Validate supplied configuration



85
86
87
88
89
90
# File 'lib/page_magic/elements/config.rb', line 85

def validate!
  raise PageMagic::InvalidConfigurationException, 'element type required' unless type
  raise PageMagic::InvalidConfigurationException, INVALID_ELEMENT_CLASS_MSG unless valid_element_class?

  self
end