Module: Nametrainer::CollectionLoader

Defined in:
lib/nametrainer/collectionloader.rb

Overview

CollectionLoader.load loads a collection from a directory and returns a Collection instance.

It searches in the directory for files with the given file extensions (ignoring case) and for each file creates a Person instance, using the file name as the person’s name (extension is removed, underscore is converted to space) or the content of a corresponding ‘txt’ file, and the file name as the image attribute.

Class Method Summary collapse

Class Method Details

.load(args) ⇒ Object

Loads a collection. Expects an argument hash with:

:directory - collection directory :extensions - array of file extensions :collection_class - defaults to Collection :person_class - defaults to Person

Returns a Collection instance.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nametrainer/collectionloader.rb', line 27

def self.load(args)
  directory  = args[:directory]
  extensions = args[:extensions]
  collection_class = args[:collection_class] || Collection
  person_class = args[:person_class] || Person

  pattern = extensions.map {|ext| ext.downcase }.uniq.join('|')
  valid_extensions = /\.(#{pattern})\Z/i
  files = Dir.glob("#{directory}/*").grep(valid_extensions)
  persons = files.sort.map {|file| person_class.new(get_name(file), file) }

  collection_class.new(:persons => persons, :directory => directory)
end