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)


21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/kithe/asset/derivative_definition.rb', line 21

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:) ⇒ Object



12
13
14
15
16
17
18
# File 'app/models/kithe/asset/derivative_definition.rb', line 12

def call(original_file:,attacher:)
  if proc_accepts_keyword?(:attacher)
    proc.call(original_file, attacher: attacher)
  else
    proc.call(original_file)
  end
end