class PageMagic::Elements::Config

class Config - use to validate input to {PageMagic::Elements#elment}

Constants

INVALID_ELEMENT_CLASS_MSG
INVALID_SELECTOR_MSG
TYPE_REQUIRED_MESSAGE

Public Class Methods

build(args, type) click to toggle source

Create ‘Config` used to build instances `PageMagic::Element` see `Page::Elements#element` for details @param [Args<Object>] args arguments passed to `Page::Elements#element` @return [Config]

# 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

Private Class Methods

compute_argument(args, clazz) click to toggle source
# File lib/page_magic/elements/config.rb, line 48
def compute_argument(args, clazz)
  remove_argument(args, clazz) || clazz.new
end
compute_name(args, element_class) click to toggle source
# File lib/page_magic/elements/config.rb, line 38
def compute_name(args, element_class)
  name = remove_argument(args, Symbol)
  name || element_class.name.demodulize.underscore.to_sym unless element_class.is_a?(Element)
end
compute_selector(args, element_class) click to toggle source
# File lib/page_magic/elements/config.rb, line 43
def compute_selector(args, element_class)
  selector = remove_argument(args, Hash)
  selector || element_class.selector if element_class.respond_to?(:selector)
end
field?(type) click to toggle source
# File lib/page_magic/elements/config.rb, line 61
def field?(type)
  %i[ text_field checkbox select_list radio textarea field file_field fillable_field
      radio_button select].include?(type)
end
remove_argument(args, clazz) click to toggle source
# File lib/page_magic/elements/config.rb, line 52
def remove_argument(args, clazz)
  argument = args.find { |arg| arg.is_a?(clazz) }
  args.delete(argument)
end
type_for(type) click to toggle source
# File lib/page_magic/elements/config.rb, line 57
def type_for(type)
  field?(type) ? :field : type
end

Public Instance Methods

element_options() click to toggle source

Options for the building of ‘PageMagic::Element` via `PageMagic::ElementDefinitionBuilder#new` @return [Hash<Symbol,Object>]

# File lib/page_magic/elements/config.rb, line 69
def element_options
  to_h.except(:element_class, :name, :type, :options).update(selector: selector)
end
selector() click to toggle source

Selector built using supplied configuration @return [PageMagic::Element::Selector::Model]

# 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!() click to toggle source

Validate supplied configuration @raise [PageMagic::InvalidConfigurationException] @return [PageMagic::Elements::Config]

# 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

Private Instance Methods

valid_element_class?() click to toggle source
# File lib/page_magic/elements/config.rb, line 98
def valid_element_class?
  element_class && (element_class == PageMagic::Element || element_class < PageMagic::Element)
end
validate_selector?(selector) click to toggle source
# File lib/page_magic/elements/config.rb, line 94
def validate_selector?(selector)
  selector.is_a?(Hash) && !selector.empty?
end