跳到內容 跳到搜尋

Active Storage 變體

一組可應用於 Blob 以建立變體的轉換。此類別透過 ActiveStorage::Blob#variant 方法公開,而且很少需要直接使用它。

如果確實需要直接使用它,將使用轉換雜湊進行實例化,其中金鑰是指令,而值是引數。範例:

ActiveStorage::Variation.new(resize_to_limit: [100, 100], colourspace: "b-w", rotate: "-90", saver: { trim: true })

這些選項與 ImageProcessing 指令直接對應。

方法
C
D
E
F
K
N
T
W

屬性

[R] transformations

類別公開方法

decode(key)

傳回一個 Variation 實例,其中包含由 encode 編碼的轉換。

# File activestorage/app/models/active_storage/variation.rb, line 35
def decode(key)
  new ActiveStorage.verifier.verify(key, purpose: :variation)
end

encode(transformations)

傳回 transformations 的簽署金鑰,可將其用於在 URL 或複合金鑰(如 ActiveStorage::Variant#key)中參考特定變體。

# File activestorage/app/models/active_storage/variation.rb, line 41
def encode(transformations)
  ActiveStorage.verifier.generate(transformations, purpose: :variation)
end

new(transformations)

# File activestorage/app/models/active_storage/variation.rb, line 46
def initialize(transformations)
  @transformations = transformations.deep_symbolize_keys
end

wrap(variator)

傳回一個 Variation 實例,該實例根據所提供的變異器建立。如果變異器是 Variation,則不加修改傳回。如果它是 String,則將其傳遞給 ActiveStorage::Variation.decode。否則,它會假設成轉換 Hash,並直接傳遞給建構函式。

# File activestorage/app/models/active_storage/variation.rb, line 23
def wrap(variator)
  case variator
  when self
    variator
  when String
    decode variator
  else
    new variator
  end
end

實例公開方法

content_type()

# File activestorage/app/models/active_storage/variation.rb, line 70
def content_type
  Marcel::MimeType.for(extension: format.to_s)
end

default_to(defaults)

# File activestorage/app/models/active_storage/variation.rb, line 50
def default_to(defaults)
  self.class.new transformations.reverse_merge(defaults)
end

摘要()

# File activestorage/app/models/active_storage/variation.rb, line 79
def digest
  OpenSSL::Digest::SHA1.base64digest Marshal.dump(transformations)
end

格式()

# File activestorage/app/models/active_storage/variation.rb, line 62
def format
  transformations.fetch(:format, :png).tap do |format|
    if Marcel::Magic.by_extension(format.to_s).nil?
      raise ArgumentError, "Invalid variant format (#{format.inspect})"
    end
  end
end

金鑰()

傳回所有變異經過 轉換 所實體化簽署的金鑰。

# File activestorage/app/models/active_storage/variation.rb, line 75
def key
  self.class.encode(transformations)
end

轉換(檔案, &區塊)

接受 檔案 物件,針對物件執行 轉換,並將轉換後的圖像儲存在暫存檔案中。

# File activestorage/app/models/active_storage/variation.rb, line 56
def transform(file, &block)
  ActiveSupport::Notifications.instrument("transform.active_storage") do
    transformer.transform(file, format: format, &block)
  end
end