gitlab-org--gitlab-foss/lib/gitlab/template/base_template.rb

68 lines
1.5 KiB
Ruby
Raw Normal View History

2016-05-27 05:00:56 -04:00
module Gitlab
module Template
class BaseTemplate
def initialize(path)
@path = path
end
def name
File.basename(@path, self.class.extension)
end
def content
2016-06-17 02:37:19 -04:00
File.read(@path)
end
2016-05-27 05:00:56 -04:00
class << self
def all
self.categories.keys.flat_map { |cat| by_category(cat) }
2016-05-27 05:00:56 -04:00
end
def find(key)
file_name = "#{key}#{self.extension}"
directory = select_directory(file_name)
directory ? new(File.join(category_directory(directory), file_name)) : nil
2016-05-27 05:00:56 -04:00
end
2016-06-17 02:37:19 -04:00
def categories
raise NotImplementedError
end
def extension
raise NotImplementedError
end
def base_dir
raise NotImplementedError
end
2016-05-27 05:00:56 -04:00
def by_category(category)
templates_for_directory(category_directory(category))
2016-05-27 05:00:56 -04:00
end
def category_directory(category)
File.join(base_dir, categories[category])
2016-05-27 05:00:56 -04:00
end
private
def select_directory(file_name)
categories.keys.find do |category|
File.exist?(File.join(category_directory(category), file_name))
end
2016-05-27 05:00:56 -04:00
end
def templates_for_directory(dir)
dir << '/' unless dir.end_with?('/')
Dir.glob(File.join(dir, "*#{self.extension}")).select { |f| f =~ filter_regex }.map { |f| new(f) }
2016-05-27 05:00:56 -04:00
end
def filter_regex
@filter_reges ||= /#{Regexp.escape(extension)}\z/
2016-05-27 05:00:56 -04:00
end
end
end
end
end