class IJSRails::Script

Represents an inline script stored in the inline scripts directory.

Attributes

options[R]

@return [Hash] The options to use when compiling the script.

script[R]

@return [String] The name of a script within the inline scripts directory.

Public Class Methods

new(script, options = {}) click to toggle source

Create a new `Script` instance.

@param script [String] The name of a script within the inline scripts directory. @param options [Hash] The options to use when compiling the script.

# File lib/ijs-rails/script.rb, line 20
def initialize(script, options = {})
  @script = script
  @options = options

  raise IJSRails::ScriptNotFound, script unless File.file?(file_path)
end

Public Instance Methods

cache_file_path() click to toggle source

@return [String] The path to the compiled script in the cache directory.

# File lib/ijs-rails/script.rb, line 41
def cache_file_path
  @cache_file_path ||= File.join(Rails.application.config.inline_js.cache_path, "#{script}-#{fingerprint}.min.js")
end
cached?() click to toggle source

@return [Boolean] `true` when the cache contains a copy of the compiled script, `false` otherwise.

# File lib/ijs-rails/script.rb, line 47
def cached?
  File.file?(cache_file_path)
end
compile!() click to toggle source

Compile the script and cache it for reuse.

@return [String] The contents of the compiled script.

# File lib/ijs-rails/script.rb, line 65
def compile!
  # Compile the script.
  script = File.read(file_path)
  script = "!function(){\n#{script}\n}();" if isolate?
  script = Uglifier.compile(script, options.except(:isolate))

  # Create the cache directory for the script (unless it already exists).
  cache_directory_path = File.dirname(cache_file_path)
  FileUtils.mkdir_p(cache_directory_path) unless File.directory?(cache_directory_path)

  # Write the script to the determined cache file path.
  File.write(cache_file_path, script)

  script
end
compiled() click to toggle source

Retrieve the compiled script either from the cache or by compiling it.

@return [String] The contents of the compiled script.

# File lib/ijs-rails/script.rb, line 85
def compiled
  return File.read(cache_file_path) if cached?

  compile!
end
file_path() click to toggle source

@return [String] The path to the script file.

# File lib/ijs-rails/script.rb, line 29
def file_path
  @file_path ||= File.join(Rails.application.config.inline_js.path, "#{script}.js")
end
fingerprint() click to toggle source

@return [String] The fingerprint representing the script and current options.

# File lib/ijs-rails/script.rb, line 35
def fingerprint
  @fingerprint ||= Zlib.crc32(options.to_json + File.read(file_path)).to_s
end
isolate?() click to toggle source

@return [Boolean] `true` when the script should be wrapped isolated in an anonymous function, `false` otherwise.

# File lib/ijs-rails/script.rb, line 53
def isolate?
  if options.key?(:isolate)
    options[:isolate]
  else
    Rails.application.config.inline_js.isolate_scripts
  end
end