module SeleniumRecord::ComponentAutoload

Provides shortcuts for selenium object creation

Public Class Methods

component_loader(component_type, _, opts = {}) click to toggle source

@param component_type [Symbol] type of component @param _ [String] the return type to show in autogenerated documentation @param [Hash] opts the options of component loader @param opts [Array<String>] suffixes The list of valid suffixes for

parent classes of the instantiated component

@option opts [String] :nested_folder Marks whether to search the

component inside a folder specific to parent component
# File lib/selenium_record/component_autoload.rb, line 52
def self.component_loader(component_type, _, opts = {})
  mod = self
  define_method "#{component_type}_for" do |item|
    record_opts = mod.extract_options(component_type, self.class, {
      subject: item.to_s.camelize,
      suffixes: %w(View)
    }.merge(opts))
    create_record(object, record_opts).tap(&:load_dom)
  end
end
extract_group(klass_name, opts = {}) click to toggle source

@param suffixes [Array<String>]

# File lib/selenium_record/component_autoload.rb, line 25
def self.extract_group(klass_name, opts = {})
  return unless opts[:nested_folder]
  suffixes = [*opts[:suffixes]]
  suffixes.map do |suffix|
    match_data = Regexp.new("^(.*)#{suffix}$").match(klass_name)
    match_data[1] if match_data
  end.compact.first
end
extract_namespace(*modules) click to toggle source

@param modules [Array<String>] @return [Module] the module containing the classes for the marker type

group
# File lib/selenium_record/component_autoload.rb, line 18
def self.extract_namespace(*modules)
  modules.compact.reduce(Configuration.objects_module) do |klass, sym|
    klass.const_get(sym)
  end
end
extract_options(component_type, klass, opts = {}) click to toggle source

@param component_type [Symbol] @param item [Symbol] @param klass [SeleniumRecord::Base] @param suffixes [Array<String>]

# File lib/selenium_record/component_autoload.rb, line 38
def self.extract_options(component_type, klass, opts = {})
  suffix = component_type.to_s.capitalize
  group = extract_group(klass.name.split('::').last, opts)
  namespace = extract_namespace(suffix.pluralize, group)
  { namespace: namespace, suffix: suffix, subject: opts[:subject] }
end
included(base) click to toggle source
# File lib/selenium_record/component_autoload.rb, line 8
def self.included(base)
  base.extend(ClassMethods)
  base.instance_eval do
    attr_reader :components
  end
end

Public Instance Methods

method_missing(method, *args) click to toggle source

Proxies all calls to component methods

Calls superclass method
# File lib/selenium_record/component_autoload.rb, line 86
def method_missing(method, *args)
  [*@components].each do |comp|
    return comp.send(method, *args) if comp.respond_to?(method)
  end
rescue UnknownComponent
  super
end
view_for(model, opts = {}) click to toggle source

@param model [ActiveRecord::Base] @param [Hash] opts the options to instance a view @param opts [String] :subject The name of the view. Default to model class

name

@return [SeleniumObject::View::ApplicationView]

# File lib/selenium_record/component_autoload.rb, line 80
def view_for(model, opts = {})
  view_options = opts.merge namespace: so_module(:views), suffix: 'View'
  create_record(model, view_options).tap(&:load_dom)
end

Private Instance Methods

component_for(name) click to toggle source

@param name [Symbol] name of the component. Valid formats Regex:

/(?<component_name>.*)_(?<component_type>view|tab|pill|modal|panel)$/
# File lib/selenium_record/component_autoload.rb, line 152
def component_for(name)
  matches = self.class.component_method_regex.match(name)
  if matches
    send "#{matches[:component_type]}_for", matches[:component_name]
  else
    fail UnknownComponent
  end
end
load_component(name) click to toggle source

@param name [Symbol] name of the component. Valid formats Regex:

/(?<component_name>.*)_(?<component_type>view|panel)$/
# File lib/selenium_record/component_autoload.rb, line 144
def load_component(name)
  component = component_for name
  instance_variable_set("@#{name}", component)
  @components << component
end
load_components(names) click to toggle source

@param names [Array<Symbol>] list of names of the components.

Valid formats Regex:
/(?<component_name>.*)_(?<component_type>view|tab|pill|modal|panel)$/
# File lib/selenium_record/component_autoload.rb, line 137
def load_components(names)
  @components ||= []
  names.each { |name| load_component name }
end