Module: SeleniumRecord::ComponentAutoload

Included in:
Base
Defined in:
lib/selenium_record/component_autoload.rb

Overview

Provides shortcuts for selenium object creation

Defined Under Namespace

Modules: ClassMethods Classes: UnknownComponent

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Proxies all calls to component methods



86
87
88
89
90
91
92
# 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

Class Method Details

.component_loader(component_type, _, opts = {}) ⇒ Object

Parameters:

  • component_type (Symbol)

    type of component

  • _ (String)

    the return type to show in autogenerated documentation

  • opts (Hash) (defaults to: {})

    the options of component loader

  • opts (Array<String>) (defaults to: {})

    suffixes The list of valid suffixes for parent classes of the instantiated component

Options Hash (opts):

  • :nested_folder (String)

    Marks whether to search the component inside a folder specific to parent component



52
53
54
55
56
57
58
59
60
61
# 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 = {}) ⇒ Object

Parameters:

  • suffixes (Array<String>)


25
26
27
28
29
30
31
32
# 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) ⇒ Module

Returns the module containing the classes for the marker type group.

Parameters:

  • modules (Array<String>)

Returns:

  • (Module)

    the module containing the classes for the marker type group



18
19
20
21
22
# 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 = {}) ⇒ Object

Parameters:



38
39
40
41
42
43
# 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) ⇒ Object



8
9
10
11
12
13
# 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

Instance Method Details

Parameters:

  • modal_sym (Symbol)

Returns:



73
# File 'lib/selenium_record/component_autoload.rb', line 73

component_loader :modal, 'SeleniumRecord::Base'

#panel_for(panel_sym) ⇒ SeleniumRecord::Base

Parameters:

  • panel_sym (Symbol)

Returns:



67
68
# File 'lib/selenium_record/component_autoload.rb', line 67

component_loader :panel, 'SeleniumRecord::Base', nested_folder: true,
suffixes: %w(Pill)

#pill_for(pill_sym) ⇒ SeleniumObjects::Pills::ApplicationPill

Parameters:

  • pill_sym (Symbol)

Returns:

  • (SeleniumObjects::Pills::ApplicationPill)


71
72
# File 'lib/selenium_record/component_autoload.rb', line 71

component_loader :pill, 'SeleniumObjects::Pills::ApplicationPill',
nested_folder: true, suffixes: %w(Tab)

#tab_for(tab_sym) ⇒ SeleniumObjects::Tabs::ApplicationTab

Parameters:

  • tab_sym (Symbol)

Returns:

  • (SeleniumObjects::Tabs::ApplicationTab)


69
70
# File 'lib/selenium_record/component_autoload.rb', line 69

component_loader :tab, 'SeleniumObjects::Tabs::ApplicationTab',
nested_folder: true

#view_for(model, opts = {}) ⇒ SeleniumObject::View::ApplicationView

Parameters:

  • model (ActiveRecord::Base)
  • opts (Hash) (defaults to: {})

    the options to instance a view

  • opts (String) (defaults to: {})

    :subject The name of the view. Default to model class name

Returns:

  • (SeleniumObject::View::ApplicationView)


80
81
82
83
# 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