Class: Fuse::Document::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/fuse/document/asset.rb,
lib/fuse/document/asset/font.rb,
lib/fuse/document/asset_types.rb,
lib/fuse/document/asset/java_script.rb,
lib/fuse/document/asset/style_sheet.rb

Direct Known Subclasses

Font, Html, Image, JavaScript, StyleSheet, Xml, Xsl

Defined Under Namespace

Modules: HasDependents Classes: Font, Html, Image, JavaScript, StyleSheet, Xml, Xsl

Constant Summary collapse

TYPES =
{
    css:    StyleSheet,
    scss:   StyleSheet::Sass,
    sass:   StyleSheet::Sass,
    js:     JavaScript,
    coffee: JavaScript::Coffee,
    jpg:    Image,
    jpeg:   Image,
    png:    Image,
    gif:    Image,
    svg:    Image,
    ico:    Image,
    ttf:    Font,
    woff:   Font,
    eot:    Font,
    otf:    Font,
    xml:    Xml,
    xsl:    Xsl,
    htm:    Html,
    html:   Html
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, root) ⇒ Asset

Returns a new instance of Asset.



23
24
25
26
# File 'lib/fuse/document/asset.rb', line 23

def initialize(path, root)
  @root = root
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/fuse/document/asset.rb', line 21

def path
  @path
end

Class Method Details

.[](dir) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fuse/document/asset.rb', line 5

def self.[](dir)
  assets = Fuse::Document::AssetCollection.new
  Dir[File.join dir, '**/*.*'].each do |full_path|
    asset = self.for(full_path[dir.length..-1], dir)
    assets << asset if asset
  end
  assets
end

.for(path, root = Dir.pwd) ⇒ Object



14
15
16
17
18
19
# File 'lib/fuse/document/asset.rb', line 14

def self.for(path, root = Dir.pwd)
  full_path = File.join root, path
  return unless File.exists? full_path
  type = TYPES[(File.extname(path)[1..-1] || '').to_sym]
  (@cache ||= {})[File.expand_path full_path] ||= type.new(path, root) if type
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/fuse/document/asset.rb', line 48

def call(env)
  if filter?
    body = filter
    [200, {'Content-Type' => type, 'Content-Length' => body.length.to_s}, [body]]
  else
    Rack::File.new(@root).call(env)
  end
end

#extensionObject



68
69
70
# File 'lib/fuse/document/asset.rb', line 68

def extension
  @extension ||= File.extname(path).downcase[1..-1]
end

#filter?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/fuse/document/asset.rb', line 44

def filter?
  respond_to? :filter
end

#filteredObject



40
41
42
# File 'lib/fuse/document/asset.rb', line 40

def filtered
  filter? ? filter : raw
end

#full_pathObject



28
29
30
# File 'lib/fuse/document/asset.rb', line 28

def full_path
  @full_path ||= File.join @root, @path
end

#rawObject



36
37
38
# File 'lib/fuse/document/asset.rb', line 36

def raw
  File.open(full_path, 'rb') { |f| f.read }
end

#relative_pathObject



32
33
34
# File 'lib/fuse/document/asset.rb', line 32

def relative_path
  @relative_path ||= path.sub(%r`^[\\/]`, '')
end

#to_datauri(compress = false) ⇒ Object



61
62
63
64
65
66
# File 'lib/fuse/document/asset.rb', line 61

def to_datauri(compress = false)
  'data:%s;base64,%s' % [
      type,
      Base64.strict_encode64(compress && respond_to?(:compress) ? self.compress : raw)
  ]
end

#typeObject



57
58
59
# File 'lib/fuse/document/asset.rb', line 57

def type
  @type ||= Rack::Mime.mime_type('.' + extension)
end