Class: Alula::AttachmentProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/attachment_processor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ AttachmentProcessor

Returns a new instance of AttachmentProcessor.



9
10
11
12
13
# File 'lib/alula/attachment_processor.rb', line 9

def initialize(opts)
  @site = opts.delete(:site)
  
  @@lock = Mutex.new
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



7
8
9
# File 'lib/alula/attachment_processor.rb', line 7

def site
  @site
end

Class Method Details

.processorsObject



4
# File 'lib/alula/attachment_processor.rb', line 4

def self.processors; @@processors ||= {}; end

.register(name, klass) ⇒ Object



3
# File 'lib/alula/attachment_processor.rb', line 3

def self.register(name, klass); processors[name] = klass; end

Instance Method Details

#asset_name(name, *path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/alula/attachment_processor.rb', line 19

def asset_name(name, *path)
  path ||= []

  @@lock.synchronize do      
    md5 = Digest::MD5.hexdigest(name)
    asset_hash = md5[0..3]
    asset_name = File.join(path + [asset_hash]) + File.extname(name)
    until !mapping.key(asset_name) or mapping.key(asset_name) == name.downcase
      asset_hash = md5[0..(asset_hash.length)]
      asset_name = File.join(path + [asset_hash]) + File.extname(name)
    end
  
    mapping[name.downcase] = asset_name
  end
  
  mapping[name.downcase]
end

#get(item) ⇒ Object



37
38
39
40
41
42
# File 'lib/alula/attachment_processor.rb', line 37

def get(item)
  # Try to use cached processor for extension
  type = _type(item)
  options = site.config.attachments[type]
  self.processors[type].new(item, options: options, site: site, attachments: self)
end

#mappingObject



15
16
17
# File 'lib/alula/attachment_processor.rb', line 15

def mapping
  @mapping ||= {}
end

#processorsObject



5
# File 'lib/alula/attachment_processor.rb', line 5

def processors; self.class.processors; end