Class: Saviour::BaseUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/saviour/base_uploader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BaseUploader

Returns a new instance of BaseUploader.



6
7
8
# File 'lib/saviour/base_uploader.rb', line 6

def initialize(opts = {})
  @data = opts.fetch(:data, {})
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/saviour/base_uploader.rb', line 10

def method_missing(name, *args, &block)
  if @data.key?(name)
    @data[name]
  else
    super
  end
end

Class Method Details

.process(name = nil, opts = {}, type = :memory, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/saviour/base_uploader.rb', line 44

def process(name = nil, opts = {}, type = :memory, &block)
  if block_given?
    processors.push(method_or_block: name || block, type: type)
  else
    processors.push(method_or_block: name || block, type: type, opts: opts)
  end
end

.process_with_file(name = nil, opts = {}, &block) ⇒ Object



52
53
54
# File 'lib/saviour/base_uploader.rb', line 52

def process_with_file(name = nil, opts = {}, &block)
  process(name, opts, :file, &block)
end

.processorsObject



40
41
42
# File 'lib/saviour/base_uploader.rb', line 40

def processors
  @processors ||= []
end

.store_dir(name = nil, &block) ⇒ Object



57
58
59
# File 'lib/saviour/base_uploader.rb', line 57

def store_dir(name = nil, &block)
  store_dirs.push(name || block)
end

.store_dirsObject



36
37
38
# File 'lib/saviour/base_uploader.rb', line 36

def store_dirs
  @store_dirs ||= []
end

Instance Method Details

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/saviour/base_uploader.rb', line 18

def respond_to_missing?(name, *)
  @data.key?(name) || super
end

#write(contents, filename) ⇒ Object

Raises:

  • (RuntimeError)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/saviour/base_uploader.rb', line 22

def write(contents, filename)
  store_dir = Uploader::StoreDirExtractor.new(self).store_dir
  raise RuntimeError, "Please use `store_dir` before trying to write" unless store_dir

  if Config.processing_enabled
    contents, filename = Uploader::ProcessorsRunner.new(self).run!(contents, filename)
  end

  path = ::File.join(store_dir, filename)
  Config.storage.write(contents, path)
  path
end