Class: Kithe::Asset::DerivativeDefinition
- Inherits:
-
Object
- Object
- Kithe::Asset::DerivativeDefinition
- Defined in:
- app/models/kithe/asset/derivative_definition.rb
Overview
A definition of a derivative creation routine, this is intended to be an internal class, it’s what’s created when you call Kithe::Asset#define_derivative
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#default_create ⇒ Object
readonly
Returns the value of attribute default_create.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#proc ⇒ Object
readonly
Returns the value of attribute proc.
-
#storage_key ⇒ Object
readonly
Returns the value of attribute storage_key.
Instance Method Summary collapse
-
#applies_to_content_type?(original_content_type) ⇒ Boolean
Do content-type restrictions defined for this definition match a given asset?.
-
#call(original_file:, attacher:) ⇒ Hash
Add_metadata hash of metadata to add to derivative on storage.
-
#initialize(key:, proc:, content_type: nil, default_create: true) ⇒ DerivativeDefinition
constructor
A new instance of DerivativeDefinition.
Constructor Details
#initialize(key:, proc:, content_type: nil, default_create: true) ⇒ DerivativeDefinition
Returns a new instance of DerivativeDefinition.
5 6 7 8 9 10 |
# File 'app/models/kithe/asset/derivative_definition.rb', line 5 def initialize(key:, proc:, content_type: nil, default_create: true) @key = key.to_sym @content_type = content_type @default_create = default_create @proc = proc end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
4 5 6 |
# File 'app/models/kithe/asset/derivative_definition.rb', line 4 def content_type @content_type end |
#default_create ⇒ Object (readonly)
Returns the value of attribute default_create.
4 5 6 |
# File 'app/models/kithe/asset/derivative_definition.rb', line 4 def default_create @default_create end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
4 5 6 |
# File 'app/models/kithe/asset/derivative_definition.rb', line 4 def key @key end |
#proc ⇒ Object (readonly)
Returns the value of attribute proc.
4 5 6 |
# File 'app/models/kithe/asset/derivative_definition.rb', line 4 def proc @proc end |
#storage_key ⇒ Object (readonly)
Returns the value of attribute storage_key.
4 5 6 |
# File 'app/models/kithe/asset/derivative_definition.rb', line 4 def storage_key @storage_key end |
Instance Method Details
#applies_to_content_type?(original_content_type) ⇒ Boolean
Do content-type restrictions defined for this definition match a given asset?
41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/kithe/asset/derivative_definition.rb', line 41 def applies_to_content_type?(original_content_type) return true if content_type.nil? return true if content_type == original_content_type return false if original_content_type.nil? return true if (content_type.kind_of?(Array) && content_type.include?(original_content_type)) content_type == original_content_type.sub(%r{/.+\Z}, '') end |
#call(original_file:, attacher:) ⇒ Hash
Returns add_metadata hash of metadata to add to derivative on storage.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/kithe/asset/derivative_definition.rb', line 13 def call(original_file:,attacher:) = {} kwargs = {} if proc_accepts_keyword?(:attacher) kwargs[:attacher] = attacher end if proc_accepts_keyword?(:add_metadata) kwargs[:add_metadata] = end return_val = if kwargs.present? proc.call(original_file, **kwargs) else proc.call(original_file) end # Save in context to later write to actual stored derivative metadata if .present? attacher.context[:add_metadata] ||= {} attacher.context[:add_metadata][key] = end return_val end |