Sha256: a5ba215b1c91253c4e6e0b44ae0bccdf76a45c7d078027bd481b02cd19d283b6

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module Geri

  # Page encapsulates a site page and it's metadata
  class Page
    attr_reader :page_index, :file

    META_CAPTURE = /\A---(.*)\n---\B/m

    # Finds a page within the site and returns a new Page instance
    #
    # @param path[string|NilClass] the url path for which to look for the page
    # @return [Page] a page object
    def self.find(path)
      path = '/' unless path
      new(path)
    end

    # @private
    def initialize(path)
      @path       = path
      @file       = index_or_name
    end

    # Returns the metadata found in the metadata section of the page as a symbolized hash
    #
    # @return [Hash] the page metadata
    def metadata
      unless @metadata && capture_metadata
        @metadata ||= YAML.load(capture_metadata)
      end
      @metadata.symbolize_keys!
    end

    private

    def capture_metadata
      @captured_metadata ||= META_CAPTURE.match(raw)[1]
    end

    def raw
      @raw ||= File.read(file)
    end

    def index_or_name
      return site_root + @path + '/index.html.erb' unless File.exists?(site_root + @path + '.html.erb')
      site_root + @path + '.html.erb'
    end

    def site_root
      Rails.root.join('site').to_s
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geri-0.1.1 app/models/geri/page.rb
geri-0.1.0 app/models/geri/page.rb