Class: AgentCode::Blueprint::BlueprintParser
- Inherits:
-
Object
- Object
- AgentCode::Blueprint::BlueprintParser
- 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
-
#compute_file_hash(file_path) ⇒ String
Compute SHA-256 hash of a file's contents.
- #normalize_columns(columns) ⇒ Object
- #normalize_field_list(value) ⇒ Object
-
#normalize_options(options) ⇒ Object
────────────────────────────────────────────── Normalization helpers ──────────────────────────────────────────────.
- #normalize_permissions(permissions) ⇒ Object
-
#parse_model(file_path) ⇒ Hash
Parse a model blueprint YAML file into normalized structure.
-
#parse_roles(file_path) ⇒ Hash<String, Hash>
Parse _roles.yaml file into normalized role definitions.
Instance Method Details
#compute_file_hash(file_path) ⇒ String
Compute SHA-256 hash of a file's contents.
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 () { belongs_to_organization: .fetch("belongs_to_organization", false), soft_deletes: .fetch("soft_deletes", true), audit_trail: .fetch("audit_trail", false), owner: .fetch("owner", nil), except_actions: .fetch("except_actions", []), pagination: .fetch("pagination", false), per_page: .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 () result = {} .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.
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: (parsed["options"] || {}), columns: normalize_columns(parsed["columns"] || {}), relationships: parsed["relationships"] || [], permissions: (parsed["permissions"] || {}), source_file: source_file } end |
#parse_roles(file_path) ⇒ Hash<String, Hash>
Parse _roles.yaml file into normalized role definitions.
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 |