Sha256: 7e1530b9f55c8d91812232ae6fe6ec01dec6137432302bd03e3f736e237cf0b3

Contents?: true

Size: 1.42 KB

Versions: 8

Compression:

Stored size: 1.42 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
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fun_with_files-0.0.14 ./lib/fun_with/files/file_orderer.rb
fun_with_files-0.0.13 ./lib/fun_with/files/file_orderer.rb
fun_with_files-0.0.12 ./lib/fun_with/files/file_orderer.rb
fun_with_files-0.0.9 ./lib/fun_with/files/file_orderer.rb
fun_with_files-0.0.8 ./lib/fun_with/files/file_orderer.rb
fun_with_files-0.0.7 ./lib/fun_with/files/file_orderer.rb
fun_with_files-0.0.6 ./lib/files/file_orderer.rb
fun_with_files-0.0.5 ./lib/files/file_orderer.rb