Class: SC::Builder::JavaScript

Inherits:
Base
  • Object
show all
Defined in:
lib/sproutcore/builders/javascript.rb

Overview

This build is used to process a single javascript file. It will substitute any calls to sc_super and sc_static() (or static_url()). It does NOT product a combined JavaScript for production. See the Builder::CombinedJavaScript for more.

Instance Attribute Summary

Attributes inherited from Base

#entry

Instance Method Summary collapse

Methods inherited from Base

build, #initialize, #joinlines, #readlines, #replace_static_url, #static_url, #writelines

Constructor Details

This class inherits a constructor from SC::Builder::Base

Instance Method Details

#build(dst_path) ⇒ Object



19
20
21
22
# File 'lib/sproutcore/builders/javascript.rb', line 19

def build(dst_path)
  lines = readlines(entry.source_path).map { |l| rewrite_inline_code(l) }
  writelines dst_path, lines
end

#localized_strings?Boolean

Returns true if the current entry is a localized strings file. These files receive some specialized processing to allow for server-side only strings. – You can name a string key beginning with “@@” and it will be removed.

Returns:

  • (Boolean)


28
29
30
# File 'lib/sproutcore/builders/javascript.rb', line 28

def localized_strings?
  @lstrings ||= entry.localized? && entry.filename =~ /strings.js$/
end

#rewrite_inline_code(line) ⇒ Object

Rewrites inline content for a single line



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sproutcore/builders/javascript.rb', line 33

def rewrite_inline_code(line)

  # Fors strings file, remove server-side keys (i.e '@@foo' = 'bar')
  if localized_strings?
    line = line.gsub(/["']@@.*["']\s*?:\s*?["'].*["']\s*,\s*$/,'')

  # Otherwise process sc_super 
  else
    if line.match(/sc_super\(\s*\)/)
      line = line.gsub(/sc_super\(\s*\)/, 'arguments.callee.base.apply(this,arguments)')
    elsif line.match(/sc_super\(.+?\)/)
      SC.logger.warn "\nWARNING: Calling sc_super() with arguments is DEPRECATED. Please use sc_super() only.\n\n"
      line = line.gsub(/sc_super\((.+?)\)/, 'arguments.callee.base.apply(this, \1)')
    end
  end

  # and finally rewrite static_url
  return replace_static_url(line)
end