Class: MimeMagic

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.binary?(thing) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/store/digest/object.rb', line 41

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



64
65
66
# File 'lib/store/digest/object.rb', line 64

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

.parents(type) ⇒ Object



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

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

Instance Method Details

#descendant_of?(type) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/store/digest/object.rb', line 35

def descendant_of? type
  lineage.map(&:type).include? type.to_s.downcase
end

#lineageObject



29
30
31
# File 'lib/store/digest/object.rb', line 29

def lineage
  ([self] + parents.map { |t| t.lineage }.flatten).uniq
end

#parentsObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/store/digest/object.rb', line 16

def parents
  out = TYPES.fetch(type.to_s.downcase, [nil, []])[1].map do |x|
    self.class.new x
  end
  # add this unless we're it
  out << self.class.new('application/octet-stream') if
    out.empty? and type.downcase != 'application/octet-stream'

  out.uniq
end