Class: Wunderbar::Asset
- Inherits:
-
Object
- Object
- Wunderbar::Asset
- Defined in:
- lib/wunderbar/asset.rb,
lib/wunderbar/react.rb
Constant Summary collapse
- @@cached_scripts =
{}
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.
-
.virtual ⇒ Object
don’t fall back to content if file doesn’t exist on disk.
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
asset contents.
-
#mtime ⇒ Object
readonly
asset modification time.
-
#options ⇒ Object
readonly
general options.
-
#path ⇒ Object
readonly
asset file location.
Class Method Summary collapse
- .clear ⇒ Object
- .content_type_for(path) ⇒ Object
- .convert(file) ⇒ Object
- .css(options) ⇒ Object
- .declarations(root, prefix) ⇒ Object
- .find(path) ⇒ Object
- .script(options) ⇒ Object
- .scripts ⇒ 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/wunderbar/asset.rb', line 72 def initialize() @options = source = [:file] || __FILE__ @contents = [:contents] [:name] ||= File.basename([:file]) if source if [:name] @mtime = File.mtime(source) @path = [:name] dest = File.(@path, Asset.root) if not File.exist?(dest) or File.mtime(dest) < @mtime 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 unless Asset.virtual @contents ||= File.read(source) end end end end |
Class Attribute Details
.path ⇒ Object
URI path prepended to individual asset path
19 20 21 |
# File 'lib/wunderbar/asset.rb', line 19 def path @path end |
.root ⇒ Object
location where the asset directory is to be found/placed
22 23 24 |
# File 'lib/wunderbar/asset.rb', line 22 def root @root end |
.virtual ⇒ Object
don’t fall back to content if file doesn’t exist on disk
25 26 27 |
# File 'lib/wunderbar/asset.rb', line 25 def virtual @virtual end |
Instance Attribute Details
#contents ⇒ Object (readonly)
asset contents
32 33 34 |
# File 'lib/wunderbar/asset.rb', line 32 def contents @contents end |
#mtime ⇒ Object (readonly)
asset modification time
35 36 37 |
# File 'lib/wunderbar/asset.rb', line 35 def mtime @mtime end |
#options ⇒ Object (readonly)
general options
38 39 40 |
# File 'lib/wunderbar/asset.rb', line 38 def @options end |
#path ⇒ Object (readonly)
asset file location
29 30 31 |
# File 'lib/wunderbar/asset.rb', line 29 def path @path end |
Class Method Details
.clear ⇒ Object
40 41 42 43 |
# File 'lib/wunderbar/asset.rb', line 40 def self.clear @@scripts = [] @@stylesheets = [] end |
.content_type_for(path) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/wunderbar/asset.rb', line 45 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) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/wunderbar/react.rb', line 19 def self.convert(file) cached = @@cached_scripts[file] return cached if cached and cached.uptodate? return nil unless File.exist? file @@cached_scripts[file] = Ruby2JS.convert(File.read(file), file: file) end |
.css(options) ⇒ Object
104 105 106 |
# File 'lib/wunderbar/asset.rb', line 104 def self.css() @@stylesheets << self.new() 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) if before 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
55 56 57 58 |
# File 'lib/wunderbar/asset.rb', line 55 def self.find(path) (@@scripts.find {|script| script.path == path}) || (@@stylesheets.find {|script| script.path == path}) end |
.script(options) ⇒ Object
100 101 102 |
# File 'lib/wunderbar/asset.rb', line 100 def self.script() @@scripts << self.new() end |
.scripts ⇒ Object
108 109 110 |
# File 'lib/wunderbar/asset.rb', line 108 def self.scripts @@scripts end |