Class: Ast::Merge::MergerConfig
- Inherits:
-
Object
- Object
- Ast::Merge::MergerConfig
- Defined in:
- lib/ast/merge/merger_config.rb
Overview
Configuration object for SmartMerger options.
This class encapsulates common configuration options used across all *-merge gem SmartMerger implementations. It provides a standardized interface for merge configuration and validates option values.
Constant Summary collapse
- VALID_PREFERENCES =
Valid values for preference (when using Symbol)
%i[destination template].freeze
Instance Attribute Summary collapse
-
#add_template_only_nodes ⇒ Boolean
readonly
Whether to add nodes that only exist in template - false (default) - Skip template-only nodes - true - Add template-only nodes to result.
-
#freeze_token ⇒ String
readonly
Token used for freeze block markers.
-
#node_typing ⇒ Hash{Symbol,String => #call}?
readonly
Node typing configuration.
-
#preference ⇒ Symbol, Hash
readonly
Which version to prefer when nodes have matching signatures.
-
#signature_generator ⇒ Proc?
readonly
Custom signature generator proc.
Class Method Summary collapse
-
.destination_wins(freeze_token: nil, signature_generator: nil, node_typing: nil) ⇒ MergerConfig
Create a config preset for “destination wins” merging.
-
.template_wins(freeze_token: nil, signature_generator: nil, node_typing: nil) ⇒ MergerConfig
Create a config preset for “template wins” merging.
Instance Method Summary collapse
-
#initialize(preference: :destination, add_template_only_nodes: false, freeze_token: nil, signature_generator: nil, node_typing: nil) ⇒ MergerConfig
constructor
Initialize a new MergerConfig.
-
#per_type_preference? ⇒ Boolean
Check if Hash-based per-type preferences are configured.
-
#prefer_destination? ⇒ Boolean
Check if destination version should be preferred on signature match.
-
#prefer_template? ⇒ Boolean
Check if template version should be preferred on signature match.
-
#preference_for(type) ⇒ Symbol
Get the preference for a specific node type or merge_type.
-
#to_h(default_freeze_token: nil) ⇒ Hash
Convert config to a hash suitable for passing to SmartMerger.
-
#with(**options) ⇒ MergerConfig
Create a new config with updated values.
Constructor Details
#initialize(preference: :destination, add_template_only_nodes: false, freeze_token: nil, signature_generator: nil, node_typing: nil) ⇒ MergerConfig
Initialize a new MergerConfig.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ast/merge/merger_config.rb', line 90 def initialize( preference: :destination, add_template_only_nodes: false, freeze_token: nil, signature_generator: nil, node_typing: nil ) validate_preference!(preference) NodeTyping.validate!(node_typing) if node_typing @preference = preference @add_template_only_nodes = add_template_only_nodes @freeze_token = freeze_token @signature_generator = signature_generator @node_typing = node_typing end |
Instance Attribute Details
#add_template_only_nodes ⇒ Boolean (readonly)
Returns Whether to add nodes that only exist in template
-
false (default) - Skip template-only nodes
-
true - Add template-only nodes to result.
64 65 66 |
# File 'lib/ast/merge/merger_config.rb', line 64 def add_template_only_nodes @add_template_only_nodes end |
#freeze_token ⇒ String (readonly)
Returns Token used for freeze block markers.
67 68 69 |
# File 'lib/ast/merge/merger_config.rb', line 67 def freeze_token @freeze_token end |
#node_typing ⇒ Hash{Symbol,String => #call}? (readonly)
Returns Node typing configuration. Maps node type names to callable objects that can transform nodes and optionally add merge_type attributes for per-node-type preferences.
75 76 77 |
# File 'lib/ast/merge/merger_config.rb', line 75 def node_typing @node_typing end |
#preference ⇒ Symbol, Hash (readonly)
Returns Which version to prefer when nodes have matching signatures. As Symbol:
-
:destination (default) - Keep destination version (preserves customizations)
-
:template - Use template version (applies updates)
As Hash:
-
Keys are node types (Symbol) or merge_types from node_typing
-
Values are :destination or :template
-
Use :default key for fallback preference
@example { default: :destination, lint_gem: :template, config_call: :template }.
59 60 61 |
# File 'lib/ast/merge/merger_config.rb', line 59 def preference @preference end |
#signature_generator ⇒ Proc? (readonly)
Returns Custom signature generator proc.
70 71 72 |
# File 'lib/ast/merge/merger_config.rb', line 70 def signature_generator @signature_generator end |
Class Method Details
.destination_wins(freeze_token: nil, signature_generator: nil, node_typing: nil) ⇒ MergerConfig
Create a config preset for “destination wins” merging. Destination customizations are preserved, template-only content is skipped.
203 204 205 206 207 208 209 210 211 |
# File 'lib/ast/merge/merger_config.rb', line 203 def self.destination_wins(freeze_token: nil, signature_generator: nil, node_typing: nil) new( preference: :destination, add_template_only_nodes: false, freeze_token: freeze_token, signature_generator: signature_generator, node_typing: node_typing, ) end |
.template_wins(freeze_token: nil, signature_generator: nil, node_typing: nil) ⇒ MergerConfig
Create a config preset for “template wins” merging. Template updates are applied, template-only content is added.
220 221 222 223 224 225 226 227 228 |
# File 'lib/ast/merge/merger_config.rb', line 220 def self.template_wins(freeze_token: nil, signature_generator: nil, node_typing: nil) new( preference: :template, add_template_only_nodes: true, freeze_token: freeze_token, signature_generator: signature_generator, node_typing: node_typing, ) end |
Instance Method Details
#per_type_preference? ⇒ Boolean
Check if Hash-based per-type preferences are configured.
162 163 164 |
# File 'lib/ast/merge/merger_config.rb', line 162 def per_type_preference? @preference.is_a?(Hash) end |
#prefer_destination? ⇒ Boolean
Check if destination version should be preferred on signature match. For Hash preferences, checks the :default key.
111 112 113 114 115 116 117 |
# File 'lib/ast/merge/merger_config.rb', line 111 def prefer_destination? if @preference.is_a?(Hash) @preference.fetch(:default, :destination) == :destination else @preference == :destination end end |
#prefer_template? ⇒ Boolean
Check if template version should be preferred on signature match. For Hash preferences, checks the :default key.
123 124 125 126 127 128 129 |
# File 'lib/ast/merge/merger_config.rb', line 123 def prefer_template? if @preference.is_a?(Hash) @preference.fetch(:default, :destination) == :template else @preference == :template end end |
#preference_for(type) ⇒ Symbol
Get the preference for a specific node type or merge_type.
When preference is a Hash, looks up the preference for the given type, falling back to :default, then to :destination.
149 150 151 152 153 154 155 156 157 |
# File 'lib/ast/merge/merger_config.rb', line 149 def preference_for(type) if @preference.is_a?(Hash) @preference.fetch(type) do @preference.fetch(:default, :destination) end else @preference end end |
#to_h(default_freeze_token: nil) ⇒ Hash
Uses :preference key to match SmartMerger’s API (not :preference)
Convert config to a hash suitable for passing to SmartMerger.
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/ast/merge/merger_config.rb', line 171 def to_h(default_freeze_token: nil) result = { preference: @preference, add_template_only_nodes: @add_template_only_nodes, } result[:freeze_token] = @freeze_token || default_freeze_token if @freeze_token || default_freeze_token result[:signature_generator] = @signature_generator if @signature_generator result[:node_typing] = @node_typing if @node_typing result end |
#with(**options) ⇒ MergerConfig
Create a new config with updated values.
186 187 188 189 190 191 192 193 194 |
# File 'lib/ast/merge/merger_config.rb', line 186 def with(**) self.class.new( preference: .fetch(:preference, @preference), add_template_only_nodes: .fetch(:add_template_only_nodes, @add_template_only_nodes), freeze_token: .fetch(:freeze_token, @freeze_token), signature_generator: .fetch(:signature_generator, @signature_generator), node_typing: .fetch(:node_typing, @node_typing), ) end |