Class: Coradoc::CoreModel::Metadata

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/core_model/metadata.rb

Overview

Represents metadata associated with a document element

Stores arbitrary key-value pairs for tracking source location, processing information, and other contextual data.

Examples:

meta = Metadata.new
meta["source_line"] = 42
meta["parser_version"] = "1.0.0"

Instance Attribute Summary

Attributes inherited from Base

#element_attributes, #id, #metadata_entries, #title

Instance Method Summary collapse

Methods inherited from Base

#accept, #attr, #metadata, #semantically_equivalent?, #set_attr, #set_metadata

Instance Method Details

#[](key) ⇒ String?

Get a metadata value by key

Parameters:

  • key (String)

    The metadata key

Returns:

  • (String, nil)

    The value or nil if not found



26
27
28
29
30
# File 'lib/coradoc/core_model/metadata.rb', line 26

def [](key)
  return nil if entries.nil?

  find_entry(key)&.value
end

#[]=(key, value) ⇒ Object

Set a metadata value

Parameters:

  • key (String)

    The metadata key

  • value (String)

    The value to set



35
36
37
38
39
40
41
42
43
# File 'lib/coradoc/core_model/metadata.rb', line 35

def []=(key, value)
  self.entries ||= []
  existing = find_entry(key)
  if existing
    existing.value = value
  else
    entries << MetadataEntry.new(key: key, value: value)
  end
end

#key?(key) ⇒ Boolean

Check if a key exists

Parameters:

  • key (String)

    The key to check

Returns:

  • (Boolean)

    True if key exists



48
49
50
51
52
# File 'lib/coradoc/core_model/metadata.rb', line 48

def key?(key)
  return false if entries.nil?

  !find_entry(key).nil?
end

#keysArray<String>

Get all keys

Returns:

  • (Array<String>)

    List of keys



56
57
58
59
60
# File 'lib/coradoc/core_model/metadata.rb', line 56

def keys
  return [] if entries.nil?

  entries.map(&:key)
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)

    Hash of all metadata entries



64
65
66
67
68
# File 'lib/coradoc/core_model/metadata.rb', line 64

def to_h
  return {} if entries.nil?

  entries.each_with_object({}) { |entry, hash| hash[entry.key] = entry.value }
end