Class: DocumentBuilder::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/document_builder/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, selector:, type:) ⇒ Collection

Returns a new instance of Collection.



6
7
8
9
10
# File 'lib/document_builder/collection.rb', line 6

def initialize(name, selector:, type:)
  @name = name
  @selector = selector
  @type = type
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/document_builder/collection.rb', line 4

def name
  @name
end

#nodeObject (readonly)

Returns the value of attribute node.



4
5
6
# File 'lib/document_builder/collection.rb', line 4

def node
  @node
end

#selectorObject (readonly)

Returns the value of attribute selector.



4
5
6
# File 'lib/document_builder/collection.rb', line 4

def selector
  @selector
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/document_builder/collection.rb', line 4

def type
  @type
end

Instance Method Details

#call(document) ⇒ Object



12
13
14
15
16
# File 'lib/document_builder/collection.rb', line 12

def call(document)
  return nil if document.nil?
  @node = document.xpath(selector)
  self
end

#eachObject



18
19
20
21
22
# File 'lib/document_builder/collection.rb', line 18

def each
  @node.each do |element|
    yield type.call(element)
  end
end

#inspectObject



28
29
30
# File 'lib/document_builder/collection.rb', line 28

def inspect
  "#<#{self.class}:0x#{self.object_id.to_s(16)}> Attributes: " + JSON.pretty_generate(to_hash)
end

#to_hashObject



36
37
38
# File 'lib/document_builder/collection.rb', line 36

def to_hash
  entries.map(&:to_hash)
end

#to_json(*args) ⇒ Object



32
33
34
# File 'lib/document_builder/collection.rb', line 32

def to_json(*args)
  JSON.generate(to_hash)
end

#to_s(*args) ⇒ Object



24
25
26
# File 'lib/document_builder/collection.rb', line 24

def to_s(*args)
  JSON.pretty_generate(to_hash)
end