Sha256: 28284f13d4131b2ad5b5b638dc1556259a47d69db99a98313272040a29eac3d1

Contents?: true

Size: 936 Bytes

Versions: 4

Compression:

Stored size: 936 Bytes

Contents

module HighVoltage
  # A command for finding pages by id. This encapsulates the concepts of
  # mapping page names to file names.
  class PageFinder
    def initialize(page_id)
      @page_id = page_id
    end

    # Produce a template path to the page, in a format understood by
    # `render :template => find`
    def find
      path = clean_content_pathname.join(page_id.tr("\\", "/")).cleanpath.to_s
      if !path.start_with?("#{clean_content_pathname}/")
        raise InvalidPageIdError.new "Invalid page id: #{page_id}"
      end
      path
    rescue ArgumentError
      raise InvalidPageIdError.new "Invalid page id: #{page_id}"
    end

    def content_path
      HighVoltage.content_path
    end

    protected

    # The raw page id passed in by the user
    attr_reader :page_id

    def clean_content_pathname
      Pathname.new(content_path).cleanpath
    end
  end

  class InvalidPageIdError < StandardError; end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
high_voltage-4.0.0 lib/high_voltage/page_finder.rb
high_voltage-4.0.0.rc1 lib/high_voltage/page_finder.rb
high_voltage-3.1.2 lib/high_voltage/page_finder.rb
high_voltage-3.1.1 lib/high_voltage/page_finder.rb