Module: Sinatra::AssetPack::ClassMethods

Included in:
Sinatra::AssetPack
Defined in:
lib/sinatra/assetpack/class_methods.rb

Overview

Class methods that will be given to the Sinatra application.

Instance Method Summary collapse

Instance Method Details

#add_compressed_routes!Object

Add routes for the compressed versions



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sinatra/assetpack/class_methods.rb', line 24

def add_compressed_routes!
  assets.packages.each do |name, package|
    get package.route_regex do
      if defined?(settings.assets.app.clear_tilt_cache) && settings.assets.app.clear_tilt_cache
        AssetPack.clear_tilt_cache!(@template_cache, settings.assets.app)
      end

      mtime, contents = @template_cache.fetch(package.path) {
        [ package.mtime, package.minify ]
      }

      content_type package.type
      last_modified mtime
      assets_expires
      contents
    end
  end
end

#add_individual_routes!Object

Add the routes for the individual files.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sinatra/assetpack/class_methods.rb', line 44

def add_individual_routes!
  assets.served.each do |path, from|
    get %r{#{"^/#{path}/".squeeze('/')}(.*)$} do |file|
      fmt = File.extname(file)[1..-1]

      if file =~ /(.*)\.([a-f0-9]{32})\.(.*)/
        clean_file = "#{$1}.#{$3}"
        requested_hash = $2
      else
        clean_file = file
      end

      # Sanity checks
      pass unless AssetPack.supported_formats.include?(fmt)

      fn = asset_path_for(clean_file, from) or pass

      pass if settings.assets.ignored?("#{path}/#{clean_file}")

      # Send headers
      content_type fmt.to_sym
      last_modified File.mtime(fn)
      assets_expires

      format = File.extname(fn)[1..-1]

      if defined?(settings.assets.app.clear_tilt_cache) && settings.assets.app.clear_tilt_cache
        AssetPack.clear_tilt_cache!(@template_cache, settings.assets.app)
      end

      if AssetPack.supported_formats.include?(format)
        # Static file
        if fmt == 'css'
          # Matching static file format
          pass unless fmt == File.extname(fn)[1..-1]
          @template_cache.fetch(fn) { asset_filter_css File.read(fn) }
        else
          # It's a raw file, just send it
          send_file fn
        end
      else
        # Dynamic file
        not_found unless AssetPack.tilt_formats[format] == fmt

        @template_cache.fetch(fn) {
          settings.assets.fetch_dynamic_asset(fn) {
            out = render format.to_sym, File.read(fn), :filename => fn
            out = asset_filter_css(out)  if fmt == 'css'
            out
          }
        }
      end
    end
  end
end

#assets(&blk) ⇒ Object

Sets asset options, or gets them



6
7
8
9
10
11
# File 'lib/sinatra/assetpack/class_methods.rb', line 6

def assets(&blk)
  @options ||= Options.new(self, &blk)
  self.assets_initialize!  if block_given?

  @options
end

#assets_initialize!Object



13
14
15
16
17
18
19
20
21
# File 'lib/sinatra/assetpack/class_methods.rb', line 13

def assets_initialize!
  add_compressed_routes!
  add_individual_routes!

  # Cache the built files.
  if assets.prebuild && !reload_templates
    assets.cache! { |file| $stderr.write "** Building #{file}...\n" }
  end
end