Sha256: 6f5c8ef982f9451913673919e50db2ea08211de65ba296089f2f2b7f8c740863

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

module FunWith
  module Files
    # FileOrderer holds a set of strings/regexes, then reorders a set of files (FunWith::Files::FilePaths, actually)
    # by matching the filenames against one regex after another.  Allows you to say,
    # "I want the title page, the foreward, then all the chapters, then all the appendixes, then the afterword."
    # Ex:  FileOrderer( ["title_page", "forward", "chapter-.*", "afterword", "appendix.*" ).reorder( pages )
    # Only compares the basename minus extension.  Files should come from the same directory.  cover.extension always comes first.
    class FileOrderer
      def initialize( matchers )
        @matchers = matchers.map do |m| 
          case m
          when Regexp
            m
          when String
            /^#{m}$/
          end
        end
        
        @matchers.unshift( /^cover$/ )
        @matchers.push( /^.*$/ )
      end
      
      def reorder( files )
        files = files.map(&:fwf_filepath)
        
        ordered_files = @matchers.inject( [] ) do |collector, matcher|
          matched_files = files.select do |f| 
            name = f.basename_no_ext.to_s
            matcher.match( name )
          end
          
          matched_files.sort_by! do |filename|
            filename.basename_no_ext.to_s
          end
          
          collector += matched_files.sort
          files -= matched_files
          
          collector
        end
        
        ordered_files
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fun_with_files-0.0.18 ./lib/fun_with/files/file_orderer.rb
fun_with_files-0.0.15 ./lib/fun_with/files/file_orderer.rb