Class: AgentCode::Blueprint::BlueprintParser

Inherits:
Object
  • Object
show all
Defined in:
lib/agentcode/blueprint/blueprint_parser.rb

Overview

Parses YAML blueprint files into normalized data structures. Port of agentcode-server BlueprintParser.php / agentcode-adonis-server blueprint_parser.ts.

Instance Method Summary collapse

Instance Method Details

#compute_file_hash(file_path) ⇒ String

Compute SHA-256 hash of a file's contents.

Parameters:

  • file_path (String)

Returns:

  • (String)

    64-char hex hash



83
84
85
# File 'lib/agentcode/blueprint/blueprint_parser.rb', line 83

def compute_file_hash(file_path)
  Digest::SHA256.hexdigest(File.read(file_path))
end

#normalize_columns(columns) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/agentcode/blueprint/blueprint_parser.rb', line 103

def normalize_columns(columns)
  result = []

  columns.each do |name, value|
    if value.is_a?(String)
      # Short syntax: field_name: type
      result << {
        name: name,
        type: value,
        nullable: false,
        unique: false,
        index: false,
        default: nil,
        filterable: false,
        sortable: false,
        searchable: false,
        precision: nil,
        scale: nil,
        foreign_model: nil
      }
    elsif value.is_a?(Hash)
      result << {
        name: name,
        type: value.fetch("type", "string"),
        nullable: value.fetch("nullable", false),
        unique: value.fetch("unique", false),
        index: value.fetch("index", false),
        default: value.fetch("default", nil),
        filterable: value.fetch("filterable", false),
        sortable: value.fetch("sortable", false),
        searchable: value.fetch("searchable", false),
        precision: value.fetch("precision", nil),
        scale: value.fetch("scale", nil),
        foreign_model: value.fetch("foreign_model", nil)
      }
    end
  end

  result
end

#normalize_field_list(value) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/agentcode/blueprint/blueprint_parser.rb', line 162

def normalize_field_list(value)
  return [] if value.nil?
  return ["*"] if value == "*"
  return [value] if value.is_a?(String)
  return value if value.is_a?(Array)

  []
end

#normalize_options(options) ⇒ Object

────────────────────────────────────────────── Normalization helpers ──────────────────────────────────────────────



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/agentcode/blueprint/blueprint_parser.rb', line 91

def normalize_options(options)
  {
    belongs_to_organization: options.fetch("belongs_to_organization", false),
    soft_deletes: options.fetch("soft_deletes", true),
    audit_trail: options.fetch("audit_trail", false),
    owner: options.fetch("owner", nil),
    except_actions: options.fetch("except_actions", []),
    pagination: options.fetch("pagination", false),
    per_page: options.fetch("per_page", 25)
  }
end

#normalize_permissions(permissions) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/agentcode/blueprint/blueprint_parser.rb', line 144

def normalize_permissions(permissions)
  result = {}

  permissions.each do |role, value|
    next unless value.is_a?(Hash)

    result[role] = {
      actions: value["actions"] || [],
      show_fields: normalize_field_list(value["show_fields"]),
      create_fields: normalize_field_list(value["create_fields"]),
      update_fields: normalize_field_list(value["update_fields"]),
      hidden_fields: normalize_field_list(value["hidden_fields"])
    }
  end

  result
end

#parse_model(file_path) ⇒ Hash

Parse a model blueprint YAML file into normalized structure.

Parameters:

  • file_path (String)

Returns:

  • (Hash)

    ParsedBlueprint hash



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/agentcode/blueprint/blueprint_parser.rb', line 47

def parse_model(file_path)
  content = read_file(file_path)

  raise "Blueprint file is empty: #{file_path}" if content.strip.empty?

  parsed = begin
    YAML.safe_load(content, permitted_classes: [Symbol])
  rescue Psych::SyntaxError
    raise "Invalid YAML syntax in: #{file_path}"
  end

  raise "Invalid YAML structure in: #{file_path}" unless parsed.is_a?(Hash)
  raise "Missing 'model' key in: #{file_path}" unless parsed["model"]

  model_name = parsed["model"]
  slug = parsed["slug"] || model_to_slug(model_name)
  table = parsed["table"] || slug

  source_file = File.basename(file_path)

  {
    model: model_name,
    slug: slug,
    table: table,
    options: normalize_options(parsed["options"] || {}),
    columns: normalize_columns(parsed["columns"] || {}),
    relationships: parsed["relationships"] || [],
    permissions: normalize_permissions(parsed["permissions"] || {}),
    source_file: source_file
  }
end

#parse_roles(file_path) ⇒ Hash<String, Hash>

Parse _roles.yaml file into normalized role definitions.

Parameters:

  • file_path (String)

Returns:

  • (Hash<String, Hash>)

    e.g. { 'owner' => { name: 'Owner', description: '...' } }



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/agentcode/blueprint/blueprint_parser.rb', line 15

def parse_roles(file_path)
  content = read_file(file_path)

  raise "Blueprint roles file is empty: #{file_path}" if content.strip.empty?

  parsed = begin
    YAML.safe_load(content, permitted_classes: [Symbol])
  rescue Psych::SyntaxError
    raise "Invalid YAML syntax in: #{file_path}"
  end

  raise "Invalid YAML structure in: #{file_path}" unless parsed.is_a?(Hash)
  raise "Missing 'roles' key in: #{file_path}" unless parsed["roles"]

  roles = {}

  parsed["roles"].each do |slug, value|
    raise "Invalid role definition for '#{slug}' — expected a hash" unless value.is_a?(Hash)

    roles[slug] = {
      name: value["name"] || slug_to_name(slug),
      description: value["description"] || ""
    }
  end

  roles
end