Module: Opdotenv::SourceParser

Defined in:
lib/opdotenv/source_parser.rb

Overview

Parser for simplified source strings Infers field_name and field_type from the path pattern

Constant Summary collapse

NOTES_PLAIN_FIELD =
"notesPlain"

Class Method Summary collapse

Class Method Details

.build_parsed_source(path, item_name, field_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/opdotenv/source_parser.rb', line 29

def build_parsed_source(path, item_name, field_name)
  if field_name
    field_type = FormatInferrer.infer_from_name(field_name)
    {path: path, field_name: field_name, field_type: field_type}
  elsif (field_type = FormatInferrer.infer_from_name(item_name))
    {path: path, field_name: NOTES_PLAIN_FIELD, field_type: field_type}
  else
    # All other items: load all fields without parsing
    {path: path, field_name: nil, field_type: nil}
  end
end

.normalize_hash(source) ⇒ Hash

Normalize hash format for backward compatibility

Parameters:

  • source (Hash)

    Source hash with :path, :field_name, :field_type keys

Returns:

  • (Hash)

    Normalized source



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/opdotenv/source_parser.rb', line 55

def normalize_hash(source)
  {
    path: source[:path] || source["path"],
    field_name: source[:field_name] || source["field_name"],
    field_type: (source[:field_type] || source["field_type"])&.to_sym,
    overwrite: if source.key?(:overwrite)
                 source[:overwrite]
               else
                 (source.key?("overwrite") ? source["overwrite"] : nil)
               end
  }
end

.parse(source) ⇒ Hash

Parse a source string or hash into normalized format

Parameters:

  • source (String, Hash)

    Source string (e.g., "op://Vault/Item") or hash for backward compatibility

Returns:

  • (Hash)

    Normalized source with :path, :field_name, and :field_type keys

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/opdotenv/source_parser.rb', line 13

def parse(source)
  # Handle hash format for backward compatibility
  return normalize_hash(source) if source.is_a?(Hash)

  # Handle string format
  raise ArgumentError, "Source must start with 'op://'" unless source.to_s.start_with?("op://")

  path = source.to_s
  _, item = parse_op_path(path)
  item_parts = item.split("/")
  item_name = item_parts.first
  field_name = (item_parts.length > 1) ? item_parts[1] : nil

  build_parsed_source(path, item_name, field_name)
end

.parse_op_path(path) ⇒ Array<String>

Parse op:// path to extract vault and item

Parameters:

  • path (String)

    Path like "op://Vault/Item" or "op://Vault/Item/Field"

Returns:

  • (Array<String>)

    [vault, item] tuple

Raises:

  • (ArgumentError)


45
46
47
48
49
# File 'lib/opdotenv/source_parser.rb', line 45

def parse_op_path(path)
  m = path.match(/\Aop:\/\/([^\/]+)\/(.+)/)
  raise ArgumentError, "Invalid op path: #{path}" unless m
  [m[1], m[2]]
end