Class: Elementor::ElementSet
- Defined in:
- lib/elementor/element_set.rb
Overview
ElementSet objects wrap a Nokogiri #search result and add additional functionality such as chained filtering.
Instance Attribute Summary collapse
-
#result ⇒ Object
Returns the value of attribute result.
-
#selector ⇒ Object
Returns the value of attribute selector.
Instance Method Summary collapse
- #method_missing(sym, *args, &block) ⇒ Object
- #respond_to?(sym) ⇒ Boolean
-
#with_attrs(options = {}) ⇒ Object
(also: #attrs)
Attribute filtering using hashes.
-
#with_text(matcher) ⇒ Object
(also: #text)
A simple filter for selecting only elements with content that either includes a String passed in, or matches a Regexp.
Methods inherited from Array
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
36 37 38 |
# File 'lib/elementor/element_set.rb', line 36 def method_missing(sym, *args, &block) result.respond_to?(sym) ? result.send(sym, doc, *args) : super end |
Instance Attribute Details
#result ⇒ Object
Returns the value of attribute result.
5 6 7 |
# File 'lib/elementor/element_set.rb', line 5 def result @result end |
#selector ⇒ Object
Returns the value of attribute selector.
5 6 7 |
# File 'lib/elementor/element_set.rb', line 5 def selector @selector end |
Instance Method Details
#respond_to?(sym) ⇒ Boolean
40 41 42 |
# File 'lib/elementor/element_set.rb', line 40 def respond_to?(sym) result.respond_to?(sym) || super end |
#with_attrs(options = {}) ⇒ Object Also known as: attrs
Attribute filtering using hashes. See the specs for examples.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/elementor/element_set.rb', line 22 def with_attrs(={}) filter do |item| .all? do |key, value| case value when Regexp then item[key.to_s] =~ value when String then item[key.to_s] == value else item[key.to_s] == value.to_s end end end end |
#with_text(matcher) ⇒ Object Also known as: text
A simple filter for selecting only elements with content that either includes a String passed in, or matches a Regexp.
9 10 11 12 13 14 15 16 17 |
# File 'lib/elementor/element_set.rb', line 9 def with_text(matcher) filter do |item| case matcher when Regexp then item.text =~ matcher when String then item.text.include?(matcher) else item.text.include?(matcher.to_s) end end end |