Skip to content

Instantly share code, notes, and snippets.

@miyucy
Created November 4, 2019 08:07
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 miyucy/96321e8cd9eca544a2415b8b1bd73947 to your computer and use it in GitHub Desktop.
Save miyucy/96321e8cd9eca544a2415b8b1bd73947 to your computer and use it in GitHub Desktop.
# @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