Class: Gtiny::Parser
- Inherits:
-
Object
- Object
- Gtiny::Parser
- Defined in:
- lib/gtiny/parser.rb
Overview
A class that will check a string against registered encoding classes.
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #parse(string) ⇒ Object
- #parse!(string) ⇒ Object
- #register(klass) ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
6 7 8 |
# File 'lib/gtiny/parser.rb', line 6 def initialize @registrants = [] end |
Instance Method Details
#parse(string) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gtiny/parser.rb', line 14 def parse(string) @registrants.each do |registrant| next unless registrant.matches?(string) identifier = registrant.new(string, check: false) next unless identifier.valid? return identifier end nil end |
#parse!(string) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/gtiny/parser.rb', line 27 def parse!(string) identifier = parse(string) return identifier unless identifier.nil? raise(ParseError, "not a known identifier") end |
#register(klass) ⇒ Object
10 11 12 |
# File 'lib/gtiny/parser.rb', line 10 def register(klass) @registrants << klass end |