Sha256: 635037a7fcd181b3084684f6213a2168813e81ed974df974fd4e7dc3197a49e3

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 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 relative_page_pathname_for(pathname)
      relative_path = pathname.relative_path_from(pages_pathname)
      if relative_path.to_s.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

3 entries across 3 versions & 1 rubygems

Version Path
scrapbook-0.3.2 app/models/scrapbook/scrapbook.rb
scrapbook-0.3.1 app/models/scrapbook/scrapbook.rb
scrapbook-0.3.0 app/models/scrapbook/scrapbook.rb