class Amber::SiteLoader

Public Class Methods

load(config) click to toggle source

page_paths pages_by_name pages_by_path pages_by_locale_path root dir_list

# File lib/amber/site_loader.rb, line 18
def self.load(config)
  config.reset_timestamp

  # create base_page
  base_page = begin
    if config.path.nil?
      @root = StaticPage.new(nil, 'root', config.pages_dir)
      add_page(@root)
      @root
    else
      name = File.basename(config.path)
      page = StaticPage.new(find_parent(config.path), name, config.pages_dir, config.path_prefix)
      add_page(page)
      page
    end
  end
  base_page.config = config

  # load menu and locals
  I18n.load_path += Dir[File.join(config.locales_dir, '/*.{rb,yml,yaml}')] if config.locales_dir

  # add the full directory tree
  base_page.scan_directory_tree do |page, asset_dir|
    add_page(page) if page
    @dir_list << asset_dir if asset_dir
  end
  @page_paths += @pages_by_path.keys

  # recursively add sub-sites
  config.children.each do |sub_config|
    add_configuration(sub_config)
  end
end

Private Class Methods

find_parent(path) click to toggle source
# File lib/amber/site_loader.rb, line 54
def self.find_parent(path)
  so_far = []
  path.split('/').compact.each do |path_segment|
    so_far << path_segment
    if page = @pages_by_path[so_far.join('/')]
      return page
    end
  end
  return @root
end

Private Instance Methods

add_aliases(locale, page, path_hash) click to toggle source

registers a page's aliases with the site

# File lib/amber/site_loader.rb, line 82
def add_aliases(locale, page, path_hash)
  page.aliases(locale).each do |alias_path|
    alias_path_str = alias_path.join('/')
    if path_hash[alias_path_str]
      Amber.logger.warn "WARNING: page `#{page.path.join('/')}` has alias `#{alias_path_str}`, but this path is already taken by `#{path_hash[alias_path_str].path.join('/')}` (locale = #{locale})."
    else
      path_hash[alias_path_str] = page
    end
  end
end
add_page(page) click to toggle source

registers a page with the site, indexing the page path in our various hashes

# File lib/amber/site_loader.rb, line 68
def add_page(page)
  @pages_by_name[page.name] = page
  @pages_by_path[page.path.join('/')] = page
  add_aliases(I18n.default_locale, page, @pages_by_path)
  page.locales.each do |locale|
    next if locale == I18n.default_locale
    add_aliases(locale, page, @pages_by_locale_path[locale])
  end
  @page_list << page
end