Class: Rack::Bundle::FileSystemStore

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/bundle/file_system_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir = Dir.tmpdir) ⇒ FileSystemStore

Returns a new instance of FileSystemStore.



6
7
8
# File 'lib/rack/bundle/file_system_store.rb', line 6

def initialize dir = Dir.tmpdir
  @dir = dir
end

Instance Attribute Details

#bundlesObject

Returns the value of attribute bundles.



4
5
6
# File 'lib/rack/bundle/file_system_store.rb', line 4

def bundles
  @bundles
end

#dirObject

Returns the value of attribute dir.



4
5
6
# File 'lib/rack/bundle/file_system_store.rb', line 4

def dir
  @dir
end

Instance Method Details

#add(bundle) ⇒ Object



21
22
23
24
25
# File 'lib/rack/bundle/file_system_store.rb', line 21

def add bundle
  File.open("#{dir}/rack-bundle-#{bundle.hash}.#{bundle.extension}", 'w') do |file|
    file << bundle.contents
  end    
end

#find_bundle_by_hash(hash) ⇒ Object



10
11
12
13
14
15
# File 'lib/rack/bundle/file_system_store.rb', line 10

def find_bundle_by_hash hash
  found = Dir["#{dir}/rack-bundle-#{hash}.*"]
  return nil unless found.any?
  type, contents = File.extname(found.first).sub(/^./, ''), File.read(File.join(dir, File.basename(found.first)))
  type == 'js' ? Rack::Bundle::JSBundle.new(contents) : Rack::Bundle::CSSBundle.new(contents)
end

#has_bundle?(bundle) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/rack/bundle/file_system_store.rb', line 17

def has_bundle? bundle
  File.exists? "#{dir}/rack-bundle-#{bundle.hash}.#{bundle.extension}"
end