class Dreck::Result

Represents the state and results of a {Dreck} parse.

Attributes

expected[R]

@return [Array] the type, name, and count information for arguments

results[R]

@return [Hash] the parsing results hash

Public Class Methods

new(args, strict: true) click to toggle source

@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

[](key) click to toggle source

@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_absorption!() click to toggle source

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_expected() click to toggle source

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
list(type, sym, count: nil) click to toggle source

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
parse!() click to toggle source

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_list!(type, sym, count) click to toggle source

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_type!(type, sym) click to toggle source

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
strict?() click to toggle source

@return [Boolean] whether the results were parsed strictly

# File lib/dreck/result.rb, line 31
def strict?
  @strict
end