Class: Elementor::ElementSet
- Includes:
- NodeSet
- 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 included from NodeSet
included, #initialize, #replace, #select
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
73 74 75 |
# File 'lib/elementor/element_set.rb', line 73 def method_missing(sym, *args, &block) result.respond_to?(sym) ? result.send(sym, self, *args) : super end |
Instance Attribute Details
#result ⇒ Object
Returns the value of attribute result.
42 43 44 |
# File 'lib/elementor/element_set.rb', line 42 def result @result end |
#selector ⇒ Object
Returns the value of attribute selector.
42 43 44 |
# File 'lib/elementor/element_set.rb', line 42 def selector @selector end |
Instance Method Details
#respond_to?(sym) ⇒ Boolean
77 78 79 |
# File 'lib/elementor/element_set.rb', line 77 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.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/elementor/element_set.rb', line 59 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.
46 47 48 49 50 51 52 53 54 |
# File 'lib/elementor/element_set.rb', line 46 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 |