class Wunderbar::Asset

Attributes

path[RW]

URI path prepended to individual asset path

root[RW]

location where the asset directory is to be found/placed

virtual[RW]

don’t fall back to content if file doesn’t exist on disk

contents[R]

asset contents

mtime[R]

asset modification time

options[R]

general options

Public Class Methods

clear() click to toggle source
# File lib/wunderbar/asset.rb, line 79
def self.clear
  @@scripts = []
  @@stylesheets = []
end
content_type_for(path) click to toggle source
# File lib/wunderbar/asset.rb, line 84
def self.content_type_for(path)
  if @@scripts.any? {|script| script.path == path}
    'application/javascript'
  elsif @@stylesheets.any? {|script| script.path == path}
    'text/css'
  else
    'application/octet-stream'
  end
end
convert(file) click to toggle source
# File lib/wunderbar/render.rb, line 18
def self.convert(file)
  basename = file.sub(/-es5\.js\.rb$/, '.js.rb')
  options = {file: basename}
  options.merge!(eslevel: 5, strict: false) unless basename == file
  cached = @@cached_scripts[file]
  return cached if cached and cached.uptodate?
  return nil unless File.exist? basename
  @@cached_scripts[file] = Ruby2JS.convert(File.read(basename), options)
end
css(options) click to toggle source
# File lib/wunderbar/asset.rb, line 124
def self.css(options)
  @@stylesheets << self.new(options)
end
declarations(root, prefix) click to toggle source
# File lib/wunderbar/asset.rb, line 136
def self.declarations(root, prefix)
  base = prefix.to_s + Asset.path

  unless @@scripts.empty?
    before = root.at('script')
    if before
      before = before.parent while before.parent and 
        not %w(head body).include? before.parent.name.to_s
    end
    parent = (before ? before.parent : root.at('body')) || root

    nodes = []
    @@scripts.each do |script|
      if script.path
        path = script.path
        path = "#{base}/#{path}" unless path.start_with? '/'
        nodes << Node.new(:script, src: "#{path}?#{script.mtime.to_i}")
      elsif script.contents
        nodes << ScriptNode.new(:script, script.contents)
      end
    end

    nodes.each {|node| node.parent = parent}
    index = parent.children.index(before) || -1
    parent.children.insert(index, *nodes)
  end

  unless @@stylesheets.empty?
    before = root.at('link[rel=stylesheet]')
    if before
      before = before.parent while before.parent and 
        not %w(head body).include? before.parent.name.to_s
    end
    parent = (before ? before.parent : root.at('head')) || root

    nodes = []
    @@stylesheets.each do |stylesheet|
      if stylesheet.path
        path = stylesheet.path
        path = "#{base}/#{path}" unless path.start_with? '/'
        nodes << Node.new(:link, rel: "stylesheet", type: "text/css",
          href: "#{path}?#{stylesheet.mtime.to_i}")
      elsif stylesheet.contents
        nodes << StyleNode.new(:style, stylesheet.contents)
      end
    end

    nodes.each {|node| node.parent = parent}
    index = parent.children.index(before) || -1
    parent.children.insert(index, *nodes)
  end
end
find(path) click to toggle source
# File lib/wunderbar/asset.rb, line 94
def self.find(path)
  (@@scripts.find {|script| script.path == path}) ||
    (@@stylesheets.find {|script| script.path == path})
end
new(options) click to toggle source

Options: typically :name plus either :file or :contents

:name => name to be used for the asset
:file => source for the asset
:contents => contents of the asset
# File lib/wunderbar/asset.rb, line 112
def initialize(options)
  @options = options
  @contents = options[:contents]
  @path = nil

  options[:name] ||= File.basename(options[:file])
end
script(options) click to toggle source
# File lib/wunderbar/asset.rb, line 120
def self.script(options)
  @@scripts << self.new(options)
end
scripts() click to toggle source
# File lib/wunderbar/asset.rb, line 128
def self.scripts
  @@scripts
end
stylesheets() click to toggle source
# File lib/wunderbar/asset.rb, line 132
def self.stylesheets
  @@stylesheets
end

Public Instance Methods

path() click to toggle source

asset file location

# File lib/wunderbar/asset.rb, line 29
def path
  return @path if @path or @contents

  if @options[:name]
    source = @options[:file] || __FILE__
    @mtime = File.mtime(source)
    @path = @options[:name]

    # look for asset in site
    if ENV['DOCUMENT_ROOT']
      root = File.join(ENV['DOCUMENT_ROOT'], 'assets')
      dest = File.expand_path(@path, root)
      if File.exist?(dest) and File.size(dest) == File.size(source)
        @path = "/assets/#{@path}"
        return @path
      end
    end

    # look for asset in app
    dest = File.expand_path(@path, Asset.root)
    if File.exist?(dest) and File.size(dest) == File.size(source)
      return @path
    end

    # try to make one
    begin
      FileUtils.mkdir_p File.dirname(dest)
      if @options[:file]
        FileUtils.cp source, dest, :preserve => true
      else
        open(dest, 'w') {|file| file.write @contents}
      end
    rescue
      @path = nil unless Asset.virtual
      @contents ||= File.read(source)
    end
  end

  @path
end