Class: Wunderbar::Asset
- Inherits:
-
Object
- Object
- Wunderbar::Asset
- Defined in:
- lib/wunderbar/asset.rb
Class Attribute Summary collapse
-
.path ⇒ Object
URI path prepended to individual asset path.
-
.root ⇒ Object
location where the asset directory is to be found/placed.
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
asset contents.
-
#path ⇒ Object
readonly
asset file location.
Class Method Summary collapse
- .clear ⇒ Object
- .content_type_for(path) ⇒ Object
- .css(options) ⇒ Object
- .declarations(parent, prefix) ⇒ Object
- .script(options) ⇒ Object
Instance Method Summary collapse
-
#initialize(options) ⇒ Asset
constructor
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.
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() source = [:file] || __FILE__ @contents = [:contents] [:name] ||= File.basename([:file]) if source if [:name] @path = [:name] dest = File.(@path, Asset.root) if not File.exist?(dest) or File.mtime(dest) < File.mtime(source) begin FileUtils.mkdir_p File.dirname(dest) if [: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
.path ⇒ Object
URI path prepended to individual asset path
18 19 20 |
# File 'lib/wunderbar/asset.rb', line 18 def path @path end |
.root ⇒ Object
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
#contents ⇒ Object (readonly)
asset contents
28 29 30 |
# File 'lib/wunderbar/asset.rb', line 28 def contents @contents end |
#path ⇒ Object (readonly)
asset file location
25 26 27 |
# File 'lib/wunderbar/asset.rb', line 25 def path @path end |
Class Method Details
.clear ⇒ Object
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() @@stylesheets << self.new() 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() @@scripts << self.new() end |