Class: IJSRails::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/ijs-rails/script.rb

Overview

Represents an inline script stored in the inline scripts directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, options = {}) ⇒ Script

Create a new ‘Script` instance.

Parameters:

  • script (String)

    The name of a script within the inline scripts directory.

  • options (Hash) (defaults to: {})

    The options to use when compiling the script.

Raises:



20
21
22
23
24
25
# 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

Instance Attribute Details

#optionsHash (readonly)

Returns The options to use when compiling the script.

Returns:

  • (Hash)

    The options to use when compiling the script.



13
14
15
# File 'lib/ijs-rails/script.rb', line 13

def options
  @options
end

#scriptString (readonly)

Returns The name of a script within the inline scripts directory.

Returns:

  • (String)

    The name of a script within the inline scripts directory.



9
10
11
# File 'lib/ijs-rails/script.rb', line 9

def script
  @script
end

Instance Method Details

#cache_file_pathString

Returns The path to the compiled script in the cache directory.

Returns:

  • (String)

    The path to the compiled script in the cache directory.



41
42
43
# 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?Boolean

Returns ‘true` when the cache contains a copy of the compiled script, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` when the cache contains a copy of the compiled script, `false` otherwise.



47
48
49
# File 'lib/ijs-rails/script.rb', line 47

def cached?
  File.file?(cache_file_path)
end

#compile!String

Compile the script and cache it for reuse.

Returns:

  • (String)

    The contents of the compiled script.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#compiledString

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

Returns:

  • (String)

    The contents of the compiled script.



85
86
87
88
89
# File 'lib/ijs-rails/script.rb', line 85

def compiled
  return File.read(cache_file_path) if cached?

  compile!
end

#file_pathString

Returns The path to the script file.

Returns:

  • (String)

    The path to the script file.



29
30
31
# File 'lib/ijs-rails/script.rb', line 29

def file_path
  @file_path ||= File.join(Rails.application.config.inline_js.path, "#{script}.js")
end

#fingerprintString

Returns The fingerprint representing the script and current options.

Returns:

  • (String)

    The fingerprint representing the script and current options.



35
36
37
# File 'lib/ijs-rails/script.rb', line 35

def fingerprint
  @fingerprint ||= Zlib.crc32(options.to_json + File.read(file_path)).to_s
end

#isolate?Boolean

Returns ‘true` when the script should be wrapped isolated in an anonymous function, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` when the script should be wrapped isolated in an anonymous function, `false` otherwise.



53
54
55
56
57
58
59
# 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