Class: Indexmap::Storage::Memory
- Inherits:
-
Object
- Object
- Indexmap::Storage::Memory
- Defined in:
- lib/indexmap/storage/memory.rb
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
"application/xml"
Instance Method Summary collapse
- #delete(filename) ⇒ Object
- #exist?(filename) ⇒ Boolean
-
#initialize(files = [], public_url: nil) ⇒ Memory
constructor
A new instance of Memory.
- #list(prefix: nil, suffix: nil) ⇒ Object
- #public_url(filename) ⇒ Object
- #read(filename) ⇒ Object
- #write(filename, body, content_type: DEFAULT_CONTENT_TYPE) ⇒ Object
Constructor Details
#initialize(files = [], public_url: nil) ⇒ Memory
Returns a new instance of Memory.
10 11 12 13 14 15 16 |
# File 'lib/indexmap/storage/memory.rb', line 10 def initialize(files = [], public_url: nil) @files = {} @public_url_base = public_url Array(files).each do |file| write(file.filename, file.body, content_type: file.content_type) end end |
Instance Method Details
#delete(filename) ⇒ Object
42 43 44 |
# File 'lib/indexmap/storage/memory.rb', line 42 def delete(filename) files.delete(normalize_filename(filename)) end |
#exist?(filename) ⇒ Boolean
31 32 33 |
# File 'lib/indexmap/storage/memory.rb', line 31 def exist?(filename) files.key?(normalize_filename(filename)) end |
#list(prefix: nil, suffix: nil) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/indexmap/storage/memory.rb', line 35 def list(prefix: nil, suffix: nil) files.keys.select do |filename| (prefix.nil? || filename.start_with?(prefix)) && (suffix.nil? || filename.end_with?(suffix)) end.sort end |
#public_url(filename) ⇒ Object
46 47 48 49 50 |
# File 'lib/indexmap/storage/memory.rb', line 46 def public_url(filename) return normalize_filename(filename) if public_url_base.to_s.strip.empty? URI.join("#{public_url_base.to_s.delete_suffix("/")}/", normalize_filename(filename)).to_s end |
#read(filename) ⇒ Object
27 28 29 |
# File 'lib/indexmap/storage/memory.rb', line 27 def read(filename) files.fetch(normalize_filename(filename)).body end |
#write(filename, body, content_type: DEFAULT_CONTENT_TYPE) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/indexmap/storage/memory.rb', line 18 def write(filename, body, content_type: DEFAULT_CONTENT_TYPE) normalized = normalize_filename(filename) files[normalized] = File.new( filename: normalized, body: body.to_s, content_type: content_type || DEFAULT_CONTENT_TYPE ) end |