Class: Ast::Merge::Recipe::Preset
- Inherits:
-
Object
- Object
- Ast::Merge::Recipe::Preset
- Defined in:
- lib/ast/merge/recipe/preset.rb
Overview
Base configuration for merge presets.
A Preset provides merge configuration (signature generators, node typing, preferences) without requiring a template file. This is useful for defining reusable merge behaviors that can be applied to any merge operation.
Config inherits from Preset and adds template/target file handling
for standalone recipe execution.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#description ⇒ String?
readonly
Preset description.
-
#freeze_token ⇒ String?
readonly
Freeze token for preserving sections.
-
#merge_config ⇒ Hash
readonly
Merge configuration.
-
#name ⇒ String
readonly
Preset name.
-
#parser ⇒ Symbol
readonly
Parser to use (:prism, :markly, :psych, etc.).
-
#parser_explicit ⇒ Boolean
readonly
Whether the parser was explicitly configured in YAML/hash input.
-
#preset_path ⇒ String?
readonly
Path to the preset file (for script resolution).
Class Method Summary collapse
-
.load(path) ⇒ Preset
Load a preset from a YAML file.
Instance Method Summary collapse
-
#add_missing ⇒ Boolean, Proc
Get the add_missing setting, loading as callable if it's a script reference.
-
#add_missing? ⇒ Boolean, Proc
Convenience alias for boolean check.
-
#initialize(config, preset_path: nil) ⇒ Preset
constructor
Initialize a preset from a hash.
-
#match_refiner ⇒ Object?
Get the match_refiner callable, loading from script if needed.
-
#node_typing ⇒ Hash?
Get the node_typing configuration with callables loaded.
-
#normalize_whitespace ⇒ Boolean
Get the normalize_whitespace setting.
-
#parser_explicit? ⇒ Boolean
Whether the parser was explicitly configured.
-
#preference ⇒ Symbol, Hash
Get the merge preference setting.
-
#rehydrate_link_references ⇒ Boolean
Get the rehydrate_link_references setting.
-
#script_loader ⇒ ScriptLoader
Get the script loader instance.
-
#signature_generator ⇒ Proc?
Get the signature_generator callable, loading from script if needed.
-
#to_h ⇒ Hash
Convert preset to a hash suitable for SmartMerger options.
Constructor Details
#initialize(config, preset_path: nil) ⇒ Preset
Initialize a preset from a hash.
72 73 74 75 76 77 78 79 80 |
# File 'lib/ast/merge/recipe/preset.rb', line 72 def initialize(config, preset_path: nil) @preset_path = preset_path @name = config['name'] || 'unnamed' @description = config['description'] @parser_explicit = config.key?('parser') || config.key?(:parser) @parser = (config['parser'] || config[:parser] || 'prism').to_sym @merge_config = parse_merge_config(config['merge'] || {}) @freeze_token = config['freeze_token'] end |
Instance Attribute Details
#description ⇒ String? (readonly)
Returns Preset description.
37 38 39 |
# File 'lib/ast/merge/recipe/preset.rb', line 37 def description @description end |
#freeze_token ⇒ String? (readonly)
Returns Freeze token for preserving sections.
46 47 48 |
# File 'lib/ast/merge/recipe/preset.rb', line 46 def freeze_token @freeze_token end |
#merge_config ⇒ Hash (readonly)
Returns Merge configuration.
43 44 45 |
# File 'lib/ast/merge/recipe/preset.rb', line 43 def merge_config @merge_config end |
#name ⇒ String (readonly)
Returns Preset name.
34 35 36 |
# File 'lib/ast/merge/recipe/preset.rb', line 34 def name @name end |
#parser ⇒ Symbol (readonly)
Returns Parser to use (:prism, :markly, :psych, etc.).
40 41 42 |
# File 'lib/ast/merge/recipe/preset.rb', line 40 def parser @parser end |
#parser_explicit ⇒ Boolean (readonly)
Returns Whether the parser was explicitly configured in YAML/hash input.
52 53 54 |
# File 'lib/ast/merge/recipe/preset.rb', line 52 def parser_explicit @parser_explicit end |
#preset_path ⇒ String? (readonly)
Returns Path to the preset file (for script resolution).
49 50 51 |
# File 'lib/ast/merge/recipe/preset.rb', line 49 def preset_path @preset_path end |
Class Method Details
.load(path) ⇒ Preset
Load a preset from a YAML file.
60 61 62 63 64 65 |
# File 'lib/ast/merge/recipe/preset.rb', line 60 def load(path) raise ArgumentError, "Preset 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
#add_missing ⇒ Boolean, Proc
Get the add_missing setting, loading as callable if it's a script reference.
103 104 105 106 107 108 109 110 111 |
# File 'lib/ast/merge/recipe/preset.rb', line 103 def add_missing value = merge_config[:add_missing] return true if value.nil? return value if [true, false].include?(value) return value if value.respond_to?(:call) # It's a script reference - load it script_loader.load_callable(value) end |
#add_missing? ⇒ Boolean, Proc
Convenience alias for boolean check.
116 117 118 |
# File 'lib/ast/merge/recipe/preset.rb', line 116 def add_missing? add_missing end |
#match_refiner ⇒ Object?
Get the match_refiner callable, loading from script if needed.
145 146 147 148 149 150 151 |
# File 'lib/ast/merge/recipe/preset.rb', line 145 def match_refiner value = merge_config[:match_refiner] return if value.nil? return value if value.respond_to?(:call) || value.is_a?(Ast::Merge::MatchRefinerBase) script_loader.load_callable(value) end |
#node_typing ⇒ Hash?
Get the node_typing configuration with callables loaded.
134 135 136 137 138 139 140 |
# File 'lib/ast/merge/recipe/preset.rb', line 134 def node_typing value = merge_config[:node_typing] return if value.nil? return value if value.is_a?(Hash) && value.values.all? { |v| v.respond_to?(:call) } script_loader.load_callable_hash(value) end |
#normalize_whitespace ⇒ Boolean
Get the normalize_whitespace setting.
This is a format-specific post-processing option forwarded to mergers that explicitly support it (currently markdown-family recipe flows).
159 160 161 |
# File 'lib/ast/merge/recipe/preset.rb', line 159 def normalize_whitespace merge_config[:normalize_whitespace] == true end |
#parser_explicit? ⇒ Boolean
Whether the parser was explicitly configured.
This lets callers distinguish between the recipe model's internal
default (:prism) and a parser choice the recipe author intended the
stock runner to honor automatically.
89 90 91 |
# File 'lib/ast/merge/recipe/preset.rb', line 89 def parser_explicit? parser_explicit == true end |
#preference ⇒ Symbol, Hash
Get the merge preference setting.
96 97 98 |
# File 'lib/ast/merge/recipe/preset.rb', line 96 def preference merge_config[:preference] || :template end |
#rehydrate_link_references ⇒ Boolean
Get the rehydrate_link_references setting.
166 167 168 |
# File 'lib/ast/merge/recipe/preset.rb', line 166 def rehydrate_link_references merge_config[:rehydrate_link_references] == true end |
#script_loader ⇒ ScriptLoader
Get the script loader instance.
189 190 191 |
# File 'lib/ast/merge/recipe/preset.rb', line 189 def script_loader @script_loader ||= ScriptLoader.new(recipe_path: preset_path) end |
#signature_generator ⇒ Proc?
Get the signature_generator callable, loading from script if needed.
123 124 125 126 127 128 129 |
# File 'lib/ast/merge/recipe/preset.rb', line 123 def signature_generator value = merge_config[:signature_generator] return if value.nil? return value if value.respond_to?(:call) script_loader.load_callable(value) end |
#to_h ⇒ Hash
Convert preset to a hash suitable for SmartMerger options.
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/ast/merge/recipe/preset.rb', line 173 def to_h { preference: preference, add_template_only_nodes: add_missing, signature_generator: signature_generator, node_typing: node_typing, match_refiner: match_refiner, freeze_token: freeze_token, normalize_whitespace: normalize_whitespace, rehydrate_link_references: rehydrate_link_references }.compact end |