Sha256: 040bd73b2fca66ffc9b27043faae8bfad386bc8d7bfcb520ecd267fc04515e7c

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

# 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
class Kithe::Asset::DerivativeDefinition
  attr_reader :key, :content_type, :default_create, :proc, :storage_key
  def initialize(key:, storage_key:, proc:, content_type: nil, default_create: true)
    @key = key
    @content_type = content_type
    @storage_key = storage_key
    @default_create = default_create
    @proc = proc
  end

  def call(original_file:,record:)
    if proc_accepts_record_keyword?
      proc.call(original_file, record: record)
    else
      proc.call(original_file)
    end
  end

  # Do content-type restrictions defined for this definition match a given asset?
  def applies_to?(asset)
    return true if content_type.nil?

    return true if content_type == asset.content_type

    return false if asset.content_type.nil?

    return true if (content_type.kind_of?(Array) && content_type.include?(asset.content_type))

    content_type == asset.content_type.sub(%r{/.+\Z}, '')
  end

  private

  def proc_accepts_record_keyword?
    proc.parameters.include?([:key, :record]) || proc.parameters.include?([:keyreq, :record])
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kithe-2.0.0.pre.alpha2 app/models/kithe/asset/derivative_definition.rb
kithe-2.0.0.pre.alpha1 app/models/kithe/asset/derivative_definition.rb
kithe-1.1.2 app/models/kithe/asset/derivative_definition.rb
kithe-1.1.1 app/models/kithe/asset/derivative_definition.rb
kithe-1.1.0 app/models/kithe/asset/derivative_definition.rb
kithe-1.0.0 app/models/kithe/asset/derivative_definition.rb