Sha256: ebd4c8979c45513812ba5e2424309e473d220c96cc31a3731c10859286c3e4d5

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

module Morion
  class FoldersAndFilesFinder
    EXTENSION_FILTER_REGEX = /.*(\.jpg|\.jpeg|\.png|\.svg)/

    def initialize
      @rails_root = Rails.root.to_s
    end

    def folder_paths
      app_assets_paths = Rails.application.assets.paths.select {|path| path =~ /#{@rails_root}/}
      app_assets_paths.map do |path|
        pathname = Pathname.new(path)
        pathname.exist? ? pathname.cleanpath : nil
      end.compact
    end

    def file_paths
      Find.find(*folder_paths).select do |path|
        path =~ EXTENSION_FILTER_REGEX && permitted_path?(path)
      end
    end

    def files_by_folders
      files = file_paths.map do |path|
        File.new(path, folder_paths, @rails_root)
      end

      @folders = files.group_by {|file| file.folder_path }
    end

    private

    def permitted_path?(path)
      if Morion::Config.blacklist.any?
        !blacklisted?(path)
      elsif Morion::Config.whitelist.any?
        whitelisted?(path)
      else
        true
      end
    end

    def blacklisted?(path)
      include_path?(path, Morion::Config.blacklist)
    end

    def whitelisted?(path)
      include_path?(path, Morion::Config.whitelist)
    end

    def include_path?(path, list)
      list.any? do |filter|
        path.match(/#{filter}/)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
morion-0.0.9 lib/morion/folders_and_files_finder.rb
morion-0.0.8 lib/morion/folders_and_files_finder.rb
morion-0.0.7 lib/morion/folders_and_files_finder.rb
morion-0.0.6 lib/morion/folders_and_files_finder.rb