Sha256: a1ed9c7cdab986969523e4f6f723b03f7056be2fc8ebc8eb5f158426f89efff5

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Scrapbook
  # Model to assest list a folder's contents
  class FolderListingViewModel
    attr_reader :view, :scrapbook, :pathname, :files, :folders

    def initialize(view, scrapbook, pathname)
      self.view = view
      self.scrapbook = scrapbook
      self.pathname = pathname.directory? ? pathname : pathname.dirname
      self.folders, self.files = split_files_and_folders
    end

    def root?
      pathname == scrapbook.pages_pathname
    end

    def parent_display_name
      return nil if root?
      return scrapbook.name if pathname.parent == scrapbook.pages_pathname

      pathname.parent.basename.to_s
    end

    def header_name
      root? ? "/#{scrapbook.name}" : "/#{pathname.basename}/"
    end

    private

    attr_writer :view, :scrapbook, :pathname, :files, :folders

    def split_files_and_folders
      helper = HelperForView.new(view)

      folders, files = pathname.children.each_with_object([[], []]) do |pname, acc|
        next if pname.basename.to_s.start_with?('.')

        if pname.directory?
          acc[0] << pname
        else
          acc[1] << helper.remove_handler_exts_from(pname)
        end
      end

      folders.sort! { |a, b| a.to_s.downcase <=> b.to_s.downcase }
      files.sort! { |a, b| a.to_s.downcase <=> b.to_s.downcase }

      [folders, files]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scrapbook-0.2.2 app/helpers/scrapbook/folder_listing_view_model.rb
scrapbook-0.2.1 app/helpers/scrapbook/folder_listing_view_model.rb
scrapbook-0.2.0 app/helpers/scrapbook/folder_listing_view_model.rb