Class: Rack::Bundle::DatabaseStore

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = ENV['DATABASE_URL']) ⇒ DatabaseStore

Returns a new instance of DatabaseStore.



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

def initialize url = ENV['DATABASE_URL']
  @db = Sequel.connect(url)
  create_table!
end

Instance Attribute Details

#bundlesObject

Returns the value of attribute bundles.



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

def bundles
  @bundles
end

#dbObject

Returns the value of attribute db.



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

def db
  @db
end

Instance Method Details

#add(bundle) ⇒ Object



18
19
20
21
22
23
# File 'lib/rack/bundle/database_store.rb', line 18

def add bundle
  return false if has_bundle? bundle
  @db[:rack_bundle].insert :contents => bundle.contents, 
    :hash => bundle.hash,
    :type => bundle.is_a?(Rack::Bundle::JSBundle) ? 'js' : 'css'
end

#find_bundle_by_hash(hash) ⇒ Object



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

def find_bundle_by_hash hash
  return nil unless result = @db[:rack_bundle].where(:hash => hash).first
  result[:type] == 'js' ? 
    Rack::Bundle::JSBundle.new(result[:contents]) :
    Rack::Bundle::CSSBundle.new(result[:contents])
end

#has_bundle?(bundle) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/rack/bundle/database_store.rb', line 25

def has_bundle? bundle
  not find_bundle_by_hash(bundle.hash).nil?
end