Module: Indexer::Importer::FileImportation

Included in:
Indexer::Importer
Defined in:
lib/indexer/importer/file.rb

Overview

Import metadata from individual files.

Instance Method Summary collapse

Instance Method Details

#import(source) ⇒ Object

Files import procedure.



12
13
14
15
16
17
18
19
# File 'lib/indexer/importer/file.rb', line 12

def import(source)
  if File.directory?(source)
    load_directory(source)
    true
  else
    super(source) if defined?(super)
  end
end

#load_directory(folder) ⇒ Object

TODO:

Subdirectories are simply omitted. Maybe do otherwise in future?

Import files from a given directory. This will only import files that have a name corresponding to a metadata attribute, unless the file name is listed in the customs file within the directory.

However, files with an extension of .index will be loaded as YAML wholeclothe and not as a single attribute.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/indexer/importer/file.rb', line 31

def load_directory(folder)
  if File.directory?(folder)
    customs = read_customs(folder)

    files = Dir[File.join(folder, '*')]

    files.each do |file|
      next if File.directory?(file)
      name = File.basename(file).downcase
      next load_yaml(file) if File.extname(file) == '.index'
      next load_field_file(file) if customs.include?(name)
      next load_field_file(file) if .attributes.include?(name.to_sym)
    end
  end
end

#load_field_file(file) ⇒ Object

Import a field setting from a file.

TODO: Ultimately support JSON and maybe other types, and possibly use mime-types library to recognize them.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/indexer/importer/file.rb', line 53

def load_field_file(file)
  if File.directory?(file)
    # ...
  else
    case File.extname(file).downcase
    when '.yaml', '.yml'
      name = File.basename(file).downcase
      name = name.chomp('.yaml').chomp('.yml')
      [name] = YAML.load_file(file)
      # TODO: should yaml files with explict extension by merged instead?
      #metadata.merge!(YAML.load_file(file))
    when '.text', '.txt'
      name = File.basename(file).downcase
      name = name.chomp('.text').chomp('.txt')
      text = File.read(file)
      [name] = text.strip
    else
      text = File.read(file)
      if /\A---/ =~ text
        name = File.basename(file).downcase
        [name] = YAML.load(text)
      else
        name = File.basename(file).downcase
        [name] = text.strip
      end
    end
  end
end

#read_customs(folder) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/indexer/importer/file.rb', line 83

def read_customs(folder)
  list = []
  file = Dir[File.join(folder, 'customs{,.*}')].first
  if file
    if %w{.yaml .yml}.include?(File.extname(file))
      list = YAML.load_file(file) || []
      raise TypeError, "index: customs is not a array" unless Array === list
    else
      text = File.read(file)
      if yaml?(text)
        list = YAML.load_file(file) || []
        raise TypeError, "index: customs is not a array" unless Array === list
      else
        list = text.split("\n")
        list = list.collect{ |pattern| pattern.strip  }
        list = list.reject { |pattern| pattern.empty? }
        list = list.collect{ |pattern| Dir[File.join(folder, pattern)] }.flatten
      end
    end
  end
  return list
end