Commits

heliostatic  committed d3c1414

better adding of parent directory, refactored field cleaning

  • Participants
  • Parent commits 95e0b4b

Comments (0)

Files changed (1)

File lib/extensions/book_cleaner.rb

 
     def get_fields_from_path(path, ext)
       fields = File.basename(path, ext)
+      parent_directory = clean_field(File.dirname(self.fullpath).split('/').last)
       fields_array = split_fields(fields)
-      clean_fields(fields_array)
+      fields_array = clean_fields(fields_array)
+      fields_array = add_parent_directory(fields_array, parent_directory)
     end
 
     def split_fields(fields)
       fields
     end
 
+    def add_parent_directory(array_of_fields, parent_directory)
+      unless array_of_fields.include?(parent_directory) || array_of_fields.include?(flip_and_strip(parent_directory))
+        array_of_fields << parent_directory if parent_directory.include?(',')
+      end
+      array_of_fields
+    end
+
     def clean_fields(fields)
-      fields = fields.map { |f| f.gsub(/_\s/, ': ').gsub('_', ' ').titlecase.strip }
+      fields = fields.map { |f| clean_field(f) }
       if fields.size > 2
         fields = fields.delete_if { |f| f =~ /.*\d+.*/ }
       end
       fields
     end
 
+    def clean_field(field)
+      field.gsub(/_\s/, ': ').gsub('_', ' ').titlecase.strip
+    end
+
     def check_author(fields)
       if self.author.nil? || self.title.nil?
         fields.each_with_index do |f, i|