Class: Ace::Support::Markdown::Molecules::FrontmatterEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/markdown/molecules/frontmatter_editor.rb

Overview

Atomic frontmatter field updates for markdown documents Handles nested keys, special values, and immutable transformations

Class Method Summary collapse

Class Method Details

.apply_nested_update(frontmatter, key_path, value) ⇒ Object

Apply update to nested key path



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ace/support/markdown/molecules/frontmatter_editor.rb', line 77

def self.apply_nested_update(frontmatter, key_path, value)
  parts = key_path.split(".")
  target = frontmatter

  # Navigate to the target hash
  parts[0...-1].each do |part|
    target[part] ||= {}
    target = target[part]
  end

  # Set the final value
  target[parts.last] = process_value(value)
end

.apply_updates(frontmatter, updates) ⇒ Object

Apply updates to frontmatter hash (mutable operation on copy)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ace/support/markdown/molecules/frontmatter_editor.rb', line 61

def self.apply_updates(frontmatter, updates)
  updates.each do |key, value|
    key_str = key.to_s

    # Handle nested keys with dots (e.g., "update.last-updated")
    if key_str.include?(".")
      apply_nested_update(frontmatter, key_str, value)
    else
      frontmatter[key_str] = process_value(value)
    end
  end

  frontmatter
end

.delete_field(document, key) ⇒ MarkdownDocument

Delete a field from frontmatter

Parameters:

  • document (MarkdownDocument)

    The document to update

  • key (String)

    The field key to delete

Returns:

  • (MarkdownDocument)

    New document with field removed



38
39
40
41
42
43
44
# File 'lib/ace/support/markdown/molecules/frontmatter_editor.rb', line 38

def self.delete_field(document, key)
  updated_frontmatter = document.frontmatter.dup
  updated_frontmatter.delete(key)
  updated_frontmatter.delete(key.to_sym) if key.is_a?(String)

  document.with_frontmatter(updated_frontmatter)
end

.merge_updates(document, updates_list) ⇒ MarkdownDocument

Merge multiple updates atomically

Parameters:

  • document (MarkdownDocument)

    The document to update

  • updates_list (Array<Hash>)

    List of update hashes

Returns:

  • (MarkdownDocument)

    New document with all updates applied

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
# File 'lib/ace/support/markdown/molecules/frontmatter_editor.rb', line 50

def self.merge_updates(document, updates_list)
  raise ArgumentError, "Updates list must be an array" unless updates_list.is_a?(Array)

  updates_list.reduce(document) do |doc, updates|
    update(doc, updates)
  end
end

.process_value(value) ⇒ Object

Process special values before setting



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ace/support/markdown/molecules/frontmatter_editor.rb', line 92

def self.process_value(value)
  case value
  when "today"
    Date.today.strftime("%Y-%m-%d")
  when "now"
    Time.now.strftime("%Y-%m-%d %H:%M:%S")
  when /^\d{4}-\d{2}-\d{2}$/
    value # Already a date string
  else
    value
  end
end

.update(document, updates) ⇒ MarkdownDocument

Update frontmatter fields in a document

Parameters:

  • document (MarkdownDocument)

    The document to update

  • updates (Hash)

    Fields to update (supports nested keys with dots)

Returns:

  • (MarkdownDocument)

    New document with updated frontmatter

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
# File 'lib/ace/support/markdown/molecules/frontmatter_editor.rb', line 16

def self.update(document, updates)
  raise ArgumentError, "Document must be a MarkdownDocument" unless document.is_a?(Models::MarkdownDocument)
  raise ArgumentError, "Updates must be a hash" unless updates.is_a?(Hash)

  updated_frontmatter = apply_updates(document.frontmatter.dup, updates)

  document.with_frontmatter(updated_frontmatter)
end

.update_field(document, key, value) ⇒ MarkdownDocument

Update a single field

Parameters:

  • document (MarkdownDocument)

    The document to update

  • key (String)

    The field key (supports dots for nesting)

  • value (Object)

    The new value

Returns:

  • (MarkdownDocument)

    New document with updated field



30
31
32
# File 'lib/ace/support/markdown/molecules/frontmatter_editor.rb', line 30

def self.update_field(document, key, value)
  update(document, {key => value})
end