Class: Domino
- Inherits:
-
Object
- Object
- Domino
- Extended by:
- Capybara::DSL, Enumerable
- Includes:
- Capybara::DSL
- Defined in:
- lib/domino.rb
Overview
assert_nil Dom::Post.find_by_title(‘First Post’)
Defined Under Namespace
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Direct access to the capybara node, in case you need anything special.
Class Method Summary collapse
-
.all ⇒ Object
Get an array of all the Dominos.
-
.attribute(attribute, selector = nil, &callback) ⇒ Object
Define an attribute for this Domino.
- .attribute_definitions ⇒ Object
- .attributes ⇒ Object
-
.each ⇒ Object
Iterate over all the Dominos.
-
.find! ⇒ Object
Returns Domino for capybara node matching selector.
-
.find_by(attributes) ⇒ Object
Returns Domino for capybara node matching all attributes.
-
.find_by!(attributes) ⇒ Object
Returns Domino for capybara node matching all attributes.
-
.selector(s) ⇒ Object
Define the selector for this Domino.
-
.where(attributes) ⇒ Object
Returns collection of Dominos for capybara node matching all attributes.
Instance Method Summary collapse
-
#attribute(selector, &callback) ⇒ Object
Get the text of the first dom element matching a selector:.
- #attributes ⇒ Object
-
#id ⇒ Object
Dom id for this object.
Instance Attribute Details
#node ⇒ Object (readonly)
Direct access to the capybara node, in case you need anything special
52 53 54 |
# File 'lib/domino.rb', line 52 def node @node end |
Class Method Details
.all ⇒ Object
Get an array of all the Dominos
65 66 67 |
# File 'lib/domino.rb', line 65 def all map { |domino| domino } end |
.attribute(attribute, selector = nil, &callback) ⇒ Object
Define an attribute for this Domino
module Dom
class Post
attribute :title # defaults to selector '.title'
attribute :body, '.post-body' # use a custom selector
end
end
This will define an attr_reader on the Domino and also a find_by_attribute method:
Dom::Post.all.first.title
Dom::Post.find_by_title("First Post")
Dom::Post.find_by_title(/^First/)
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/domino.rb', line 134 def attribute(attribute, selector = nil, &callback) selector ||= %(.#{attribute.to_s.tr('_', '-')}) attribute_definitions[attribute] = Attribute.new(attribute, selector, &callback) class_eval %{ def #{attribute} self.class.attribute_definitions[:#{attribute}].value(node) end def self.find_by_#{attribute}(value) find_by_attribute(:#{attribute}, value) end } end |
.attribute_definitions ⇒ Object
115 116 117 |
# File 'lib/domino.rb', line 115 def attribute_definitions @attribute_definitions ||= {} end |
.attributes ⇒ Object
111 112 113 |
# File 'lib/domino.rb', line 111 def attributes attribute_definitions.keys end |
.each ⇒ Object
Iterate over all the Dominos
58 59 60 61 62 |
# File 'lib/domino.rb', line 58 def each nodes.each do |node| yield new(node) end end |
.find! ⇒ Object
Returns Domino for capybara node matching selector.
Raises an error if no matching node is found. For drivers that support asynchronous behavior, this method waits for a matching node to appear.
74 75 76 77 |
# File 'lib/domino.rb', line 74 def find! require_selector! new(Capybara.current_session.find(@selector)) end |
.find_by(attributes) ⇒ Object
Returns Domino for capybara node matching all attributes.
80 81 82 |
# File 'lib/domino.rb', line 80 def find_by(attributes) where(attributes).first end |
.find_by!(attributes) ⇒ Object
Returns Domino for capybara node matching all attributes.
Raises an error if no matching node is found.
87 88 89 |
# File 'lib/domino.rb', line 87 def find_by!(attributes) find_by(attributes) || raise(Capybara::ElementNotFound) end |
.selector(s) ⇒ Object
Define the selector for this Domino
module Dom
class Post
selector '#posts .post'
end
end
107 108 109 |
# File 'lib/domino.rb', line 107 def selector(s) @selector = s end |
.where(attributes) ⇒ Object
Returns collection of Dominos for capybara node matching all attributes.
92 93 94 95 96 97 98 |
# File 'lib/domino.rb', line 92 def where(attributes) select do |domino| attributes.all? do |key, value| domino.send(key) == value if domino.respond_to?(key) end end end |
Instance Method Details
#attribute(selector, &callback) ⇒ Object
Get the text of the first dom element matching a selector:
Dom::Post.all.first.attribute('.title')
Or get the value of the attribute of this dom element:
Dom::Post.all.first.attribute('&[href]')
176 177 178 |
# File 'lib/domino.rb', line 176 def attribute(selector, &callback) Attribute.new(nil, selector, &callback).value(node) end |
#attributes ⇒ Object
185 186 187 188 189 |
# File 'lib/domino.rb', line 185 def attributes self.class.attributes.each_with_object({}) do |attribute, memo| memo[attribute] = send(attribute) end end |
#id ⇒ Object
Dom id for this object.
181 182 183 |
# File 'lib/domino.rb', line 181 def id node['id'].nil? ? nil : %(##{node['id']}) end |