class Dreck::Result
Represents the state and results of a {Dreck} parse.
Attributes
@return [Array] the type, name, and count information for arguments
@return [Hash] the parsing results hash
Public Class Methods
@param args [Array<String>] the arguments to parse @param strict [Boolean] whether or not to be strict about argument absorption
# File lib/dreck/result.rb, line 17 def initialize(args, strict: true) @args = args.dup @strict = strict @expected = [] @results = {} end
Public Instance Methods
@param key [Symbol] the named argument to look up @return [Object] the parsed value
# File lib/dreck/result.rb, line 26 def [](key) @results[key] end
Check whether the expected arguments absorb all supplied arguments. @raise [AbsoptionError] if absorption fails and {strict?} is not true @api private
# File lib/dreck/result.rb, line 74 def check_absorption! count, greedy = count_expected return unless strict? raise GreedyAbsorptionError.new(count, @args.size) if count >= @args.size && greedy raise AbsorptionError.new(count, @args.size) if count != @args.size && !greedy end
Count the number of arguments expected to be supplied. @raise [Integer, nil] the number of expected arguments, or nil if a list
of indeterminate size is specified
@api private
# File lib/dreck/result.rb, line 87 def count_expected count = @expected.inject(0) do |n, exp| case exp.first when :list # if the list is greedy, all arguments have to be absorbed return [n, true] unless exp[3] n + exp[3] else n + 1 end end [count, false] end
Specifies a list of arguments of a given type to be parsed. @param type [Symbol] the type of the arguments @param sym [Symbol] the name of the argument in {results} @param count [Integer] the number of arguments in the list
# File lib/dreck/result.rb, line 45 def list(type, sym, count: nil) if count raise BadCountError unless count.positive? end @expected << [:list, type, sym, count] end
Perform the actual parsing. @return [Result] this instance @api private
# File lib/dreck/result.rb, line 56 def parse! check_absorption! @expected.each do |type, *rest| case type when :list parse_list!(*rest) else parse_type!(type, *rest) end end self end
Parse a one or more expected arguments of a given type and add them to
the results.
@param type [Symbol] the type of the individual elements of the list @param sym [Symbol] the key to store the results under in {results} @param count [Integer, nil] the size of the list, or nil if the list
absorbs all following arguments
@api private
# File lib/dreck/result.rb, line 110 def parse_list!(type, sym, count) args = if count @args.shift count else @args end @results[sym] = Parser.parse_list type, args end
Parse one expected argument of a given scalar type and add it to the results. @param type [Symbol] the type of the expected argument @param sym [Symbol] the key to store the result under in {results} @api private
# File lib/dreck/result.rb, line 124 def parse_type!(type, sym) arg = @args.shift @results[sym] = Parser.send("parse_#{type}", arg) end
@return [Boolean] whether the results were parsed strictly
# File lib/dreck/result.rb, line 31 def strict? @strict end