Class: Elementor::ElementSet

Inherits:
Array show all
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

Instance Method Summary collapse

Methods inherited from Array

#extract_options!

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

#resultObject

Returns the value of attribute result.



5
6
7
# File 'lib/elementor/element_set.rb', line 5

def result
  @result
end

#selectorObject

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

Returns:

  • (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(options={})
  filter do |item|
    options.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