Class: MimeMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/store/digest/object.rb

Class Method Summary collapse

Class Method Details

.ancestor_types(type) ⇒ Object



17
18
19
# File 'lib/store/digest/object.rb', line 17

def self.ancestor_types type
  parents(type).map { |t| ancestors(t) }.flatten.uniq
end

.binary?(thing) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/store/digest/object.rb', line 23

def self.binary? thing
  sample = nil

  # get some stuff out of the IO or get a substring
  if %i[tell seek read].all? { |m| thing.respond_to? m }
    pos = thing.tell
    thing.seek 0, 0
    sample = thing.read 1024
    thing.seek pos
  elsif thing.respond_to? :to_s
    sample = thing.to_s[0,1024]
  else
    raise ArgumentError, "Cannot sample an instance of {thing.class}"
  end

  # consider this to be 'binary' if empty
  return true if sample.nil? or sample.empty?
  # control codes minus ordinary whitespace
  /[\x0-\x8\xe-\x1f\x7f]/n.match?(sample) ? true : false
end

.default_type(thing) ⇒ Object



46
47
48
# File 'lib/store/digest/object.rb', line 46

def self.default_type thing
  new self.binary?(thing) ? 'application/octet-stream' : 'text/plain'
end

.parents(type) ⇒ Object



11
12
13
# File 'lib/store/digest/object.rb', line 11

def self.parents type
  TYPES.fetch(type, [nil,[]])[1].map { |t| new t }.uniq
end