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, Image, JavaScript, StyleSheet

Defined Under Namespace

Modules: HasDependents Classes: Font, Image, JavaScript, StyleSheet

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.



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

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.[](dir) ⇒ Object



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

def [](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

.const_missing(name) ⇒ Object



12
13
14
# File 'lib/fuse/document/asset_types.rb', line 12

def self.const_missing(name)
  (@anonymous_subclasses ||= {})[name] ||= Class.new(self)
end

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



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

def 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



52
53
54
55
56
57
58
59
# File 'lib/fuse/document/asset.rb', line 52

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



72
73
74
# File 'lib/fuse/document/asset.rb', line 72

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

#filter?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/fuse/document/asset.rb', line 48

def filter?
  respond_to? :filter
end

#filteredObject



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

def filtered
  filter? ? filter : raw
end

#full_pathObject



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

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

#rawObject



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

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

#relative_pathObject



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

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

#to_datauri(compressed = false) ⇒ Object



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

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

#typeObject



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

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