class RemoteFiles::File

Attributes

configuration[R]
content[R]
content_type[R]
errors[R]
identifier[R]
last_update_ts[R]
populate_stored_in[R]
stored_in[R]

Public Class Methods

from_url(url) click to toggle source
# File lib/remote_files/file.rb, line 31
def self.from_url(url)
  RemoteFiles.default_configuration.file_from_url(url)
end
new(identifier, options = {}) click to toggle source
# File lib/remote_files/file.rb, line 5
def initialize(identifier, options = {})
  known_keys = [:identifier, :stored_in, :content_type, :configuration, :content, :populate_stored_in, :last_update_ts, :errors]
  known_keys.each do |key|
    options[key] ||= options.delete(key.to_s)
  end

  @identifier    = identifier
  @stored_in     = (options[:stored_in] || []).map(&:to_sym) # TODO: Refactor so that there are two classes: `File` and `FileCopy`
  @content       = options.delete(:content)
  @last_update_ts = options[:last_update_ts] || Time.now
  @content_type  = options[:content_type]
  @configuration = RemoteFiles::CONFIGURATIONS[(options[:configuration] || :default).to_sym]
  @logger        = options[:logger]
  @populate_stored_in = options[:populate_stored_in]
  @errors        = options[:errors]
  @options       = options
end

Public Instance Methods

current_url() click to toggle source
# File lib/remote_files/file.rb, line 71
def current_url
  prioritized_stores = configuration.stores.map(&:identifier) & @stored_in

  return nil if prioritized_stores.empty?

  url(prioritized_stores[0])
end
delete() click to toggle source
# File lib/remote_files/file.rb, line 112
def delete
  begin
    delete!
    true
  rescue RemoteFiles::Error => e
    false
  end
end
delete!() click to toggle source
# File lib/remote_files/file.rb, line 108
def delete!
  configuration.delete!(self)
end
delete_now!(parallel: false) click to toggle source
# File lib/remote_files/file.rb, line 121
def delete_now!(parallel: false)
  configuration.delete_now!(self, parallel: parallel)
end
logger() click to toggle source
# File lib/remote_files/file.rb, line 27
def logger
  @logger ||= configuration ? configuration.logger : RemoteFiles.logger
end
logger=(logger) click to toggle source
# File lib/remote_files/file.rb, line 23
def logger=(logger)
  @logger = logger
end
missing_stores() click to toggle source
# File lib/remote_files/file.rb, line 61
def missing_stores
  configuration.stores - stores
end
options() click to toggle source
# File lib/remote_files/file.rb, line 35
def options
  @options.merge(
    :identifier    => identifier,
    :stored_in     => stored_in,
    :content_type  => content_type,
    :configuration => configuration.name,
    :populate_stored_in => populate_stored_in
  )
end
read_write_stores() click to toggle source
# File lib/remote_files/file.rb, line 57
def read_write_stores
  stores.reject(&:read_only?)
end
retrieve!() click to toggle source
# File lib/remote_files/file.rb, line 79
def retrieve!
  stores.each do |store|
    begin
      file = store.retrieve!(identifier)
      next unless file
      @content      = file.content
      @content_type = file.content_type
      # :populate_stored_in is a boolean
      @stored_in = file.stored_in if @populate_stored_in
      return true
    rescue Error => e
    end
  end

  raise NotFoundError
end
store!() click to toggle source
# File lib/remote_files/file.rb, line 96
def store!
  configuration.store!(self)
end
store_once!() click to toggle source
# File lib/remote_files/file.rb, line 100
def store_once!
  configuration.store_once!(self)
end
stored?() click to toggle source
# File lib/remote_files/file.rb, line 45
def stored?
  !@stored_in.empty?
end
stored_everywhere?() click to toggle source
# File lib/remote_files/file.rb, line 49
def stored_everywhere?
  missing_stores.empty?
end
stores() click to toggle source
# File lib/remote_files/file.rb, line 53
def stores
  @stored_in.map { |store_id| configuration.lookup_store(store_id) }
end
synchronize!() click to toggle source
# File lib/remote_files/file.rb, line 104
def synchronize!
  configuration.synchronize!(self)
end
url(store_identifier = nil) click to toggle source
# File lib/remote_files/file.rb, line 65
def url(store_identifier = nil)
  store = store_identifier ? configuration.lookup_store(store_identifier) : configuration.primary_store
  return nil unless store
  store.url(identifier)
end