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
19
20
# 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]
  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]
  @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

#last_update_tsObject (readonly)

Returns the value of attribute last_update_ts.



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

def last_update_ts
  @last_update_ts
end

#populate_stored_inObject (readonly)

Returns the value of attribute populate_stored_in.



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

def populate_stored_in
  @populate_stored_in
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



30
31
32
# File 'lib/remote_files/file.rb', line 30

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

Instance Method Details

#current_urlObject



70
71
72
73
74
75
76
# File 'lib/remote_files/file.rb', line 70

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

  return nil if prioritized_stores.empty?

  url(prioritized_stores[0])
end

#deleteObject



111
112
113
114
115
116
117
118
# File 'lib/remote_files/file.rb', line 111

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

#delete!Object



107
108
109
# File 'lib/remote_files/file.rb', line 107

def delete!
  configuration.delete!(self)
end

#delete_now!Object



120
121
122
# File 'lib/remote_files/file.rb', line 120

def delete_now!
  configuration.delete_now!(self)
end

#loggerObject



26
27
28
# File 'lib/remote_files/file.rb', line 26

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

#logger=(logger) ⇒ Object



22
23
24
# File 'lib/remote_files/file.rb', line 22

def logger=(logger)
  @logger = logger
end

#missing_storesObject



60
61
62
# File 'lib/remote_files/file.rb', line 60

def missing_stores
  configuration.stores - stores
end

#optionsObject



34
35
36
37
38
39
40
41
42
# File 'lib/remote_files/file.rb', line 34

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_storesObject



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

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

#retrieve!Object

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/remote_files/file.rb', line 78

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!Object



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

def store!
  configuration.store!(self)
end

#store_once!Object



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

def store_once!
  configuration.store_once!(self)
end

#stored?Boolean

Returns:

  • (Boolean)


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

def stored?
  !@stored_in.empty?
end

#stored_everywhere?Boolean

Returns:

  • (Boolean)


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

def stored_everywhere?
  missing_stores.empty?
end

#storesObject



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

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

#synchronize!Object



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

def synchronize!
  configuration.synchronize!(self)
end

#url(store_identifier = nil) ⇒ Object



64
65
66
67
68
# File 'lib/remote_files/file.rb', line 64

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