Class: Locator::Result

Inherits:
Array
  • Object
show all
Defined in:
lib/locator/result.rb

Constant Summary collapse

MATCH_TYPES =
{
  :alt     => :contains,
  :title   => :contains,
  :content => :contains
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.contains?(value, selector) ⇒ Boolean

Returns:



26
27
28
# File 'lib/locator/result.rb', line 26

def contains?(value, selector)
  value.include?(selector)
end

.equals?(value, selector) ⇒ Boolean

Returns:



22
23
24
# File 'lib/locator/result.rb', line 22

def equals?(value, selector)
  value == selector
end

.matches?(name, value, selector) ⇒ Boolean

Returns:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/locator/result.rb', line 10

def matches?(name, value, selector)
  value, selector = Locator.decode(value), Locator.decode(selector) if Locator.decode_entities?
  value = normalize_whitespace(value)
  case selector
  when Regexp
    value =~ selector
  else
    type = MATCH_TYPES[name] || :equals
    send("#{type}?", value, selector)
  end
end

.normalize_whitespace(value) ⇒ Object



30
31
32
# File 'lib/locator/result.rb', line 30

def normalize_whitespace(value)
  value.gsub(/\s+/, ' ').strip
end

Instance Method Details

#+(other) ⇒ Object



48
49
50
# File 'lib/locator/result.rb', line 48

def +(other)
  replace(super)
end

#filter(selector, locatables) ⇒ Object



39
40
41
# File 'lib/locator/result.rb', line 39

def filter(selector, locatables)
  selector ? filter_by(selector, locatables) : self
end

#filter!(selector, locatables) ⇒ Object



35
36
37
# File 'lib/locator/result.rb', line 35

def filter!(selector, locatables)
  selector ? replace(filter(selector, locatables)) : self
end

#sort!Object



43
44
45
46
# File 'lib/locator/result.rb', line 43

def sort!
  each  { |element| element.matches.sort! }
  super { |lft, rgt| lft.matches.first.length <=> rgt.matches.first.length }
end