Class: MicroMicro::Property

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

Direct Known Subclasses

ImpliedProperty

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, token) ⇒ Property

Parse a node for property data.

Parameters:

  • node (Nokogiri::XML::Element)
  • token (String)

    A hyphen-separated token representing a microformats2 property value (e.g. p-name, u-url).


75
76
77
78
# File 'lib/micro_micro/property.rb', line 75

def initialize(node, token)
  @node = node
  @prefix, @name = token.split("-", 2)
end

Instance Attribute Details

#collectionMicroMicro::PropertiesCollection

The MicroMicro::PropertiesCollection to which this MicroMicro::Property belongs.

Returns:

  • (MicroMicro::PropertiesCollection)

40
41
42
# File 'lib/micro_micro/property.rb', line 40

def collection
  @collection
end

#nameString (readonly)

This MicroMicro::Property‘s name value.

Returns:

  • (String)

45
46
47
# File 'lib/micro_micro/property.rb', line 45

def name
  @name
end

#nodeNokogiri::XML::Element (readonly)

This MicroMicro::Property‘s node.

Returns:

  • (Nokogiri::XML::Element)

50
51
52
# File 'lib/micro_micro/property.rb', line 50

def node
  @node
end

#prefixString (readonly)

This MicroMicro::Property‘s prefix value.

Returns:

  • (String)

    One of dt, e, p, or u.


55
56
57
# File 'lib/micro_micro/property.rb', line 55

def prefix
  @prefix
end

Class Method Details

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

Extract MicroMicro::Propertys from a context.

Parameters:

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

Returns:


61
62
63
64
65
66
67
68
# File 'lib/micro_micro/property.rb', line 61

def self.from_context(context)
  PropertyNodeSearch
    .new(context.document)
    .search(context)
    .flat_map do |node|
      Helpers.property_class_names_from(node).map { |token| new(node, token) }
    end
end

Instance Method Details

#date_time_property?Boolean

Is this MicroMicro::Property a datetime property?

Returns:

  • (Boolean)

83
84
85
# File 'lib/micro_micro/property.rb', line 83

def date_time_property?
  prefix == "dt"
end

#embedded_markup_property?Boolean

Is this MicroMicro::Property an embedded markup property?

Returns:

  • (Boolean)

90
91
92
# File 'lib/micro_micro/property.rb', line 90

def embedded_markup_property?
  prefix == "e"
end

#implied?Boolean

Always return false when asked if this MicroMicro::Property is an implied property.

Returns:

  • (Boolean)

See Also:


100
101
102
# File 'lib/micro_micro/property.rb', line 100

def implied?
  false
end

#inspectString

:nocov:

Returns:

  • (String)

107
108
109
110
111
112
# File 'lib/micro_micro/property.rb', line 107

def inspect
  "#<#{self.class}:#{format("%#0x", object_id)} " \
    "name: #{name.inspect}, " \
    "prefix: #{prefix.inspect}, " \
    "value: #{value.inspect}>"
end

#itemMicroMicro::Item?

Parse this MicroMicro::Property‘s node as a Item, if applicable.

Returns:


119
120
121
# File 'lib/micro_micro/property.rb', line 119

def item
  @item ||= Item.new(node) if item_node?
end

#item_node?Boolean

Should this MicroMicro::Property‘s node be parsed as a Item?

Returns:

  • (Boolean)

See Also:


129
130
131
# File 'lib/micro_micro/property.rb', line 129

def item_node?
  @item_node ||= Helpers.item_node?(node)
end

#plain_text_property?Boolean

Is this MicroMicro::Property a plain text property?

Returns:

  • (Boolean)

136
137
138
# File 'lib/micro_micro/property.rb', line 136

def plain_text_property?
  prefix == "p"
end

#url_property?Boolean

Is this MicroMicro::Property a url property?

Returns:

  • (Boolean)

143
144
145
# File 'lib/micro_micro/property.rb', line 143

def url_property?
  prefix == "u"
end

#valueString, Hash

Return this MicroMicro::Property‘s parsed value.

rubocop:disable Metrics

Returns:

  • (String, Hash)

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/micro_micro/property.rb', line 152

def value
  @value ||=
    if item_node?
      hash = item.to_h

      return hash.merge(parser.value) if embedded_markup_property?

      p_property = item.properties.find_by(name: "name") if plain_text_property?
      u_property = item.properties.find_by(name: "url") if url_property?

      hash.merge(value: (p_property || u_property || parser).value)
    else
      parser.value
    end
end

#value?Boolean

Returns true if this MicroMicro::Property‘s value is anything other than blank or nil.

Returns:

  • (Boolean)

173
174
175
# File 'lib/micro_micro/property.rb', line 173

def value?
  value.present?
end