Class: Rack::Sprockets::Source
- Inherits:
-
Object
- Object
- Rack::Sprockets::Source
- Defined in:
- lib/rack/sprockets/source.rb
Constant Summary collapse
- PREFERRED_EXTENSIONS =
[:js]
- SECRETARY_DEFAULTS =
{ :expand_paths => true }
- YUI_OPTS =
{ :munge => true }
Instance Attribute Summary collapse
-
#js_resource ⇒ Object
readonly
Returns the value of attribute js_resource.
Instance Method Summary collapse
- #cache ⇒ Object
- #cache? ⇒ Boolean
- #compiled ⇒ Object (also: #to_js, #js)
- #compress? ⇒ Boolean
- #files ⇒ Object
-
#initialize(js_resource, options = {}) ⇒ Source
constructor
A new instance of Source.
- #secretary ⇒ Object
Constructor Details
#initialize(js_resource, options = {}) ⇒ Source
Returns a new instance of Source.
24 25 26 27 28 29 30 31 |
# File 'lib/rack/sprockets/source.rb', line 24 def initialize(js_resource, ={}) @js_resource = js_resource.gsub(/^\/+/, '') @compress = [:compress] @cache = [:cache] @folder = get_required_path(, :folder) @secretary = SECRETARY_DEFAULTS.merge([:secretary] || {}) end |
Instance Attribute Details
#js_resource ⇒ Object (readonly)
Returns the value of attribute js_resource.
22 23 24 |
# File 'lib/rack/sprockets/source.rb', line 22 def js_resource @js_resource end |
Instance Method Details
#cache ⇒ Object
39 40 41 |
# File 'lib/rack/sprockets/source.rb', line 39 def cache @cache end |
#cache? ⇒ Boolean
36 37 38 |
# File 'lib/rack/sprockets/source.rb', line 36 def cache? !@cache.nil? end |
#compiled ⇒ Object Also known as: to_js, js
53 54 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 |
# File 'lib/rack/sprockets/source.rb', line 53 def compiled @compiled ||= begin compiled_js = secretary.concatenation.to_s compiled_js = case @compress when :whitespace, true compiled_js.delete("\n") when :yui if defined?(YUI::JavaScriptCompressor) YUI::JavaScriptCompressor.new(YUI_OPTS).compress(compiled_js) else raise LoadError, "YUI::JavaScriptCompressor is not available. Install it with: gem install yui-compressor" end else compiled_js end if cache? && !File.exists?(cf = File.join(@cache, "#{@js_resource}.js")) FileUtils.mkdir_p(File.dirname(cf)) File.open(cf, "w") do |file| file.write(compiled_js) end end compiled_js end end |
#compress? ⇒ Boolean
33 34 35 |
# File 'lib/rack/sprockets/source.rb', line 33 def compress? !!@compress end |
#files ⇒ Object
43 44 45 |
# File 'lib/rack/sprockets/source.rb', line 43 def files @files ||= js_sources end |
#secretary ⇒ Object
47 48 49 50 51 |
# File 'lib/rack/sprockets/source.rb', line 47 def secretary @secretary_obj ||= Sprockets::Secretary.new(@secretary.merge({ :source_files => files })) end |