diff options
author | Eric Wong <e@80x24.org> | 2016-02-14 21:44:22 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2016-02-14 22:31:36 +0000 |
commit | fa7ae447b47085ab661898d0015c6d4f4c27c596 (patch) | |
tree | a9cc7169efc6a639c919ee382bc4d1ba3db4dfd4 | |
parent | 3c341acc9dc8d0faa583d12d97b83f43eb0358c7 (diff) | |
download | yahns-fa7ae447b47085ab661898d0015c6d4f4c27c596.tar.gz |
Switch option initialization to using a keyword hash since yet-another boolean is too much. Using kwargs won't work under Ruby 1.9.3 which we still support (for now). Note: being a part of extras/, there's no API stability guarantees but this should've maintained it.
-rw-r--r-- | extras/autoindex.rb | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/extras/autoindex.rb b/extras/autoindex.rb index e84aa7b..4deb3ef 100644 --- a/extras/autoindex.rb +++ b/extras/autoindex.rb @@ -14,13 +14,29 @@ class Autoindex FN = %{<a href="%s">%s</a>} TFMT = "%Y-%m-%d %H:%M" - def initialize(app, index = %w(index.html), skip_gzip_static = true) + def initialize(app, *args) app.respond_to?(:root) or raise ArgumentError, - "wrapped app #{app.inspect} does not respond to :root" + "wrapped app #{app.inspect} does not respond to #root" @app = app @root = app.root - @index = index - @skip_gz = skip_gzip_static + + @index = case args[0] + when Array then args.shift + when String then Array(args.shift) + else + %w(index.html) + end + + @skip_gzip_static = @skip_dotfiles = nil + case args[0] + when Hash + @skip_gzip_static = args[0][:skip_gzip_static] + @skip_dotfiles = args[0][:skip_dotfiles] + when true, false + @skip_gzip_static = args.shift + end + @skip_gzip_static = true if @skip_gzip_static.nil? + @skip_dotfiles = false if @skip_dotfiles.nil? end def redirect_slash(env) @@ -69,13 +85,15 @@ class Autoindex # generate the index, show directories first dirs = [] files = [] - ngz_idx = {} if @skip_gz # used to avoid redundant stat() + ngz_idx = {} if @skip_gzip_static # used to avoid redundant stat() dir.each do |base| case base when "." next when ".." next if path_info == "/" + when /\A\./ + next if @skip_dotfiles end begin |