Class: Rack::Sprockets::Source

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(js_name, 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_name, options={})
  @js_name  = js_name
  @compress = options[:compress]
  @cache    = options[:cache]

  @folder   = get_required_path(options, :folder)
  @secretary = SECRETARY_DEFAULTS.merge(options[:secretary] || {})
end

Instance Attribute Details

#js_nameObject (readonly)

Returns the value of attribute js_name.



22
23
24
# File 'lib/rack/sprockets/source.rb', line 22

def js_name
  @js_name
end

Instance Method Details

#cacheObject



39
40
41
# File 'lib/rack/sprockets/source.rb', line 39

def cache
  @cache
end

#cache?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rack/sprockets/source.rb', line 36

def cache?
  !@cache.nil?
end

#compiledObject 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_name}.js"))
      FileUtils.mkdir_p(@cache)
      File.open(cf, "w") do |file|
        file.write(compiled_js)
      end
    end
    
    compiled_js
  end
end

#compress?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rack/sprockets/source.rb', line 33

def compress?
  !!@compress
end

#filesObject



43
44
45
# File 'lib/rack/sprockets/source.rb', line 43

def files
  @files ||= js_sources
end

#secretaryObject



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