Class: JadePug::Compiler
- Inherits:
-
Object
- Object
- JadePug::Compiler
- Extended by:
- Memoist
- Defined in:
- lib/jade-pug/compiler.rb
Overview
Abstraction layer for engine compiler.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#engine ⇒ Jade, Pug
readonly
Returns the engine module.
-
#version ⇒ String
readonly
Returns the version of engine compiler.
Instance Method Summary collapse
-
#compilation_snippet(args) ⇒ String
protected
Generates the JavaScript code that is the bridge from the gem to the template engine compiler.
-
#compile(source, options = {}) ⇒ String
abstract
Compiles template.
-
#initialize(engine, version) ⇒ Compiler
constructor
A new instance of Compiler.
-
#npm_package_require_snippet ⇒ String
protected
abstract
Returns the JavaScript code used to access engine NPM module.
-
#prepare_options(options) ⇒ Hash
protected
Responds for preparing compilation options.
-
#prepare_source(source) ⇒ String
protected
Reads the template source code.
-
#process_result(source, result, options) ⇒ String
protected
Responds for post-processing compilation result.
-
#shipped? ⇒ true, false
Returns true if this compiler is a shipped compiler.
-
#system? ⇒ true, false
Returns true if this compiler is a system compiler.
Constructor Details
#initialize(engine, version) ⇒ Compiler
Returns a new instance of Compiler.
34 35 36 37 |
# File 'lib/jade-pug/compiler.rb', line 34 def initialize(engine, version) @engine = engine @version = version end |
Instance Attribute Details
#engine ⇒ Jade, Pug (readonly)
Returns the engine module.
Used in such cases:
-
used to compute the name for engine
-
used to refer to the error classes
23 24 25 |
# File 'lib/jade-pug/compiler.rb', line 23 def engine @engine end |
#version ⇒ String (readonly)
Returns the version of engine compiler.
29 30 31 |
# File 'lib/jade-pug/compiler.rb', line 29 def version @version end |
Instance Method Details
#compilation_snippet(args) ⇒ String (protected)
Generates the JavaScript code that is the bridge from the gem to the template engine compiler.
The code responds for:
-
invoking compiler
-
rendering template function with given locals
-
returning the result
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/jade-pug/compiler.rb', line 118 def compilation_snippet(args) method = args.fetch(:method) arguments = args.fetch(:arguments) locals = args.fetch(:locals) = args.fetch(:options) <<-JAVASCRIPT (function() { var engine = #{ npm_package_require_snippet }; var template = engine[#{ JSON.dump(method) }].apply(engine, #{ JSON.dump(arguments) }); if (typeof template === 'function') { template = template(#{ JSON.dump(locals) }); } if (typeof console === 'object' && console !== null && typeof console.log === 'function') { console.log(template); } return template; })() JAVASCRIPT end |
#compile(source, options = {}) ⇒ String
Derived compilers must implement it.
Compiles template.
By default does nothing.
49 50 51 |
# File 'lib/jade-pug/compiler.rb', line 49 def compile(source, = {}) method_not_implemented end |
#npm_package_require_snippet ⇒ String (protected)
Derived compilers must implement it.
Returns the JavaScript code used to access engine NPM module.
147 148 149 |
# File 'lib/jade-pug/compiler.rb', line 147 def npm_package_require_snippet method_not_implemented end |
#prepare_options(options) ⇒ Hash (protected)
Responds for preparing compilation options.
The next steps are executed:
-
is merges options into the engine config
-
it camelizes and symbolizes every option key
-
it removes nil values from the options
94 95 96 97 98 |
# File 'lib/jade-pug/compiler.rb', line 94 def () = engine.config.to_hash.merge() .keys.each { |k| [k.to_s.gsub(/_([a-z])/) { $1.upcase }.to_sym] = [k] } .delete_if { |k, v| v.nil? } end |
#prepare_source(source) ⇒ String (protected)
Reads the template source code. Responds for pre-processing source code. Actually, it just checks if source code responds to #read and if so
80 81 82 |
# File 'lib/jade-pug/compiler.rb', line 80 def prepare_source(source) source.respond_to?(:read) ? source.read : source end |
#process_result(source, result, options) ⇒ String (protected)
Responds for post-processing compilation result.
By default removes leading and trailing whitespace by calling String#strip and returns the result. Derived compilers may override it for it’s own special behavior.
162 163 164 |
# File 'lib/jade-pug/compiler.rb', line 162 def process_result(source, result, ) result.strip end |
#shipped? ⇒ true, false
Returns true if this compiler is a shipped compiler. Otherwise returns false.
67 68 69 |
# File 'lib/jade-pug/compiler.rb', line 67 def shipped? ShippedCompiler === self end |
#system? ⇒ true, false
Returns true if this compiler is a system compiler. Otherwise returns false.
58 59 60 |
# File 'lib/jade-pug/compiler.rb', line 58 def system? SystemCompiler === self end |