Class: Wrxer::WrxerCollection

Inherits:
WrxerObject show all
Includes:
Enumerable
Defined in:
lib/wrxer/wrxer_collection.rb

Instance Attribute Summary

Attributes inherited from WrxerObject

#document

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WrxerObject

#[], attribute, #attributes, #initialize, #method_missing, #to_json, #to_s

Methods inherited from Coercion

coerce, xpath

Constructor Details

This class inherits a constructor from Wrxer::WrxerObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wrxer::WrxerObject

Class Method Details

.call(document, params = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/wrxer/wrxer_collection.rb', line 6

def self.call(document, params = {})
  root = @xpath || params[:xpath]
  unless document.name == root
    document = document.xpath(root)
  end

  document.nil? ? nil : self.coerce(document)
end

.collection(value, xpath, parser) ⇒ Object



15
16
17
# File 'lib/wrxer/wrxer_collection.rb', line 15

def self.collection(value, xpath, parser)
  @collection = Attribute.new(value, xpath, parser)
end

.inherited(subclass) ⇒ Object



19
20
21
22
# File 'lib/wrxer/wrxer_collection.rb', line 19

def self.inherited(subclass)
  subclass.instance_variable_set(:@collection, @collection)
  super
end

Instance Method Details

#collectionObject



24
25
26
# File 'lib/wrxer/wrxer_collection.rb', line 24

def collection
  self.class.instance_variable_get(:@collection)
end

#dataObject



36
37
38
# File 'lib/wrxer/wrxer_collection.rb', line 36

def data
  lazy
end

#each(&block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/wrxer/wrxer_collection.rb', line 28

def each(&block)
  document.xpath(collection.xpath).each do |item|
    block.call(
      collection.call(item)
    )
  end
end

#inspectObject



40
41
42
# File 'lib/wrxer/wrxer_collection.rb', line 40

def inspect
  "#<#{self.class}:0x#{self.object_id.to_s(16)}> { data: #{data} } }"
end

#to_hashObject



44
45
46
# File 'lib/wrxer/wrxer_collection.rb', line 44

def to_hash
  { data: data }
end