Created
January 30, 2015 22:53
-
-
Save asterite/f73d23c2810dcb1f7b97 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
require "json" | |
abstract class Plugin | |
ALL = {} of String => Plugin.class | |
macro inherited | |
ALL[{{@type.name}}] = {{@type.name.id}} | |
end | |
end | |
class PluginOne < Plugin | |
json_mapping({one: String}) | |
def self.magic(pull) | |
new(pull) | |
end | |
end | |
class PluginTwo < Plugin | |
json_mapping({two: String}) | |
def self.magic(pull) | |
new(pull) | |
end | |
end | |
class Plugins | |
def self.new(pull : JSON::PullParser) | |
plugins = [] of Plugin | |
pull.read_object do |key| | |
plugin_class = Plugin::ALL[key] | |
plugins << plugin_class.magic(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