Class: Elementor::ElementSet

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

Instance Method Summary collapse

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

#resultObject

Returns the value of attribute result.



42
43
44
# File 'lib/elementor/element_set.rb', line 42

def result
  @result
end

#selectorObject

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

Returns:

  • (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(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.



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