Skip to content

Instantly share code, notes, and snippets.

@jgarber623
Created November 9, 2024 03:15
Show Gist options
  • Save jgarber623/7b1ffad8db34a386bd68574b80496e26 to your computer and use it in GitHub Desktop.
Save jgarber623/7b1ffad8db34a386bd68574b80496e26 to your computer and use it in GitHub Desktop.
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