Class: RemoteFiles::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config = {}) ⇒ Configuration

Returns a new instance of Configuration.



5
6
7
8
9
10
# File 'lib/remote_files/configuration.rb', line 5

def initialize(name, config = {})
  @name       = name
  @stores     = []
  @stores_map = {}
  from_hash(config)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_store(store_identifier, options = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/remote_files/configuration.rb', line 53

def add_store(store_identifier, options = {}, &block)
  store = (options[:class] || FogStore).new(store_identifier)
  block.call(store) if block_given?

  store[:read_only] = options[:read_only] if options.key?(:read_only)

  if options[:primary]
    @stores.unshift(store)
  else
    @stores << store
  end

  @stores_map[store_identifier] = store
end

#clearObject



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

def clear
  @stores.clear
  @stores_map.clear
end

#configured?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/remote_files/configuration.rb', line 68

def configured?
  !@stores.empty?
end

#delete!(file) ⇒ Object



124
125
126
# File 'lib/remote_files/configuration.rb', line 124

def delete!(file)
  RemoteFiles.delete_file(file)
end

#delete_now!(file) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/remote_files/configuration.rb', line 128

def delete_now!(file)
  exceptions = []
  stores = file.read_write_stores

  raise "No stores configured" if stores.empty?

  stores.each do |store|
    begin
      store.delete!(file.identifier)
    rescue NotFoundError => e
      exceptions << e
    end
  end

  if exceptions.size == stores.size # they all failed
    raise exceptions.first
  end

  true
end

#file_from_url(url, options = {}) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/remote_files/configuration.rb', line 158

def file_from_url(url, options = {})
  stores.each do |store|
    file = store.file_from_url(url, options.merge(:configuration => name))
    return file if file
  end

  nil
end

#from_hash(hash) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/remote_files/configuration.rb', line 25

def from_hash(hash)
  hash.each do |store_identifier, config|
    #symbolize_keys!
    cfg = {}
    config.each { |name, value| cfg[name.to_sym] = config[name] }
    config = cfg

    #camelize
    type = config[:type].gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } + 'Store'

    klass = RemoteFiles.const_get(type) rescue nil
    unless klass
      require "remote_files/#{config[:type]}_store"
      klass = RemoteFiles.const_get(type)
    end

    config.delete(:type)

    add_store(store_identifier.to_sym, :class => klass, :primary => !!config.delete(:primary)) do |store|
      config.each do |name, value|
        store[name] = value
      end
    end
  end

  self
end

#loggerObject



16
17
18
# File 'lib/remote_files/configuration.rb', line 16

def logger
  @logger ||= RemoteFiles.logger
end

#logger=(logger) ⇒ Object



12
13
14
# File 'lib/remote_files/configuration.rb', line 12

def logger=(logger)
  @logger = logger
end

#lookup_store(store_identifier) ⇒ Object



86
87
88
# File 'lib/remote_files/configuration.rb', line 86

def lookup_store(store_identifier)
  @stores_map[store_identifier.to_sym]
end

#primary_storeObject



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

def primary_store
  stores.first
end

#read_only_storesObject



78
79
80
# File 'lib/remote_files/configuration.rb', line 78

def read_only_stores
  stores.select {|s| s.read_only?}
end

#read_write_storesObject



82
83
84
# File 'lib/remote_files/configuration.rb', line 82

def read_write_stores
  stores.reject {|s| s.read_only?}
end

#store!(file) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/remote_files/configuration.rb', line 116

def store!(file)
  store_once!(file) unless file.stored?

  RemoteFiles.synchronize_stores(file) unless file.stored_everywhere?

  true
end

#store_once!(file) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/remote_files/configuration.rb', line 94

def store_once!(file)
  return file.stored_in.first if file.stored?

  exception = nil

  read_write_stores.each do |store|
    begin
      stored = store.store!(file)
      file.stored_in << store.identifier
      break
    rescue ::RemoteFiles::Error => e
      file.logger.info(e) if file.logger
      file.errors.push(e) if file.errors
      exception = e
    end
  end

  raise exception unless file.stored?

  file.stored_in.first
end

#storesObject



72
73
74
75
76
# File 'lib/remote_files/configuration.rb', line 72

def stores
  raise "You need to configure add stores to the #{name} RemoteFiles configuration" unless configured?

  @stores
end

#synchronize!(file) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/remote_files/configuration.rb', line 149

def synchronize!(file)
  file.missing_stores.each do |store|
    next if store.read_only?

    store.store!(file)
    file.stored_in << store.identifier
  end
end