Class: RemoteFiles::MemoryStore

Inherits:
AbstractStore show all
Defined in:
lib/remote_files/memory_store.rb

Instance Attribute Summary

Attributes inherited from AbstractStore

#identifier

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractStore

#[]=, #file_from_url, #initialize, #options, #read_only?, #to_sym

Constructor Details

This class inherits a constructor from RemoteFiles::AbstractStore

Class Method Details

.clear!Object



14
15
16
17
18
19
20
21
# File 'lib/remote_files/memory_store.rb', line 14

def self.clear!
  RemoteFiles::CONFIGURATIONS.values.each do |config|
    next unless config.configured?
    config.stores.each do |store|
      store.clear! if store.is_a?(RemoteFiles::MemoryStore)
    end
  end
end

Instance Method Details

#clear!Object



10
11
12
# File 'lib/remote_files/memory_store.rb', line 10

def clear!
  data.clear
end

#copy_to_store!(file, target_store) ⇒ Object



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

def copy_to_store!(file, target_store)
  target_store.data[file.identifier] = data[file.identifier]
end

#dataObject



6
7
8
# File 'lib/remote_files/memory_store.rb', line 6

def data
  @data ||= {}
end

#delete!(identifier) ⇒ Object

Raises:



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

def delete!(identifier)
  raise NotFoundError, "#{identifier} not found in #{self.identifier} store" unless data.has_key?(identifier)
  data.delete(identifier)
end

#directory_nameObject



55
56
57
# File 'lib/remote_files/memory_store.rb', line 55

def directory_name
  self.identifier.to_s
end

#files(prefix = '') ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/remote_files/memory_store.rb', line 43

def files(prefix = '')
  @data.reject do |identifier|
    !identifier.start_with? prefix
  end.map do |identifier, data|
    File.new(identifier,
             :content_type => data[:content_type],
             :last_update_ts => data[:last_update_ts],
             :stored_in => [self]
    )
  end
end

#retrieve!(identifier) ⇒ Object

Raises:



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

def retrieve!(identifier)
  raise NotFoundError, "#{identifier} not found in #{self.identifier} store" unless data.has_key?(identifier)

  File.new(identifier,
    :content      => data[identifier][:content],
    :content_type => data[identifier][:content_type],
    :last_update_ts => data[identifier][:last_update_ts],
    :stored_in    => [self]
  )
end

#store!(file) ⇒ Object



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

def store!(file)
  content = file.content.respond_to?(:read) ? file.content.read : file.content
  data[file.identifier] = { :content => content, :content_type => file.content_type, :last_update_ts => file.last_update_ts}
end

#url(identifier) ⇒ Object



64
65
66
# File 'lib/remote_files/memory_store.rb', line 64

def url(identifier)
  "memory://#{self.identifier}/#{identifier}"
end

#url_matcherObject



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

def url_matcher
  @url_matcher ||= /memory:\/\/#{identifier}\/(.*)/
end