Class: MicroMicro::Item
- Inherits:
-
Object
- Object
- MicroMicro::Item
- Defined in:
- lib/micro_micro/item.rb
Instance Attribute Summary collapse
-
#collection ⇒ MicroMicro::PropertiesCollection
The ItemsCollection to which this Item belongs.
Class Method Summary collapse
-
.from_context(context) ⇒ Array<MicroMicro::Item>
Extract Items from a context.
Instance Method Summary collapse
-
#children ⇒ MicroMicro::Collections::ItemsCollection
A collection of child Items parsed from the node.
- #children? ⇒ Boolean
-
#id ⇒ String?
The value of the node’s
id
attribute, if present. -
#id? ⇒ Boolean
Does this Item have an
id
attribute value?. -
#initialize(node) ⇒ MicroMicro::Item
constructor
Parse a node for microformats2-encoded data.
-
#inspect ⇒ String
:nocov:.
-
#properties ⇒ MicroMicro::Collections::PropertiesCollection
A collection of Propertys parsed from the node.
-
#to_h ⇒ Hash
Return the parsed Item as a Hash.
-
#types ⇒ Array<String>
An Array of root class names parsed from the node’s
class
attribute.
Constructor Details
permalink #initialize(node) ⇒ MicroMicro::Item
Parse a node for microformats2-encoded data.
51 52 53 54 55 56 57 |
# File 'lib/micro_micro/item.rb', line 51 def initialize(node) @node = node properties << implied_name if implied_name? properties << implied_photo if implied_photo? properties << implied_url if implied_url? end |
Instance Attribute Details
permalink #collection ⇒ MicroMicro::PropertiesCollection
The MicroMicro::ItemsCollection to which this MicroMicro::Item belongs.
34 35 36 |
# File 'lib/micro_micro/item.rb', line 34 def collection @collection end |
Class Method Details
permalink .from_context(context) ⇒ Array<MicroMicro::Item>
Extract MicroMicro::Items from a context.
40 41 42 43 44 45 |
# File 'lib/micro_micro/item.rb', line 40 def self.from_context(context) ItemNodeSearch .new(context.document) .search(context) .map { |node| new(node) } end |
Instance Method Details
permalink #children ⇒ MicroMicro::Collections::ItemsCollection
A collection of child MicroMicro::Items parsed from the node.
65 66 67 |
# File 'lib/micro_micro/item.rb', line 65 def children @children ||= Collections::ItemsCollection.new(self.class.from_context(node.element_children)) end |
permalink #children? ⇒ Boolean
Does this MicroMicro::Item contain any child MicroMicro::Items?
72 73 74 |
# File 'lib/micro_micro/item.rb', line 72 def children? children.any? end |
permalink #id ⇒ String?
The value of the node’s id
attribute, if present.
79 80 81 |
# File 'lib/micro_micro/item.rb', line 79 def id @id ||= node["id"]&.strip end |
permalink #id? ⇒ Boolean
Does this MicroMicro::Item have an id
attribute value?
86 87 88 |
# File 'lib/micro_micro/item.rb', line 86 def id? id.present? end |
permalink #inspect ⇒ String
:nocov:
93 94 95 96 97 98 |
# File 'lib/micro_micro/item.rb', line 93 def inspect "#<#{self.class}:#{format("%#0x", object_id)} " \ "types: #{types.inspect}, " \ "properties: #{properties.count}, " \ "children: #{children.count}>" end |
permalink #properties ⇒ MicroMicro::Collections::PropertiesCollection
A collection of Propertys parsed from the node.
104 105 106 |
# File 'lib/micro_micro/item.rb', line 104 def properties @properties ||= Collections::PropertiesCollection.new(Property.from_context(node.element_children)) end |
permalink #to_h ⇒ Hash
Return the parsed MicroMicro::Item as a Hash.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/micro_micro/item.rb', line 120 def to_h hash = { type: types, properties: properties.to_h } hash[:id] = id if id? hash[:children] = children.to_a if children? hash end |
permalink #types ⇒ Array<String>
An Array of root class names parsed from the node’s class
attribute.
135 136 137 |
# File 'lib/micro_micro/item.rb', line 135 def types @types ||= Helpers.root_class_names_from(node) end |