Class: Fleakr::Support::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sources = nil) ⇒ Attribute

Returns a new instance of Attribute.



9
10
11
12
13
14
# File 'lib/fleakr/support/attribute.rb', line 9

def initialize(name, sources = nil)
  @name = name.to_sym

  @sources = Array(sources)
  @sources << @name.to_s if @sources.empty?
end

Instance Attribute Details

#nameObject (readonly)

TODO: Refactor the location / attribute logic into a Source class



7
8
9
# File 'lib/fleakr/support/attribute.rb', line 7

def name
  @name
end

#sourcesObject (readonly)

TODO: Refactor the location / attribute logic into a Source class



7
8
9
# File 'lib/fleakr/support/attribute.rb', line 7

def sources
  @sources
end

Instance Method Details

#attribute(source) ⇒ Object



27
28
29
30
# File 'lib/fleakr/support/attribute.rb', line 27

def attribute(source)
  location, attribute = source.split('@')
  attribute || location
end

#location(source) ⇒ Object



32
33
34
# File 'lib/fleakr/support/attribute.rb', line 32

def location(source)
  split(source).first
end

#node_for(document, source) ⇒ Object



23
24
25
# File 'lib/fleakr/support/attribute.rb', line 23

def node_for(document, source)
  document.at(location(source)) || document.search("//[@#{attribute(source)}]").first
end

#split(source) ⇒ Object



16
17
18
19
20
21
# File 'lib/fleakr/support/attribute.rb', line 16

def split(source)
  location, attribute = source.split('@')
  location = self.name.to_s if location.blank?

  [location, attribute]
end

#value_from(document) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/fleakr/support/attribute.rb', line 36

def value_from(document)
  values = sources.map do |source|
    node = node_for(document, source)
    [node.attributes[attribute(source)], node.inner_text].reject{|v| v.blank?}.first unless node.nil?
  end
  values.compact.first
end