Class: Kithe::Asset::DerivativeCreator
- Inherits:
-
Object
- Object
- Kithe::Asset::DerivativeCreator
- Defined in:
- app/models/kithe/asset/derivative_creator.rb
Overview
Creates derivatives from definitions stored on an Asset class
Instance Attribute Summary collapse
-
#definitions ⇒ Object
readonly
Returns the value of attribute definitions.
-
#except ⇒ Object
readonly
Returns the value of attribute except.
-
#lazy ⇒ Object
readonly
Returns the value of attribute lazy.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
-
#shrine_attacher ⇒ Object
readonly
Returns the value of attribute shrine_attacher.
-
#source_io ⇒ Object
readonly
Returns the value of attribute source_io.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(definitions, source_io:, shrine_attacher:, only: nil, except: nil, lazy: false) ⇒ DerivativeCreator
constructor
A helper class that provides the implementation for Kithe::Asset#create_derivatives, normally only expected to be called from there.
Constructor Details
#initialize(definitions, source_io:, shrine_attacher:, only: nil, except: nil, lazy: false) ⇒ DerivativeCreator
A helper class that provides the implementation for Kithe::Asset#create_derivatives, normally only expected to be called from there.
Creates derivatives according to derivative definitions. Normally any definition with ‘default_create` true, but that can be changed with `only:` and `except:` params, which take arrays of definition keys.
Bytestream returned by a derivative definition block will be closed AND unlinked (deleted) if it is a File or Tempfile object.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 25 def initialize(definitions, source_io:, shrine_attacher:, only:nil, except:nil, lazy: false) @definitions = definitions @source_io = source_io @shrine_attacher = shrine_attacher @only = only && Array(only).collect(&:to_sym) @except = except && Array(except).collect(&:to_sym) @lazy = !!lazy unless shrine_attacher.kind_of?(Shrine::Attacher) raise ArgumentError.new("expect second arg Shrine::Attacher not #{shrine_attacher.class}") end end |
Instance Attribute Details
#definitions ⇒ Object (readonly)
Returns the value of attribute definitions.
3 4 5 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 3 def definitions @definitions end |
#except ⇒ Object (readonly)
Returns the value of attribute except.
3 4 5 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 3 def except @except end |
#lazy ⇒ Object (readonly)
Returns the value of attribute lazy.
3 4 5 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 3 def lazy @lazy end |
#only ⇒ Object (readonly)
Returns the value of attribute only.
3 4 5 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 3 def only @only end |
#shrine_attacher ⇒ Object (readonly)
Returns the value of attribute shrine_attacher.
3 4 5 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 3 def shrine_attacher @shrine_attacher end |
#source_io ⇒ Object (readonly)
Returns the value of attribute source_io.
3 4 5 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 3 def source_io @source_io end |
Instance Method Details
#call ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 38 def call return unless shrine_attacher.file.present? # if no file, can't create derivatives definitions_to_create = applicable_definitions if lazy existing_derivative_keys = shrine_attacher.derivatives.keys definitions_to_create.reject! do |defn| existing_derivative_keys.include?(defn.key) end end return {} unless definitions_to_create.present? derivatives = {} # Make sure we have this as a local file, because many processors # require it, for instance for shelling out to command line. # This might trigger a download, but note we are only doing it if we have # `definitions_to_create`, otherwise we already returned. shrine_attacher.shrine_class.with_file(source_io) do |source_io_as_file| definitions_to_create.each do |defn| deriv_bytestream = defn.call(original_file: source_io_as_file, attacher: shrine_attacher) if deriv_bytestream derivatives[defn.key] = deriv_bytestream end # may not need this but it doesn't hurt... source_io.rewind end end derivatives end |