Module: Mache::DSL::ClassMethods

Defined in:
lib/mache/dsl.rb

Overview

Provides a set of macro-like methods for wrapping HTML fragments in Node objects.

## Elements

The #element method wraps an HTML fragment in a Node and exposes it as an attribute of the declaring class.

The following example declares the ‘main` element, which is found on the welcome page using the `#main` selector:

class WelcomePage < Mache::Page
  element :main, "#main"
end

The ‘main` element can be accessed as an attribute of a `WelcomePage` instance:

page = WelcomePage.new
page.has_main? # true
page.main.text # lorem ipsum

## Components

The #component method wraps an HTML fragment in a user-defined class and exposes it as an attribute of the declaring class.

The following example declaring the ‘alert` component, which is found on the welcome page using the `#alert` selector. The component will be wrapped in the user-defined `Alert` class:

class WelcomePage < Mache::Page
  component :alert, Alert, "#alert"
end

The ‘Alert` class can define an API for accessing the alert HTML fragment:

class Alert < Mache::Node
  element :close_button, "button.close"

  def dismiss
    close_button.click
  end
end

Instance Method Summary collapse

Instance Method Details

#automation(*ids) ⇒ Object



57
58
59
# File 'lib/mache/dsl.rb', line 57

def automation(*ids)
  ids.map { |id| %([data-automation="#{id}"]) }.join(' ')
end

#component(name, klass, selector, options = {}) ⇒ Object

Defines a component that wraps an HTML fragment.

Parameters:

  • name (String, Symbol)

    a name for the component

  • klass (Class)

    a component class

  • selector (String)

    a selector to find the component

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

    a hash of options to pass to the Capybara finder



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mache/dsl.rb', line 97

def component(name, klass, selector, options = {})
  unless klass < Node
    raise ArgumentError, 'Must be given a subclass of Node'
  end

  define_method(name.to_s) do
    klass.new(node: @node.find(selector, **options))
  end

  define_helper_methods(name, selector)
end

#components(name, klass, selector, options = {}) ⇒ Object

Defines a collection of components that wrap HTML fragments.

Parameters:

  • name (String, Symbol)

    a name for the components collection

  • klass (Class)

    a component class

  • selector (String)

    a selector to find the components

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

    a hash of options to pass to the Capybara finder



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mache/dsl.rb', line 115

def components(name, klass, selector, options = {})
  unless klass < Node
    raise ArgumentError, 'Must be given a subclass of Node'
  end

  options = {minimum: 1}.merge(options)

  define_method(name.to_s) do
    @node.all(selector, **options).map do |node|
      klass.new(node: node)
    end
  end

  define_helper_methods(name, selector)
end

#element(name, selector, options = {}) ⇒ Object

Defines an element that wraps an HTML fragment.

Parameters:

  • name (String, Symbol)

    a name for the element

  • selector (String)

    a selector to find the element

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

    a hash of options to pass to the Capybara finder



66
67
68
69
70
71
72
# File 'lib/mache/dsl.rb', line 66

def element(name, selector, options = {})
  define_method(name.to_s) do
    Node.new(node: @node.find(selector, **options))
  end

  define_helper_methods(name, selector)
end

#elements(name, selector, options = {}) ⇒ Object

Defines a collection of elements that wrap HTML fragments.

Parameters:

  • name (String, Symbol)

    a name for the elements collection

  • selector (String)

    a selector to find the elements

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

    a hash of options to pass to the Capybara finder



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mache/dsl.rb', line 79

def elements(name, selector, options = {})
  options = {minimum: 1}.merge(options)

  define_method(name.to_s) do
    @node.all(selector, **options).map do |node|
      Node.new(node: node)
    end
  end

  define_helper_methods(name, selector)
end