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
-
#asset ⇒ Object
readonly
Returns the value of attribute asset.
-
#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.
-
#mark_created ⇒ Object
readonly
Returns the value of attribute mark_created.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(definitions, asset, only: nil, except: nil, lazy: false, mark_created: :not_set) ⇒ 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, asset, only: nil, except: nil, lazy: false, mark_created: :not_set) ⇒ 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.
29 30 31 32 33 34 35 36 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 29 def initialize(definitions, asset, only:nil, except:nil, lazy: false, mark_created: :not_set) @definitions = definitions @asset = asset @only = only && Array(only) @except = except && Array(except) @lazy = !!lazy @mark_created = mark_created.nil? ? (only.nil? && except.nil?) : !! mark_created end |
Instance Attribute Details
#asset ⇒ Object (readonly)
Returns the value of attribute asset.
3 4 5 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 3 def asset @asset end |
#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 |
#mark_created ⇒ Object (readonly)
Returns the value of attribute mark_created.
3 4 5 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 3 def mark_created @mark_created 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 |
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 |
# File 'app/models/kithe/asset/derivative_creator.rb', line 38 def call return unless asset.file.present? # if no file, can't create derivatives definitions_to_create = applicable_definitions if lazy existing_derivative_keys = asset.derivatives.collect(&:key).collect(&:to_s) definitions_to_create.reject! do |defn| existing_derivative_keys.include?(defn.key.to_s) end end return unless definitions_to_create.present? # Note, MAY make a superfluous copy and/or download of original file, ongoing # discussion https://github.com/shrinerb/shrine/pull/329#issuecomment-443615868 # https://github.com/shrinerb/shrine/pull/332 Shrine.with_file(asset.file) do |original_file| definitions_to_create.each do |defn| deriv_bytestream = defn.call(original_file: original_file, record: asset) if deriv_bytestream asset.update_derivative(defn.key, deriv_bytestream, storage_key: defn.storage_key) cleanup_returned_io(deriv_bytestream) end original_file.rewind end mark_derivatives_created! if mark_created end end |