Class: Kiosk::Claim::NodeClaim

Inherits:
Object
  • Object
show all
Defined in:
lib/kiosk/claim/node_claim.rb

Direct Known Subclasses

PathClaim

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options = {}, &parser) ⇒ NodeClaim

Returns a new instance of NodeClaim.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
# File 'lib/kiosk/claim/node_claim.rb', line 6

def initialize(model, options = {}, &parser)
  raise ArgumentError.new('no selector given') unless options[:selector]
  raise ArgumentError.new('no block provided') unless block_given?

  @model = model
  @selector = options[:selector]
  @parser = parser
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/kiosk/claim/node_claim.rb', line 4

def model
  @model
end

#parserObject (readonly)

Returns the value of attribute parser.



4
5
6
# File 'lib/kiosk/claim/node_claim.rb', line 4

def parser
  @parser
end

#selectorObject (readonly)

Returns the value of attribute selector.



4
5
6
# File 'lib/kiosk/claim/node_claim.rb', line 4

def selector
  @selector
end

Instance Method Details

#stake!(document) ⇒ Object

Stakes the claim over the given content document and yields the provided block for each match. The block is passed each node, which has been extended with implementation in ClaimedNode.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kiosk/claim/node_claim.rb', line 19

def stake!(document)
  select_from(document).each do |node|
    unless node.is_a?(ClaimedNode)
      node.extend(ProspectiveNode) unless node.is_a?(ProspectiveNode)

      # If the parser finds anything in the selected node, stake the claim
      if attributes = parser.call(node)
        node.extend(ClaimedNode)
        node.resource = @model.new(attributes)

        yield node if block_given?
      end
    end
  end
end