Class: Dreck::Result
- Inherits:
-
Object
- Object
- Dreck::Result
- Defined in:
- lib/dreck/result.rb
Overview
Represents the state and results of a Dreck parse.
Instance Attribute Summary collapse
-
#expected ⇒ Array
readonly
The type, name, and count information for arguments.
-
#results ⇒ Hash
readonly
The parsing results hash.
Instance Method Summary collapse
-
#[](key) ⇒ Object
The parsed value.
-
#check_absorption! ⇒ Object
private
Check whether the expected arguments absorb all supplied arguments.
-
#count_expected ⇒ Object
private
Count the number of arguments expected to be supplied.
-
#initialize(args, strict: true) ⇒ Result
constructor
A new instance of Result.
-
#list(type, sym, count: nil) ⇒ Object
Specifies a list of arguments of a given type to be parsed.
-
#parse! ⇒ Result
private
Perform the actual parsing.
-
#parse_list!(type, sym, count) ⇒ Object
private
Parse a one or more expected arguments of a given type and add them to the results.
-
#parse_type!(type, sym) ⇒ Object
private
Parse one expected argument of a given scalar type and add it to the results.
-
#strict? ⇒ Boolean
Whether the results were parsed strictly.
Constructor Details
#initialize(args, strict: true) ⇒ Result
Returns a new instance of Result.
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
#expected ⇒ Array (readonly)
Returns the type, name, and count information for arguments.
10 11 12 |
# File 'lib/dreck/result.rb', line 10 def expected @expected end |
#results ⇒ Hash (readonly)
Returns 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.
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.
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_expected ⇒ 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.
Count the number of arguments expected to be supplied.
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.
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.
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.
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.
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.
31 32 33 |
# File 'lib/dreck/result.rb', line 31 def strict? @strict end |