class Minimart::Web::CookbookShowPageGenerator

Generate “show” pages for a set of cookbooks

Attributes

clean_cookbooks[R]
cookbooks[R]

@return [Minimart::Web::Cookbooks] the cookbooks to generate show pages for

web_directory[R]

@return [String] the directory to put any generated HTML in

Public Class Methods

new(opts = {}) click to toggle source

@param [Hash] opts @option opts [String] :web_directory The directory to put any generated HTML in @option opts [String] :cookbooks The cookbooks to generate show pages for

# File lib/minimart/web/cookbook_show_page_generator.rb, line 18
def initialize(opts = {})
  @web_directory = opts[:web_directory]
  @cookbooks     = opts[:cookbooks]
  @clean_cookbooks = opts.fetch(:clean_cookbooks, true)
end

Public Instance Methods

generate() click to toggle source

Generate the HTML!

# File lib/minimart/web/cookbook_show_page_generator.rb, line 25
def generate
  clean_web_cookbooks_directory
  make_web_cookbooks_directory
  create_html_files
end

Private Instance Methods

clean_web_cookbooks_directory() click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 33
def clean_web_cookbooks_directory
  if Dir.exists?(cookbooks_directory) && @clean_cookbooks
    FileUtils.remove_entry(cookbooks_directory)
  end
end
cookbook_for_requirement(name, version_requirement) click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 70
def cookbook_for_requirement(name, version_requirement)
  (cookbooks[name] || []).find do |c|
    c.satisfies_requirement?(version_requirement)
  end
end
cookbooks_directory() click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 66
def cookbooks_directory
  File.join(web_directory, 'cookbooks')
end
create_html_files() click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 43
def create_html_files
  cookbooks.each do |cookbook_name, versions|
    versions.each do |cookbook|
      write_to_file(file(cookbook), template_content(cookbook, versions)) unless File.exists?(file(cookbook))
    end
  end
end
file(cookbook) click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 51
def file(cookbook)
  FileUtils.mkdir_p(File.join(web_directory, cookbook_dir(cookbook)))
  File.join(web_directory, cookbook_file(cookbook))
end
level() click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 76
def level
  2
end
make_web_cookbooks_directory() click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 39
def make_web_cookbooks_directory
  FileUtils.mkdir_p(cookbooks_directory)
end
template_content(cookbook, versions) click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 56
def template_content(cookbook, versions)
  render_in_base_layout do
    render_template('cookbook_show.erb', self, {cookbook: cookbook, other_versions: versions})
  end
end
write_to_file(file_path, content) click to toggle source
# File lib/minimart/web/cookbook_show_page_generator.rb, line 62
def write_to_file(file_path, content)
  File.open(file_path, 'w+') { |f| f.write(content) }
end