module Paperclip::Storage::Eitheror

Public Class Methods

extended(base) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 4
def self.extended(base)
  base.instance_eval do
    base.options[:either][:enabled] = true if base.options[:either][:enabled].nil?
    @either = Attachment.new(base.name, base.instance, base.options.merge(base.options[:either]))
    @or = Attachment.new(base.name, base.instance, base.options.merge(base.options[:or]))

    define_aliases @either, base.options[:either].fetch(:alias, {})
    define_aliases @or, base.options[:or].fetch(:alias, {})
  end
end

Public Instance Methods

either_enabled?() click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 68
def either_enabled?
  callable_option(@either, :enabled)
end
flush_deletes() click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 44
def flush_deletes
  all_storages.each do |storage|
    storage.instance_variable_set(:@queued_for_delete, @queued_for_delete)
    storage.flush_deletes
  end

  @queued_for_delete = []
end
flush_writes() click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 36
def flush_writes
  storage = usable_storage
  storage.instance_variable_set(:@queued_for_write, @queued_for_write)
  storage.flush_writes

  @queued_for_write = {}
end
method_missing(method, *args) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 64
def method_missing method, *args
  usable_storage.send(method, *args)
end
path(style_name = default_style) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 28
def path(style_name = default_style)
  usable_storage.path(style_name)
end
queue_all_for_delete() click to toggle source
Calls superclass method
# File lib/paperclip/storage/eitheror.rb, line 59
def queue_all_for_delete
  queue_some_for_delete([:original, *styles.keys].uniq)
  super
end
queue_some_for_delete(*styles) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 53
def queue_some_for_delete(*styles)
  @queued_for_delete += styles.flatten.uniq.map do |style|
    all_storages.map { |s| s.path(style) if s.exists?(style) }
  end.flatten.compact
end
sync() click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 15
def sync
  @either.assign @or
  @either.save
end
syncable?() click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 24
def syncable?
  @or.exists?
end
synced?() click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 20
def synced?
  @either.exists?
end
url(style_name = default_style, options = {}) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 32
def url(style_name = default_style, options = {})
  usable_storage.url(style_name, options)
end

Private Instance Methods

all_storages() click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 79
def all_storages
  [@either, @or]
end
callable_option(attachment, key) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 74
def callable_option(attachment, key)
  option = attachment.options[key]
  option.respond_to?(:call) ? option.call(attachment) : option
end
create_method(target, name, &block) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 102
def create_method(target, name, &block)
  target.class.send(:define_method, name, &block)
end
define_aliases(target, aliases = {}) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 89
def define_aliases target, aliases = {}
  aliases.each do |name, value|
    block = is_callable?(value) ?
      ->(*args) { value.call(@either, @or, self, *args) } :
      ->(*args) { target.send(value, *args) }
    create_method(target, name, &block)
  end
end
is_callable?(o) click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 98
def is_callable?(o)
  o.respond_to?(:call)
end
usable_storage() click to toggle source
# File lib/paperclip/storage/eitheror.rb, line 83
def usable_storage
  return @or unless either_enabled?
  return @either if !@or.exists? || @either.exists?
  options[:autosync] && sync ? @either : @or
end