Class: DocumentBuilder::Collection
- Inherits:
-
Object
- Object
- DocumentBuilder::Collection
- Includes:
- Enumerable
- Defined in:
- lib/document_builder/collection.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #call(document) ⇒ Object
- #each ⇒ Object
-
#initialize(name, selector:, type:) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ Object
- #to_hash ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s(*args) ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/document_builder/collection.rb', line 4 def name @name end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
4 5 6 |
# File 'lib/document_builder/collection.rb', line 4 def node @node end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
4 5 6 |
# File 'lib/document_builder/collection.rb', line 4 def selector @selector end |
#type ⇒ Object (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 |
#each ⇒ Object
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 |
#inspect ⇒ Object
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_hash ⇒ Object
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 |