Skip to content

Instantly share code, notes, and snippets.

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