Created
January 30, 2015 23:57
-
-
Save asterite/f9142f1b4b5361460bbf to your computer and use it in GitHub Desktop.
This file contains 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
require "json" | |
abstract class Plugin | |
macro def self.find_by_name(name) : Plugin.class | |
case name | |
{% for subclass in Plugin.all_subclasses %} | |
when {{subclass.name}} | |
{{subclass.name.id}} | |
{% end %} | |
else | |
raise "Uknown plugin: #{name}" | |
end | |
end | |
end | |
class PluginOne < Plugin | |
json_mapping({one: String}) | |
end | |
class PluginTwo < Plugin | |
json_mapping({two: String}) | |
end | |
class Plugins | |
def self.new(pull : JSON::PullParser) | |
plugins = [] of Plugin | |
pull.read_object do |key| | |
plugins << Plugin.find_by_name(key).new(pull) | |
end | |
plugins | |
end | |
end | |
class Config | |
json_mapping({plugins: Plugins}) | |
end | |
json = %( | |
{ | |
"plugins": { | |
"PluginOne": { | |
"one": "HOLA" | |
}, | |
"PluginTwo": { | |
"two": "CHAU" | |
} | |
} | |
} | |
) | |
config = Config.from_json(json) | |
pp config | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment