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.).
-
#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.
-
#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.
69 70 71 72 73 74 75 76 |
# File 'lib/ast/merge/recipe/preset.rb', line 69 def initialize(config, preset_path: nil) @preset_path = preset_path @name = config["name"] || "unnamed" @description = config["description"] @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 |
#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.
57 58 59 60 61 62 |
# File 'lib/ast/merge/recipe/preset.rb', line 57 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.
88 89 90 91 92 93 94 95 96 |
# File 'lib/ast/merge/recipe/preset.rb', line 88 def add_missing value = merge_config[:add_missing] return true if value.nil? return value if value == true || value == false 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.
101 102 103 |
# File 'lib/ast/merge/recipe/preset.rb', line 101 def add_missing? add_missing end |
#match_refiner ⇒ Object?
Get the match_refiner callable, loading from script if needed.
130 131 132 133 134 135 136 |
# File 'lib/ast/merge/recipe/preset.rb', line 130 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.
119 120 121 122 123 124 125 |
# File 'lib/ast/merge/recipe/preset.rb', line 119 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.
141 142 143 |
# File 'lib/ast/merge/recipe/preset.rb', line 141 def normalize_whitespace merge_config[:normalize_whitespace] == true end |
#preference ⇒ Symbol, Hash
Get the merge preference setting.
81 82 83 |
# File 'lib/ast/merge/recipe/preset.rb', line 81 def preference merge_config[:preference] || :template end |
#rehydrate_link_references ⇒ Boolean
Get the rehydrate_link_references setting.
148 149 150 |
# File 'lib/ast/merge/recipe/preset.rb', line 148 def rehydrate_link_references merge_config[:rehydrate_link_references] == true end |
#script_loader ⇒ ScriptLoader
Get the script loader instance.
171 172 173 |
# File 'lib/ast/merge/recipe/preset.rb', line 171 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.
108 109 110 111 112 113 114 |
# File 'lib/ast/merge/recipe/preset.rb', line 108 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.
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ast/merge/recipe/preset.rb', line 155 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 |