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


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

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

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

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

    if not File.exist?(dest) or File.mtime(dest) < @mtime
      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
  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

.virtualObject

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



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

def virtual
  @virtual
end

Instance Attribute Details

#contentsObject (readonly)

asset contents



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

def contents
  @contents
end

#mtimeObject (readonly)

asset modification time



34
35
36
# File 'lib/wunderbar/asset.rb', line 34

def mtime
  @mtime
end

#pathObject (readonly)

asset file location



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

def path
  @path
end

Class Method Details

.clearObject



36
37
38
39
# File 'lib/wunderbar/asset.rb', line 36

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

.content_type_for(path) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/wunderbar/asset.rb', line 41

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



98
99
100
# File 'lib/wunderbar/asset.rb', line 98

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

.declarations(root, prefix) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/wunderbar/asset.rb', line 112

def self.declarations(root, prefix)
  path = 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
        nodes << Node.new(:script, src: "#{path}/#{script.path}")
      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
        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}
    index = parent.children.index(before) || -1
    parent.children.insert(index, *nodes)
  end
end

.find(path) ⇒ Object



51
52
53
54
# File 'lib/wunderbar/asset.rb', line 51

def self.find(path)
  (@@scripts.find {|script| script.path == path}) ||
    (@@stylesheets.find {|script| script.path == path})
end

.script(options) ⇒ Object



94
95
96
# File 'lib/wunderbar/asset.rb', line 94

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

.scriptsObject



102
103
104
105
106
107
108
109
110
# File 'lib/wunderbar/asset.rb', line 102

def self.scripts
  @@scripts.map do |script|
    if script.contents
      script.contents
    elsif script.path
      File.read(File.expand_path(script.path, Asset.root))
    end
  end
end