class Domino::Attribute

Attributes

callback[R]
name[R]
selector[R]

Public Class Methods

new(name, selector = nil, &callback) click to toggle source
# File lib/domino/attribute.rb, line 4
def initialize(name, selector = nil, &callback)
  @callback = callback
  @name = name
  @selector = selector || %(.#{name.to_s.tr('_', '-')})
end

Public Instance Methods

convert(value) click to toggle source
# File lib/domino/attribute.rb, line 15
def convert(value)
  if value && callback.is_a?(Proc)
    callback.call(value)
  else
    value
  end
end
element(node) click to toggle source
# File lib/domino/attribute.rb, line 46
def element(node)
  if combinator?
    node
  else
    node.find(selector)
  end
end
match_value?(node, value = nil, &predicate) click to toggle source
# File lib/domino/attribute.rb, line 36
def match_value?(node, value = nil, &predicate)
  if predicate.is_a?(Proc)
    predicate.call(element(node))
  else
    node_value = value(node)
    test_value = convert(value) rescue value
    test_value === node_value
  end
end
value(node) click to toggle source
# File lib/domino/attribute.rb, line 10
def value(node)
  val = value_before_typecast(node)
  convert(val)
end
value_before_typecast(node) click to toggle source

Get the text of the first dom element matching a selector

Dom::Post.all.first.attribute('.title')
# File lib/domino/attribute.rb, line 26
def value_before_typecast(node)
  if combinator?
    node[node_attribute_key] || node.matches_css?(combinator)
  else
    node.find(selector).text
  end
rescue Capybara::ElementNotFound
  nil
end

Private Instance Methods

combinator() click to toggle source
# File lib/domino/attribute.rb, line 60
def combinator
  @combinator ||= selector.sub(/&/, '') if combinator?
end
combinator?() click to toggle source
# File lib/domino/attribute.rb, line 56
def combinator?
  selector[0] == '&'.freeze
end
node_attribute_key() click to toggle source
# File lib/domino/attribute.rb, line 64
def node_attribute_key
  @node_attribute_key ||= combinator.match(/(?<=\[).+?(?=\])/) { |m| m[0] }
end