Created
November 9, 2024 03:15
-
-
Save jgarber623/7b1ffad8db34a386bd68574b80496e26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Internal | |
def self.parsers | |
@parsers ||= {} | |
end | |
end | |
class Parser | |
def initialize(input) | |
@input = input | |
end | |
class << self | |
delegate :parsers, to: Internal | |
def parse(type, *args) | |
parsers[type].new(*args).value | |
end | |
def register(type, &block) | |
parsers[type] = Class.new(self, &block) | |
end | |
end | |
private | |
attr_reader :input | |
end | |
class PlainTextParser < Parser | |
register 'p' do | |
def value | |
@value ||= input.upcase | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment