Skip to content

Instantly share code, notes, and snippets.

@asterite
Created January 30, 2015 23:57
Show Gist options
  • Save asterite/f9142f1b4b5361460bbf to your computer and use it in GitHub Desktop.
Save asterite/f9142f1b4b5361460bbf to your computer and use it in GitHub Desktop.
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