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
.click
end
end
Instance Method Summary collapse
- #automation(*ids) ⇒ Object
-
#component(name, klass, selector, options = {}) ⇒ Object
Defines a component that wraps an HTML fragment.
-
#components(name, klass, selector, options = {}) ⇒ Object
Defines a collection of components that wrap HTML fragments.
-
#element(name, selector, options = {}) ⇒ Object
Defines an element that wraps an HTML fragment.
-
#elements(name, selector, options = {}) ⇒ Object
Defines a collection of elements that wrap HTML fragments.
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.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mache/dsl.rb', line 97 def component(name, klass, selector, = {}) 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, **)) end define_helper_methods(name, selector) end |
#components(name, klass, selector, options = {}) ⇒ Object
Defines a collection of components that wrap HTML fragments.
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, = {}) unless klass < Node raise ArgumentError, 'Must be given a subclass of Node' end = {minimum: 1}.merge() define_method(name.to_s) do @node.all(selector, **).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.
66 67 68 69 70 71 72 |
# File 'lib/mache/dsl.rb', line 66 def element(name, selector, = {}) define_method(name.to_s) do Node.new(node: @node.find(selector, **)) end define_helper_methods(name, selector) end |
#elements(name, selector, options = {}) ⇒ Object
Defines a collection of elements that wrap HTML fragments.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mache/dsl.rb', line 79 def elements(name, selector, = {}) = {minimum: 1}.merge() define_method(name.to_s) do @node.all(selector, **).map do |node| Node.new(node: node) end end define_helper_methods(name, selector) end |