Class: Ast::Merge::Recipe::Preset

Inherits:
Object
  • Object
show all
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.

Examples:

Loading a preset

preset = Preset.load("presets/gemfile.yml")
merger = Prism::Merge::SmartMerger.new(
  template, destination,
  **preset.to_h
)

Creating a preset programmatically

preset = Preset.new(
  "name" => "my_preset",
  "merge" => { "preference" => "template" }
)

See Also:

Direct Known Subclasses

Config

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, preset_path: nil) ⇒ Preset

Initialize a preset from a hash.

Parameters:

  • config (Hash)

    Parsed YAML config or programmatic config

  • preset_path (String, nil) (defaults to: nil)

    Path to preset file (for script resolution)



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

#descriptionString? (readonly)

Returns Preset description.

Returns:

  • (String, nil)

    Preset description



37
38
39
# File 'lib/ast/merge/recipe/preset.rb', line 37

def description
  @description
end

#freeze_tokenString? (readonly)

Returns Freeze token for preserving sections.

Returns:

  • (String, nil)

    Freeze token for preserving sections



46
47
48
# File 'lib/ast/merge/recipe/preset.rb', line 46

def freeze_token
  @freeze_token
end

#merge_configHash (readonly)

Returns Merge configuration.

Returns:

  • (Hash)

    Merge configuration



43
44
45
# File 'lib/ast/merge/recipe/preset.rb', line 43

def merge_config
  @merge_config
end

#nameString (readonly)

Returns Preset name.

Returns:

  • (String)

    Preset name



34
35
36
# File 'lib/ast/merge/recipe/preset.rb', line 34

def name
  @name
end

#parserSymbol (readonly)

Returns Parser to use (:prism, :markly, :psych, etc.).

Returns:

  • (Symbol)

    Parser to use (:prism, :markly, :psych, etc.)



40
41
42
# File 'lib/ast/merge/recipe/preset.rb', line 40

def parser
  @parser
end

#preset_pathString? (readonly)

Returns Path to the preset file (for script resolution).

Returns:

  • (String, nil)

    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.

Parameters:

  • path (String)

    Path to the preset YAML file

Returns:

Raises:

  • (ArgumentError)

    If file not found



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_missingBoolean, Proc

Get the add_missing setting, loading as callable if it’s a script reference.

Returns:

  • (Boolean, Proc)

    Boolean value or callable filter



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.

Returns:

  • (Boolean, Proc)


101
102
103
# File 'lib/ast/merge/recipe/preset.rb', line 101

def add_missing?
  add_missing
end

#match_refinerObject?

Get the match_refiner callable, loading from script if needed.

Returns:

  • (Object, nil)

    Match refiner instance or callable



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_typingHash?

Get the node_typing configuration with callables loaded.

Returns:

  • (Hash, nil)

    Hash of type => callable



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_whitespaceBoolean

Get the normalize_whitespace setting.

Returns:

  • (Boolean)

    Whether to collapse excessive blank lines



141
142
143
# File 'lib/ast/merge/recipe/preset.rb', line 141

def normalize_whitespace
  merge_config[:normalize_whitespace] == true
end

#preferenceSymbol, Hash

Get the merge preference setting.

Returns:

  • (Symbol, Hash)

    Preference (:template, :destination, or per-type hash)



81
82
83
# File 'lib/ast/merge/recipe/preset.rb', line 81

def preference
  merge_config[:preference] || :template
end

Get the rehydrate_link_references setting.

Returns:

  • (Boolean)

    Whether to convert inline links to reference style



148
149
150
# File 'lib/ast/merge/recipe/preset.rb', line 148

def rehydrate_link_references
  merge_config[:rehydrate_link_references] == true
end

#script_loaderScriptLoader

Get the script loader instance.

Returns:



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_generatorProc?

Get the signature_generator callable, loading from script if needed.

Returns:

  • (Proc, nil)

    Signature generator callable



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_hHash

Convert preset to a hash suitable for SmartMerger options.

Returns:

  • (Hash)


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