Created
November 4, 2019 08:07
-
-
Save miyucy/96321e8cd9eca544a2415b8b1bd73947 to your computer and use it in GitHub Desktop.
This file contains 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
# @example | |
# Rails.configuration.middleware.use( | |
# CommitteeReloader, | |
# "path/to/schema", | |
# [ | |
# Committee::Middleware::RequestValidation, | |
# schema_path: "path/to/schema" | |
# ] | |
# ) | |
class CommitteeReloader | |
def initialize(app, files, *middlewares) | |
@original_app = proc { |env| app.call env } | |
@middlewares = middlewares.reverse | |
@files = Array(files) | |
end | |
def call(env) | |
check | |
@app.call(env) | |
end | |
def check | |
current = @files.map { |file| File.mtime(file) }.max | |
return if @latest == current | |
@latest = current | |
build | |
end | |
def build | |
@app = @middlewares.inject(@original_app) do |result, (klass, args)| | |
klass.new(result, args) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment