Class: Dreck::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/dreck/result.rb

Overview

Represents the state and results of a Dreck parse.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, strict: true) ⇒ Result

Returns a new instance of Result.

Parameters:

  • args (Array<String>)

    the arguments to parse

  • strict (Boolean) (defaults to: true)

    whether or not to be strict about argument absorption



17
18
19
20
21
22
# File 'lib/dreck/result.rb', line 17

def initialize(args, strict: true)
  @args = args.dup
  @strict = strict
  @expected = []
  @results = {}
end

Instance Attribute Details

#expectedArray (readonly)

Returns the type, name, and count information for arguments.

Returns:

  • (Array)

    the type, name, and count information for arguments



10
11
12
# File 'lib/dreck/result.rb', line 10

def expected
  @expected
end

#resultsHash (readonly)

Returns the parsing results hash.

Returns:

  • (Hash)

    the parsing results hash



13
14
15
# File 'lib/dreck/result.rb', line 13

def results
  @results
end

Instance Method Details

#[](key) ⇒ Object

Returns the parsed value.

Parameters:

  • key (Symbol)

    the named argument to look up

Returns:

  • (Object)

    the parsed value



26
27
28
# File 'lib/dreck/result.rb', line 26

def [](key)
  @results[key]
end

#check_absorption!Object

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.

Check whether the expected arguments absorb all supplied arguments.

Raises:

  • (AbsoptionError)

    if absorption fails and #strict? is not true



74
75
76
77
78
79
80
81
# 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_expectedObject

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.

Count the number of arguments expected to be supplied.

Raises:

  • (Integer, nil)

    the number of expected arguments, or nil if a list of indeterminate size is specified



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object

Specifies a list of arguments of a given type to be parsed.

Parameters:

  • type (Symbol)

    the type of the arguments

  • sym (Symbol)

    the name of the argument in #results

  • count (Integer) (defaults to: nil)

    the number of arguments in the list



45
46
47
48
49
50
51
# 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!Result

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.

Perform the actual parsing.

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object

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.

Parse a one or more expected arguments of a given type and add them to the results.

Parameters:

  • type (Symbol)

    the type of the individual elements of the list

  • sym (Symbol)

    the key to store the results under in #results

  • count (Integer, nil)

    the size of the list, or nil if the list absorbs all following arguments



110
111
112
113
114
115
116
117
118
# 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) ⇒ Object

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.

Parse one expected argument of a given scalar type and add it to the results.

Parameters:

  • type (Symbol)

    the type of the expected argument

  • sym (Symbol)

    the key to store the result under in #results



124
125
126
127
128
# File 'lib/dreck/result.rb', line 124

def parse_type!(type, sym)
  arg = @args.shift

  @results[sym] = Parser.send("parse_#{type}", arg)
end

#strict?Boolean

Returns whether the results were parsed strictly.

Returns:

  • (Boolean)

    whether the results were parsed strictly



31
32
33
# File 'lib/dreck/result.rb', line 31

def strict?
  @strict
end