Last active
August 29, 2015 13:56
-
-
Save rummelonp/8995137 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
module ShigotoMachine | |
class Brain | |
def initialize(adapter) | |
@adapter = load_adapter(adapter) | |
end | |
def load_adapter(adapter) | |
klass_name = sub(/\A./) { $&.upcase } # camelize | |
result = [ | |
"shigoto_machine/adapters/#{adapter}", # bundled adapter | |
"shigoto_machine-#{adapter}", # external gem | |
].find do |f| | |
begin | |
require f | |
true | |
rescue LoadError => e | |
false | |
end | |
end | |
raise LoadError, "Can't load adapter \"#{adapter}\"" unless result | |
Adapters.const_get(klass_name).new(self) | |
end | |
end | |
module Adapters | |
class Adapter | |
def initialize(brain) | |
@brain = brain | |
end | |
end | |
# external file | |
class DekiruHito < Adapter | |
end | |
# external file | |
class Kuzu < Adapter | |
end | |
end | |
end |
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
module ShigotoMachine | |
class Brain | |
def initialize(adapter) | |
@adapter = load_adapter(adapter) | |
end | |
def load_adapter(adapter) | |
load_plugins unless @plugins_loaded | |
Adapters.find(adapter).new(self) | |
end | |
def load_plugins | |
# load shigoto_machine-* gems | |
end | |
end | |
module Adapters | |
def self.adapters | |
@adapters ||= [] | |
end | |
def self.register(name, klass) | |
adapters[name.to_sym] = klass | |
end | |
def self.find(name) | |
klass = adapters[name.to_sym] | |
raise ArgumentError, "No such adapter \"#{name}\"" unless klass | |
klass | |
end | |
class Adapter | |
def initialize(brain) | |
@brain = brain | |
end | |
end | |
class DekiruHito < Adapter | |
Adapters.register :dekiru, self | |
end | |
class Kuzu < Adapter | |
Adapters.register :kuzu, self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment