Class: Domino::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/domino/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, selector = nil, &callback) ⇒ Attribute

Returns a new instance of Attribute



4
5
6
7
8
# File 'lib/domino/attribute.rb', line 4

def initialize(name, selector = nil, &callback)
  @callback = callback
  @name = name
  @selector = selector || %(.#{name.to_s.tr('_', '-')})
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback



2
3
4
# File 'lib/domino/attribute.rb', line 2

def callback
  @callback
end

#nameObject (readonly)

Returns the value of attribute name



2
3
4
# File 'lib/domino/attribute.rb', line 2

def name
  @name
end

#selectorObject (readonly)

Returns the value of attribute selector



2
3
4
# File 'lib/domino/attribute.rb', line 2

def selector
  @selector
end

Instance Method Details

#convert(value) ⇒ Object



15
16
17
18
19
20
21
# 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) ⇒ Object



46
47
48
49
50
51
52
# 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) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



10
11
12
13
# File 'lib/domino/attribute.rb', line 10

def value(node)
  val = value_before_typecast(node)
  convert(val)
end

#value_before_typecast(node) ⇒ Object

Get the text of the first dom element matching a selector

Dom::Post.all.first.attribute('.title')


26
27
28
29
30
31
32
33
34
# 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