Skip to content

Instantly share code, notes, and snippets.

@mbleigh
Created March 29, 2012 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbleigh/2238555 to your computer and use it in GitHub Desktop.
Save mbleigh/2238555 to your computer and use it in GitHub Desktop.
A rough and naive declarative syntax for class reloading.
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