get "/" do
pattern = File.join(data_path, "*")
@files = Dir.glob(pattern).map do |path|
File.basename(path)
end
erb :index
end
def data_path if ENV["RACK_ENV"] == "test" File.expand_path("../test/data", __FILE__) else File.expand_path("../data", __FILE__) end end
data_path will check if ENV hash with key "RACK_ENV" has the value of "test". If yes, then return the path from root to cms2/test/data folder. If not , then return the absolute path from root to the folder cms2/data
Then, in get "/" block , join the data_path with * . If in development environment, then data_path is home/cms2/data then the return value is home/cms2/data/*
We use File.join is good because it will detect the OS, then join with appropriate character.
With the pattern in place, we use Dir.glob to find the files. Here it return home/cms2/data/history.txt, home/cms2/data/about.md and home/cms2/data/changes.txt , which we map it and alter the original array with just the basename of the path. Hence @files will be history.txt, changes.txt and about.md
def data_path if ENV["RACK_ENV"] == "test" File.expand_path("../test/data", __FILE__) else File.expand_path("../data", __FILE__) end end
data_path will check if ENV hash with key "RACK_ENV" has the value of "test". If yes, then return the path from root to cms2/test/data folder. If not , then return the absolute path from root to the folder cms2/data
Then, in get "/" block , join the data_path with * . If in development environment, then data_path is home/cms2/data then the return value is home/cms2/data/*
We use File.join is good because it will detect the OS, then join with appropriate character.
With the pattern in place, we use Dir.glob to find the files. Here it return home/cms2/data/history.txt, home/cms2/data/about.md and home/cms2/data/changes.txt , which we map it and alter the original array with just the basename of the path. Hence @files will be history.txt, changes.txt and about.md
Comments
Post a Comment