Class: Ast::Merge::Recipe::Config
- Defined in:
- lib/ast/merge/recipe/config.rb
Overview
Loads and represents a merge recipe from YAML configuration.
A recipe extends Preset with:
- Template file specification
- Target file patterns
- Injection point configuration
- when_missing behavior
Instance Attribute Summary collapse
-
#injection ⇒ Hash
readonly
Injection point configuration.
-
#targets ⇒ Array<String>
readonly
Glob patterns for target files.
-
#template_path ⇒ String
readonly
Path to template file (relative to recipe or absolute).
-
#when_missing ⇒ Symbol
readonly
Behavior when injection anchor not found (:skip, :add, :error).
Attributes inherited from Preset
#description, #freeze_token, #merge_config, #name, #parser, #preset_path
Class Method Summary collapse
-
.load(path) ⇒ Config
Load a recipe from a YAML file.
Instance Method Summary collapse
-
#expand_targets(base_dir: nil) ⇒ Array<String>
Expand target globs to actual file paths.
-
#finder_query ⇒ Hash
Build an InjectionPointFinder query from the injection config.
-
#initialize(config, preset_path: nil, recipe_path: nil) ⇒ Config
constructor
Create a recipe from a hash (parsed YAML or programmatic).
-
#recipe_path ⇒ Object
Alias for compatibility - recipe_path points to the same file as preset_path.
-
#replace_mode? ⇒ Boolean
Whether to use replace mode (template replaces section entirely).
-
#template_absolute_path(base_dir: nil) ⇒ String
Get the absolute path to the template file.
Methods inherited from Preset
#add_missing, #add_missing?, #match_refiner, #node_typing, #normalize_whitespace, #preference, #rehydrate_link_references, #script_loader, #signature_generator, #to_h
Constructor Details
#initialize(config, preset_path: nil, recipe_path: nil) ⇒ Config
Create a recipe from a hash (parsed YAML or programmatic).
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ast/merge/recipe/config.rb', line 87 def initialize(config, preset_path: nil, recipe_path: nil) # Support both preset_path and recipe_path for backward compatibility effective_path = preset_path || recipe_path super(config, preset_path: effective_path) @template_path = config["template"] || raise(ArgumentError, "Recipe must have 'template' key") @targets = Array(config["targets"] || ["*.md"]) @injection = parse_injection(config["injection"] || {}) @when_missing = (config["when_missing"] || "skip").to_sym end |
Instance Attribute Details
#injection ⇒ Hash (readonly)
Returns Injection point configuration.
58 59 60 |
# File 'lib/ast/merge/recipe/config.rb', line 58 def injection @injection end |
#targets ⇒ Array<String> (readonly)
Returns Glob patterns for target files.
55 56 57 |
# File 'lib/ast/merge/recipe/config.rb', line 55 def targets @targets end |
#template_path ⇒ String (readonly)
Returns Path to template file (relative to recipe or absolute).
52 53 54 |
# File 'lib/ast/merge/recipe/config.rb', line 52 def template_path @template_path end |
#when_missing ⇒ Symbol (readonly)
Returns Behavior when injection anchor not found (:skip, :add, :error).
61 62 63 |
# File 'lib/ast/merge/recipe/config.rb', line 61 def when_missing @when_missing end |
Class Method Details
.load(path) ⇒ Config
Load a recipe from a YAML file.
74 75 76 77 78 79 |
# File 'lib/ast/merge/recipe/config.rb', line 74 def load(path) raise ArgumentError, "Recipe file not found: #{path}" unless File.exist?(path) yaml = YAML.safe_load_file(path, permitted_classes: [Regexp, Symbol]) new(yaml, preset_path: path) end |
Instance Method Details
#expand_targets(base_dir: nil) ⇒ Array<String>
Expand target globs to actual file paths.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ast/merge/recipe/config.rb', line 113 def (base_dir: nil) base = base_dir || (preset_path ? File.dirname(preset_path) : Dir.pwd) targets.flat_map do |pattern| if File.absolute_path?(pattern) Dir.glob(pattern) else # Expand and normalize to remove .. segments = File.(pattern, base) Dir.glob() end end.uniq.sort end |
#finder_query ⇒ Hash
Build an InjectionPointFinder query from the injection config.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ast/merge/recipe/config.rb', line 130 def finder_query anchor = injection[:anchor] || {} boundary = injection[:boundary] || {} query = { type: anchor[:type], text: anchor[:text], position: injection[:position] || :replace, boundary_type: boundary[:type], boundary_text: boundary[:text], } # Support tree-depth based boundary detection # same_or_shallower: true means "end at next sibling (same tree level or above)" if boundary[:same_or_shallower] query[:boundary_same_or_shallower] = true end query.compact end |
#recipe_path ⇒ Object
Alias for compatibility - recipe_path points to the same file as preset_path
64 65 66 |
# File 'lib/ast/merge/recipe/config.rb', line 64 def recipe_path preset_path end |
#replace_mode? ⇒ Boolean
Whether to use replace mode (template replaces section entirely).
154 155 156 |
# File 'lib/ast/merge/recipe/config.rb', line 154 def replace_mode? merge_config[:replace_mode] == true end |
#template_absolute_path(base_dir: nil) ⇒ String
Get the absolute path to the template file.
102 103 104 105 106 107 |
# File 'lib/ast/merge/recipe/config.rb', line 102 def template_absolute_path(base_dir: nil) return @template_path if File.absolute_path?(@template_path) base = base_dir || (preset_path ? File.dirname(preset_path) : Dir.pwd) File.(@template_path, base) end |