Class: Kithe::Asset::DerivativeDefinition

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_typeObject (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_createObject (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

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'app/models/kithe/asset/derivative_definition.rb', line 4

def key
  @key
end

#procObject (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_keyObject (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?

Returns:

  • (Boolean)


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.

Returns:

  • (Hash)

    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