Class: Saviour::Uploader::ProcessorsRunner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader) ⇒ ProcessorsRunner

Returns a new instance of ProcessorsRunner.



8
9
10
# File 'lib/saviour/uploader/processors_runner.rb', line 8

def initialize(uploader)
  @uploader = uploader
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



5
6
7
# File 'lib/saviour/uploader/processors_runner.rb', line 5

def contents
  @contents
end

#fileObject



16
17
18
# File 'lib/saviour/uploader/processors_runner.rb', line 16

def file
  @file ||= Tempfile.new([SecureRandom.hex, ::File.extname(filename)]).tap { |x| x.binmode }
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

Instance Method Details

#advance!(processor, previous_type) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/saviour/uploader/processors_runner.rb', line 32

def advance!(processor, previous_type)
  if processor[:type] != previous_type
    if processor[:type] == :memory
      self.contents = ::File.read(file)
    else
      file.rewind
      file.write(contents)
      file.flush
      file.rewind
    end
  end
end

#matching_processorsObject



12
13
14
# File 'lib/saviour/uploader/processors_runner.rb', line 12

def matching_processors
  @uploader.class.processors
end

#run!(content_data, initial_filename) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/saviour/uploader/processors_runner.rb', line 65

def run!(content_data, initial_filename)
  self.contents = content_data
  self.filename = initial_filename
  previous_type = :memory

  matching_processors.each do |processor|
    advance!(processor, previous_type)

    run_processor(processor)
    previous_type = processor[:type]
  end

  if previous_type == :file
    file.rewind
    self.contents = ::File.read(file)
  end

  file.delete

  [contents, filename]
end

#run_method_or_block(method_or_block, opts, data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/saviour/uploader/processors_runner.rb', line 20

def run_method_or_block(method_or_block, opts, data)
  if method_or_block.respond_to?(:call)
    @uploader.instance_exec(data, filename, &method_or_block)
  else
    if opts.empty?
      @uploader.send(method_or_block, data, filename)
    else
      @uploader.send(method_or_block, data, filename, opts)
    end
  end
end

#run_processor(processor) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/saviour/uploader/processors_runner.rb', line 45

def run_processor(processor)
  method_or_block = processor[:method_or_block]
  opts = processor[:opts]

  if processor[:type] == :memory
    result = run_method_or_block(method_or_block, opts, contents)

    self.contents = result[0]
    self.filename = result[1]

  else
    file.rewind

    result = run_method_or_block(method_or_block, opts, file)

    self.file = result[0]
    self.filename = result[1]
  end
end