1. heliostatic
  2. ebook-renamer

Commits

heliostatic  committed c628d12

added splitting on periods

  • Participants
  • Parent commits 88fe0f5
  • Branches master

Comments (0)

Files changed (1)

File lib/extensions/book_cleaner.rb

View file
  • Ignore whitespace
     best_guess_fields(expensive)
   end
 
-
   def partial_path
     File.basename(self.fullpath, self.file_type)
   end
   end
 
   private
-    def set_author_if_possible(f)
-      if @cache
-        author = f if @cache.include?(f)
-        author = flip_and_strip(f) if @cache.include?(flip_and_strip(f))
-      else
-        author = f if Book.where(author: f).where('for_review IS NOT TRUE').any?
-        author = flip_and_strip(f) if Book.where(author: flip_and_strip(f)).where('for_review IS NOT TRUE').any?
-      end    
-      if self.author.nil?
-        self.update_attributes(author: author) if author
-      end  
-      author
-    end
 
-    def flip_and_strip(string)
-      string.split(',').reverse.join(' ').strip
+    def get_fields_from_path(path, ext)
+      fields = File.basename(path, ext)
+      fields_array = split_fields(fields)
+      clean_fields(fields_array)
     end
 
-    def get_fields_from_path(path, ext)
-      fields = File.basename(path, ext).split(/(?:\s-\s|_-_)/).map { |f| f.gsub(/_\s/, ': ').gsub('_', ' ').titlecase.strip }
+    def split_fields(fields)
+      fields = if fields.count('.') > 3
+        fields.split('.')
+      else
+        fields.split(/(?:\s-\s|_-_)/)
+      end
       if fields.length < 2
         fields << File.dirname(self.fullpath).split('/').last
       end
       fields
     end
 
+    def clean_fields(fields)
+      fields.map { |f| f.gsub(/_\s/, ': ').gsub('_', ' ').titlecase.strip }
+    end
+
     def check_author(fields)
       if self.author.nil? || self.title.nil?
         fields.each_with_index do |f, i|
       end
     end
 
+    def flip_and_strip(string)
+      string.split(',').reverse.join(' ').strip
+    end
+
+    def set_author_if_possible(f)
+      if @cache
+        author = f if @cache.include?(f)
+        author = flip_and_strip(f) if @cache.include?(flip_and_strip(f))
+      else
+        author = f if Book.where(author: f).where('for_review IS NOT TRUE').any?
+        author = flip_and_strip(f) if Book.where(author: flip_and_strip(f)).where('for_review IS NOT TRUE').any?
+      end    
+      if self.author.nil?
+        self.update_attributes(author: author) if author
+      end  
+      author
+    end
+
     def check_title(fields)
       if self.author && fields.length == 2 && self.title.nil?
         self.update_attributes(title: (fields.first == self.author ? fields.last : fields.first))