class Git::Commands::OperandAllocator::LeadingAllocationState
Encapsulates state for allocating leading positionals
@api private
Public Class Methods
Source
# File lib/git/commands/arguments.rb, line 4431 def initialize(definitions, values, required_check) @definitions = definitions @values = values @required_check = required_check @required_count = definitions.count { |d| required_check.call(d) } @extra_for_optionals = [values.size - @required_count, 0].max @val_idx = 0 @opt_idx = 0 @consumed = 0 end
Initialize allocation state for the given leading definitions and values
@param definitions [Array<Hash>] the leading operand definitions
@param values [Array] the values available for allocation
@param required_check [Method] callable returning true if a definition is required
@return [void]
Public Instance Methods
Source
# File lib/git/commands/arguments.rb, line 4448 def allocate(allocation) @definitions.each { |definition| allocate_one(allocation, definition) } @consumed end
Allocates leading positional values and returns consumed non-nil count
@param allocation [Hash{Symbol => Object}] allocation hash to populate
@return [Integer] number of non-nil positional values consumed
Private Instance Methods
Source
# File lib/git/commands/arguments.rb, line 4464 def allocate_one(allocation, definition) if @required_check.call(definition) allocate_required(allocation, definition) else allocate_optional(allocation, definition) end end
Allocate a single definition, dispatching to required or optional branch
@param allocation [Hash] the allocation hash to populate
@param definition [Hash] the operand definition to allocate
@return [void]
@api private
Source
# File lib/git/commands/arguments.rb, line 4496 def allocate_optional(allocation, definition) if @opt_idx < @extra_for_optionals allocation[definition[:name]] = value_or_default(definition) @consumed += 1 @val_idx += 1 else allocation[definition[:name]] = definition[:default] end @opt_idx += 1 end
Allocate an optional definition, consuming only if extra values remain
@param allocation [Hash] the allocation hash to populate
@param definition [Hash] the operand definition to allocate
@return [void]
@api private
Source
# File lib/git/commands/arguments.rb, line 4481 def allocate_required(allocation, definition) allocation[definition[:name]] = value_or_default(definition) @consumed += 1 if @val_idx < @values.size @val_idx += 1 end
Allocate a required definition, consuming the next value unconditionally
@param allocation [Hash] the allocation hash to populate
@param definition [Hash] the operand definition to allocate
@return [void]
@api private
Source
# File lib/git/commands/arguments.rb, line 4514 def value_or_default(definition) @val_idx < @values.size ? @values[@val_idx] : definition[:default] end
Return the current value from @values or the definition default if exhausted
@param definition [Hash] the operand definition
@return [Object] the value or default
@api private