Class: RemoteFiles::File

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_files/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, options = {}) ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/remote_files/file.rb', line 5

def initialize(identifier, options = {})
  known_keys = [:identifier, :stored_in, :content_type, :configuration, :content]
  known_keys.each do |key|
    options[key] ||= options.delete(key.to_s)
  end

  @identifier    = identifier
  @stored_in     = (options[:stored_in] || []).map(&:to_sym)
  @content       = options.delete(:content)
  @content_type  = options[:content_type]
  @configuration = RemoteFiles::CONFIGURATIONS[(options[:configuration] || :default).to_sym]
  @logger        = options[:logger]
  @options       = options
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



3
4
5
# File 'lib/remote_files/file.rb', line 3

def configuration
  @configuration
end

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/remote_files/file.rb', line 3

def content
  @content
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



3
4
5
# File 'lib/remote_files/file.rb', line 3

def content_type
  @content_type
end

#identifierObject (readonly)

Returns the value of attribute identifier.



3
4
5
# File 'lib/remote_files/file.rb', line 3

def identifier
  @identifier
end

#stored_inObject (readonly)

Returns the value of attribute stored_in.



3
4
5
# File 'lib/remote_files/file.rb', line 3

def stored_in
  @stored_in
end

Class Method Details

.from_url(url) ⇒ Object



28
29
30
# File 'lib/remote_files/file.rb', line 28

def self.from_url(url)
  RemoteFiles.default_configuration.file_from_url(url)
end

Instance Method Details

#current_urlObject



67
68
69
70
71
72
73
# File 'lib/remote_files/file.rb', line 67

def current_url
  prioritized_stores = configuration.stores.map(&:identifier) & @stored_in

  return nil if prioritized_stores.empty?

  url(prioritized_stores[0])
end

#deleteObject



106
107
108
109
110
111
112
113
# File 'lib/remote_files/file.rb', line 106

def delete
  begin
    delete!
    true
  rescue RemoteFiles::Error => e
    false
  end
end

#delete!Object



102
103
104
# File 'lib/remote_files/file.rb', line 102

def delete!
  configuration.delete!(self)
end

#delete_now!Object



115
116
117
# File 'lib/remote_files/file.rb', line 115

def delete_now!
  configuration.delete_now!(self)
end

#loggerObject



24
25
26
# File 'lib/remote_files/file.rb', line 24

def logger
  @logger ||= configuration ? configuration.logger : RemoteFiles.logger
end

#logger=(logger) ⇒ Object



20
21
22
# File 'lib/remote_files/file.rb', line 20

def logger=(logger)
  @logger = logger
end

#missing_storesObject



57
58
59
# File 'lib/remote_files/file.rb', line 57

def missing_stores
  configuration.stores - stores
end

#optionsObject



32
33
34
35
36
37
38
39
# File 'lib/remote_files/file.rb', line 32

def options
  @options.merge(
    :identifier    => identifier,
    :stored_in     => stored_in,
    :content_type  => content_type,
    :configuration => configuration.name
  )
end

#read_write_storesObject



53
54
55
# File 'lib/remote_files/file.rb', line 53

def read_write_stores
  stores.reject(&:read_only?)
end

#retrieve!Object

Raises:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/remote_files/file.rb', line 75

def retrieve!
  stores.each do |store|
    begin
      file = store.retrieve!(identifier)
      next unless file
      @content      = file.content
      @content_type = file.content_type
      return true
    rescue Error => e
    end
  end

  raise NotFoundError
end

#store!Object



90
91
92
# File 'lib/remote_files/file.rb', line 90

def store!
  configuration.store!(self)
end

#store_once!Object



94
95
96
# File 'lib/remote_files/file.rb', line 94

def store_once!
  configuration.store_once!(self)
end

#stored?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/remote_files/file.rb', line 41

def stored?
  !@stored_in.empty?
end

#stored_everywhere?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/remote_files/file.rb', line 45

def stored_everywhere?
  missing_stores.empty?
end

#storesObject



49
50
51
# File 'lib/remote_files/file.rb', line 49

def stores
  @stored_in.map { |store_id| configuration.lookup_store(store_id) }
end

#synchronize!Object



98
99
100
# File 'lib/remote_files/file.rb', line 98

def synchronize!
  configuration.synchronize!(self)
end

#url(store_identifier = nil) ⇒ Object



61
62
63
64
65
# File 'lib/remote_files/file.rb', line 61

def url(store_identifier = nil)
  store = store_identifier ? configuration.lookup_store(store_identifier) : configuration.primary_store
  return nil unless store
  store.url(identifier)
end