Class: Git::ArgsBuilder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/git/args_builder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Takes a hash of user options and a declarative map and produces an array of command-line arguments. Also validates that only supported options are provided based on the map.

API:

  • private

Constant Summary collapse

ARG_BUILDERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

This hash maps an option type to a lambda that knows how to build the corresponding command-line argument. This is a scalable dispatch table.

API:

  • private

{
  boolean: ->(config, value) { value ? config[:flag] : [] },

  boolean_negatable: lambda do |config, value|
    case value
    when true then config[:flag]
    when false then config[:flag].sub('--', '--no-')
    else []
    end
  end,

  valued_equals: ->(config, value) { "#{config[:flag]}=#{value}" if value },

  valued_space: ->(config, value) { [config[:flag], value.to_s] if value },

  repeatable_valued_space: lambda do |config, value|
    Array(value).flat_map { |v| [config[:flag], v.to_s] }
  end,

  custom: ->(config, value) { config[:builder].call(value) },

  validate_only: ->(_config, _value) { [] } # Does not build any args
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, option_map) ⇒ ArgsBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ArgsBuilder.

API:

  • private



48
49
50
51
# File 'lib/git/args_builder.rb', line 48

def initialize(opts, option_map)
  @opts = opts
  @option_map = option_map
end

Class Method Details

.build(opts, option_map)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Main entrypoint to validate options and build arguments

API:

  • private



37
38
39
40
# File 'lib/git/args_builder.rb', line 37

def self.build(opts, option_map)
  validate!(opts, option_map)
  new(opts, option_map).build
end

.validate!(opts, option_map)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Public validation method that can be called independently

API:

  • private



43
44
45
46
# File 'lib/git/args_builder.rb', line 43

def self.validate!(opts, option_map)
  validate_unsupported_keys!(opts, option_map)
  validate_configured_options!(opts, option_map)
end

Instance Method Details

#build

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/git/args_builder.rb', line 53

def build
  @option_map.flat_map do |config|
    type = config[:type]
    next config[:flag] if type == :static

    key = config[:keys].find { |k| @opts.key?(k) }
    next [] unless key

    build_arg_for_option(config, @opts[key])
  end.compact
end