Class: Libis::Format::YamlLoader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/libis/format/yaml_loader.rb

Instance Method Summary collapse

Instance Method Details

#load_formats(file_or_hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/libis/format/yaml_loader.rb', line 28

def load_formats(file_or_hash)
  hash = file_or_hash.is_a?(Hash) ? file_or_hash : YAML::load_file(file_or_hash)
  hash.each do |category, format_list|
    format_list.each do |format_name, format_info|
      format_info.symbolize_keys!
      format_name = format_name.to_sym
      new_info = Libis::Format::Info.new(
          name: format_name,
          category: category.to_sym,
          description: format_info[:NAME],
          puids: format_info[:PUID]&.strip&.split(/[\s,]+/)&.map { |v| v.strip } || [],
          mimetypes: format_info[:MIME]&.strip&.split(/[\s,]+/)&.map(&:strip) || [],
          extensions: format_info[:EXTENSIONS]&.strip&.split(/[\s,]+/)&.map { |v| v.strip } || []
      )
      if (old_info = database[format_name])
        new_info = Libis::Format::Info.new(
            name: format_name,
            category: category.to_sym,
            description: new_info.description.blank? ? old_info.description : new_info.description,
            puids: (old_info.puids + new_info.puids).uniq,
            mimetypes: (old_info.mimetypes + new_info.mimetypes).uniq,
            extensions: (old_info.extensions + new_info.extensions).uniq
        )
      end
      database[format_name] = new_info
    end
  end

end

#query(key, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/libis/format/yaml_loader.rb', line 11

def query(key, value)
  case key.to_s.downcase.to_sym
  when :name
    return [database[value.to_s.upcase.to_sym]]
  when :category
    database.find_all { |_, info| info.category == value.to_s.upcase.to_sym }
  when :puid
    database.find_all { |_, info| info.puids.include?(value) }
  when :mimetype
    database.find_all { |_, info| info.mimetypes.include?(value) }
  when :extension
    database.find_all { |_, info| info.extensions.include?(value) }
  else
    return []
  end.map(&:last)
end