Class: MicroMicro::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/micro_micro/item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ MicroMicro::Item

Parse a node for microformats2-encoded data.

Parameters:

  • node (Nokogiri::XML::Element)
[View source]

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

#collectionMicroMicro::PropertiesCollection

The MicroMicro::ItemsCollection to which this MicroMicro::Item belongs.

Returns:

  • (MicroMicro::PropertiesCollection)

34
35
36
# File 'lib/micro_micro/item.rb', line 34

def collection
  @collection
end

Class Method Details

.from_context(context) ⇒ Array<MicroMicro::Item>

Extract MicroMicro::Items from a context.

Parameters:

  • context (Nokogiri::HTML5::Document, Nokogiri::XML::NodeSet, Nokogiri::XML::Element)

Returns:

[View source]

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

#childrenMicroMicro::Collections::ItemsCollection

A collection of child MicroMicro::Items parsed from the node.

[View source]

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

#children?Boolean

Does this MicroMicro::Item contain any child MicroMicro::Items?

Returns:

  • (Boolean)
[View source]

72
73
74
# File 'lib/micro_micro/item.rb', line 72

def children?
  children.any?
end

#idString?

The value of the node’s id attribute, if present.

Returns:

  • (String, nil)
[View source]

79
80
81
# File 'lib/micro_micro/item.rb', line 79

def id
  @id ||= node["id"]&.strip
end

#id?Boolean

Does this MicroMicro::Item have an id attribute value?

Returns:

  • (Boolean)
[View source]

86
87
88
# File 'lib/micro_micro/item.rb', line 86

def id?
  id.present?
end

#inspectString

:nocov:

Returns:

  • (String)
[View source]

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

#propertiesMicroMicro::Collections::PropertiesCollection

A collection of Propertys parsed from the node.

[View source]

104
105
106
# File 'lib/micro_micro/item.rb', line 104

def properties
  @properties ||= Collections::PropertiesCollection.new(Property.from_context(node.element_children))
end

#to_hHash

Return the parsed MicroMicro::Item as a Hash.

[View source]

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

#typesArray<String>

An Array of root class names parsed from the node’s class attribute.

Returns:

  • (Array<String>)
[View source]

135
136
137
# File 'lib/micro_micro/item.rb', line 135

def types
  @types ||= Helpers.root_class_names_from(node)
end