Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added Parser#summarize, so the hash summary previously returned by Pa…

…rser#parse can be fetched later
  • Loading branch information...
commit cf7c45eec284140e8baf52ded500102f14551baf 1 parent 89a65ba
@adammck adammck authored
Showing with 18 additions and 2 deletions.
  1. +18 −2 lib/fuzz/parser.rb
View
20 lib/fuzz/parser.rb
@@ -30,7 +30,6 @@ def add_token(title, token, *init_args)
def parse(str)
@matches = []
- summary = {}
# it's okay if the argument can't
# be matched against (it might be
@@ -39,9 +38,9 @@ def parse(str)
return nil unless\
str.respond_to? :match
+ # build an array of the matched tokens
@tokens.each do |token|
unless(extracted = token.extract!(str)).nil?
- summary[token.name] = extracted.value
@matches.push(extracted)
end
end
@@ -51,6 +50,23 @@ def parse(str)
# with it (like refer it to a human)
@unparsed_str = str
+ # return a summary
+ # of the parsed data
+ summary
+ end
+
+ # Returns a hash containing a key/value
+ # pair for each matched token's name and
+ # normalized value, or raises NotParsedYet
+ # if _parse_ has not been called yet.
+ def summary
+ raise_unless_parsed
+
+ summary = {}
+ @matches.collect do |mat|
+ summary[mat.token.name] = mat.value
+ end
+
# return nil for no matches, or hash
# containing a summary of the matches
(summary.length == 0) ? nil : summary
Please sign in to comment.
Something went wrong with that request. Please try again.