Sha256: 865ca61aca407a2658fa2b7088d563579bd9ede88495240b8ff4f6882155b44a

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Scrapbook
  # Modeling the scrap book
  class Scrapbook
    NotFoundError = Class.new(StandardError)

    attr_accessor :root

    def self.find_scrapbook_for(pathname)
      scrapbooks = ::Scrapbook::Engine.config.scrapbook.paths
      candidates = scrapbooks.filter_map do |book, pname|
        relative_path = pathname.relative_path_from(pname)
        next if relative_path.to_s.start_with?('..')

        [book, relative_path.each_filename.count]
      end
      raise NotFoundError if candidates.empty?

      new(scrapbooks[candidates.min_by { _1[1] }.first])
    end

    def initialize(root)
      self.root = Pathname.new(root)
    end

    def name
      root.basename.to_s
    end

    def pages_pathname
      root.join('pages')
    end

    def relative_page_path_for(pathname)
      return '' if pathname == pages_pathname

      relative_path = pathname.relative_path_from(pages_pathname).to_s
      if relative_path.start_with?('..')
        raise ArgumentError, "Pathname isn't inside the scrapbook pages: #{relative_path}"
      end

      relative_path
    end

    def ==(other)
      other.class == self.class && other.root == root
    end
    alias eql? ==
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scrapbook-0.2.2 app/models/scrapbook/scrapbook.rb