Class: IJSRails::Script
- Inherits:
-
Object
- Object
- IJSRails::Script
- Defined in:
- lib/ijs-rails/script.rb
Overview
Represents an inline script stored in the inline scripts directory.
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
The options to use when compiling the script.
-
#script ⇒ String
readonly
The name of a script within the inline scripts directory.
Instance Method Summary collapse
-
#cache_file_path ⇒ String
The path to the compiled script in the cache directory.
-
#cached? ⇒ Boolean
‘true` when the cache contains a copy of the compiled script, `false` otherwise.
-
#compile! ⇒ String
Compile the script and cache it for reuse.
-
#compiled ⇒ String
Retrieve the compiled script either from the cache or by compiling it.
-
#file_path ⇒ String
The path to the script file.
-
#fingerprint ⇒ String
The fingerprint representing the script and current options.
-
#initialize(script, options = {}) ⇒ Script
constructor
Create a new ‘Script` instance.
-
#isolate? ⇒ Boolean
‘true` when the script should be wrapped isolated in an anonymous function, `false` otherwise.
Constructor Details
#initialize(script, options = {}) ⇒ Script
Create a new ‘Script` instance.
20 21 22 23 24 25 |
# File 'lib/ijs-rails/script.rb', line 20 def initialize(script, = {}) @script = script = raise IJSRails::ScriptNotFound, script unless File.file?(file_path) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns The options to use when compiling the script.
13 14 15 |
# File 'lib/ijs-rails/script.rb', line 13 def end |
#script ⇒ String (readonly)
Returns 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_path ⇒ String
Returns 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.
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.
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, .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 ⇒ String
Retrieve the compiled script either from the cache or by compiling it.
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_path ⇒ String
Returns 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 |
#fingerprint ⇒ String
Returns The fingerprint representing the script and current options.
35 36 37 |
# File 'lib/ijs-rails/script.rb', line 35 def fingerprint @fingerprint ||= Zlib.crc32(.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.
53 54 55 56 57 58 59 |
# File 'lib/ijs-rails/script.rb', line 53 def isolate? if .key?(:isolate) [:isolate] else Rails.application.config.inline_js.isolate_scripts end end |