Created
March 29, 2012 15:31
-
-
Save mbleigh/2238555 to your computer and use it in GitHub Desktop.
A rough and naive declarative syntax for class reloading.
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 Bouncy | |
def self.mappings | |
@@mappings ||= [] | |
end | |
def self.bounce! | |
mappings.each(&:bounce!) | |
end | |
def self.runner(const_name) | |
lambda{|env| | |
eval(const_name).call(env) | |
} | |
end | |
class Middleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
Bouncy.bounce! | |
@app.call(env) | |
end | |
end | |
class Mapping | |
attr_reader :constant, :path | |
def self.add(*args) | |
Bouncy.mappings << new(*args) | |
end | |
def initialize(constant, path) | |
@constant = constant | |
@path = path | |
end | |
def parent_const | |
eval(constant.split("::")[-2] || "Object") | |
end | |
def const_name | |
constant.split("::").last.to_sym | |
end | |
def load_path | |
[File.dirname(__FILE__), path].join('/') + '.rb' | |
end | |
def bounce! | |
parent_const.send :remove_const, const_name | |
load load_path | |
end | |
end | |
end | |
def map(constant, path) | |
Bouncy::Mapping.add(constant, path) | |
end | |
# and now the payoff.... | |
map 'Some::Constant', 'some/constant' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment