Class: Libis::Format::TypeDatabaseImpl

Inherits:
Object
  • Object
show all
Includes:
Tools::Logger, Singleton
Defined in:
lib/libis/format/type_database_impl.rb

Instance Method Summary collapse

Instance Method Details

#export_csv(filename, **options) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/libis/format/type_database_impl.rb', line 96

def export_csv(filename, **options)
  headers = @types.values.each_with_object(Set.new) { |v, s| v.each_key { |k| s << k.to_s } }
  options[:headers] = headers.to_a
  CSV.open(filename, 'w', **options) do |csv|
    @types.each_value do |v|
      csv << CSV::Row.new(v.keys, v.values.map { |x| x.is_a?(Array) ? x.join(', ') : x })
    end
  end
end

#ext_infos(ext) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/libis/format/type_database_impl.rb', line 60

def ext_infos(ext)
  ext = ext.gsub(/^\./, '')
  @types.select do |_, v|
    v[:EXTENSIONS].include?(ext)
  rescue StandardError
    false
  end.values
end

#ext_types(ext) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/libis/format/type_database_impl.rb', line 69

def ext_types(ext)
  ext = ext.gsub(/^\./, '')
  @types.select do |_, v|
    v[:EXTENSIONS].include?(ext)
  rescue StandardError
    false
  end.keys
end

#group_types(group) ⇒ Object



22
23
24
25
26
# File 'lib/libis/format/type_database_impl.rb', line 22

def group_types(group)
  @types.select do |_, v|
    v[:GROUP] == group.to_sym
  end.keys
end

#groupsObject



92
93
94
# File 'lib/libis/format/type_database_impl.rb', line 92

def groups
  @types.values.map(&:dig.with(:GROUP)).uniq
end

#known_mime?(mime) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/libis/format/type_database_impl.rb', line 85

def known_mime?(mime)
  @types.each do |_, v|
    return true if v[:MIME]&.include? mime
  end
  false
end

#load_types(file_or_hash = {}, append = true) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/libis/format/type_database_impl.rb', line 106

def load_types(file_or_hash = {}, append = true)
  hash = file_or_hash.is_a?(Hash) ? file_or_hash : YAML.load_file(file_or_hash)
  # noinspection RubyResolve
  hash.each do |group, type_info|
    type_info.each do |type_name, info|
      type_key = type_name.to_sym
      info.symbolize_keys!
      info[:TYPE] = type_key
      info[:GROUP] = group.to_sym
      info[:MIME] = begin
        info[:MIME].strip.split(/[\s,]+/).map(&:strip)
      rescue StandardError
        []
      end
      info[:EXTENSIONS] = begin
        info[:EXTENSIONS].strip.split(/[\s,]+/).map(&:strip)
      rescue StandardError
        []
      end
      info[:PUID] = info[:PUID].strip.split(/[\s,]+/).map(&:strip) if info[:PUID]
      if @types.key?(type_key)
        warn 'Type %s already defined; merging with info from %s.', type_name, file_or_hash
        info.merge!(@types[type_key]) do |_, v_new, v_old|
          case v_old
          when Array
            append ? v_old + v_new : v_new + v_old
          when Hash
            append ? v_new.merge(v_old) : v_old.merge(v_new)
          else
            append ? v_old : v_new
          end
        end
      end
      @types[type_key] = info
    end
  end
end

#mime_infos(mime) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/libis/format/type_database_impl.rb', line 44

def mime_infos(mime)
  @types.select do |_, v|
    v[:MIME].include? mime
  rescue StandardError
    false
  end.values
end

#mime_types(mime) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/libis/format/type_database_impl.rb', line 52

def mime_types(mime)
  @types.select do |_, v|
    v[:MIME].include? mime
  rescue StandardError
    false
  end.keys
end

#puid_infos(puid) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/libis/format/type_database_impl.rb', line 28

def puid_infos(puid)
  @types.select do |_, v|
    v[:PUID].include? puid
  rescue StandardError
    false
  end.values
end

#puid_typeinfo(puid) ⇒ Object



78
79
80
81
82
83
# File 'lib/libis/format/type_database_impl.rb', line 78

def puid_typeinfo(puid)
  @types.each do |_, v|
    return v if v[:PUID]&.include?(puid)
  end
  nil
end

#puid_types(puid) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/libis/format/type_database_impl.rb', line 36

def puid_types(puid)
  @types.select do |_, v|
    v[:PUID].include? puid
  rescue StandardError
    false
  end.keys
end

#typeinfo(ftype) ⇒ Object



18
19
20
# File 'lib/libis/format/type_database_impl.rb', line 18

def typeinfo(ftype)
  @types[ftype.to_sym] || {}
end