Class: Dassets::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dassets/file_store.rb

Direct Known Subclasses

NullFileStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileStore

Returns a new instance of FileStore.



10
11
12
13
# File 'lib/dassets/file_store.rb', line 10

def initialize(root)
  @root       = root
  @save_mutex = ::Mutex.new
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/dassets/file_store.rb', line 8

def root
  @root
end

Instance Method Details

#save(url_path, &block) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/dassets/file_store.rb', line 15

def save(url_path, &block)
  @save_mutex.synchronize do
    store_path(url_path).tap do |path|
      FileUtils.mkdir_p(File.dirname(path))
      File.open(path, "wb"){ |f| f.write(block.call) }
    end
  end
end

#store_path(url_path) ⇒ Object



24
25
26
# File 'lib/dassets/file_store.rb', line 24

def store_path(url_path)
  File.join(@root, url_path)
end