Class: Wunderbar::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/wunderbar/asset.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Asset

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


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
# File 'lib/wunderbar/asset.rb', line 55

def initialize(options)
  source = options[:file] || __FILE__
  @contents = options[:contents]

  options[:name] ||= File.basename(options[:file]) if source

  if options[:name]
    @path = options[:name]
    dest = File.expand_path(@path, Asset.root)

    if not File.exist?(dest) or File.mtime(dest) < File.mtime(source)
      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
        @contents ||= File.read(source)
      end
    end
  else
  end
end

Class Attribute Details

.pathObject

URI path prepended to individual asset path



18
19
20
# File 'lib/wunderbar/asset.rb', line 18

def path
  @path
end

.rootObject

location where the asset directory is to be found/placed



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

def root
  @root
end

Instance Attribute Details

#contentsObject (readonly)

asset contents



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

def contents
  @contents
end

#pathObject (readonly)

asset file location



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

def path
  @path
end

Class Method Details

.clearObject



30
31
32
33
# File 'lib/wunderbar/asset.rb', line 30

def self.clear
  @@scripts = []
  @@stylesheets = []
end

.content_type_for(path) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/wunderbar/asset.rb', line 35

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

.css(options) ⇒ Object



86
87
88
# File 'lib/wunderbar/asset.rb', line 86

def self.css(options)
  @@stylesheets << self.new(options)
end

.declarations(parent, prefix) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wunderbar/asset.rb', line 90

def self.declarations(parent, prefix)
  path = prefix.to_s + Asset.path
  nodes = []
  @@scripts.each do |script|
    if script.path
      nodes << Node.new(:script, src: "#{path}/#{script.path}")
    elsif script.contents
      nodes << ScriptNode.new(:script, script.contents)
    end
  end

  @@stylesheets.each do |stylesheet|
    if stylesheet.path
      nodes << Node.new(:link, rel: "stylesheet", type: "text/css",
        href: "#{path}/#{stylesheet.path}")
    elsif stylesheet.contents
      nodes << StyleNode.new(:style, stylesheet.contents)
    end
  end
  nodes.each {|node| node.parent = parent}
end

.script(options) ⇒ Object



82
83
84
# File 'lib/wunderbar/asset.rb', line 82

def self.script(options)
  @@scripts << self.new(options)
end