Module: ATD::Compilation
Overview
This module holds everything related to the compilation of routes.
Class Attribute Summary collapse
-
.compilers ⇒ Object
Returns the value of attribute compilers.
-
.precompilers ⇒ Object
Returns the value of attribute precompilers.
Class Method Summary collapse
-
.compile(route, *opts) ⇒ Object
This method is responsible for live compilation.
- .parse(type, name, contents, *opts) ⇒ Object
-
.precompile(route, *opts) ⇒ Object
This method is responsible for precompilation.
Instance Method Summary collapse
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
Methods included from InternalHelpers
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
10 11 12 13 14 |
# File 'lib/atd/routes.rb', line 10 def method_missing(method, *args, &block) return super unless ATD::Compilation.compilers.key?(method.to_sym) filename = args.pop ATD::Compilation.compilers[method].call(ATD::Compilation.parse(ATD::Compilation.compilers, filename, File.read(asset(filename)), Hash(args).merge(run: false)), *args) end |
Class Attribute Details
.compilers ⇒ Object
Returns the value of attribute compilers.
17 18 19 |
# File 'lib/atd/routes.rb', line 17 def compilers @compilers end |
.precompilers ⇒ Object
Returns the value of attribute precompilers.
17 18 19 |
# File 'lib/atd/routes.rb', line 17 def precompilers @precompilers end |
Class Method Details
.compile(route, *opts) ⇒ Object
This method is responsible for live compilation. It takes an ATD::Route as input, and returns either the filename if Route.output is a file or the Route.output string if Route.output is a string. It will also take the file and call the corresponding compilation method on it.
22 23 24 |
# File 'lib/atd/routes.rb', line 22 def compile(route, *opts) { content: parse(@compilers, route.filename, route.output, opts), name: route.filename } end |
.parse(type, name, contents, *opts) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/atd/routes.rb', line 50 def parse(type, name, contents, *opts) return contents if name.include?("\n") extensions = name.to_s.split(".") extensions.shift extensions.each do |extension| if type.key? extension.to_sym contents = type[extension.to_sym].call(contents, *opts) if Hash(opts.last)[:run] != false extensions -= [extension] end end contents end |
.precompile(route, *opts) ⇒ Object
This method is responsible for precompilation. It takes an ATD::Route as input and compilation options. It sets the routes filename and output properties to be the filename and the contents of that file or, if route.output doesn’t correspond to a valid file, it sets both to be route.output. It returns the precompiled version of the file regardless, and sets route.ouput to be the precompiled version unless the opt precompile: false is passed.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/atd/routes.rb', line 30 def precompile(route, *opts) opts = Hash(opts[0]) unless opts.is_a? Hash opts = opts.merge(route.args) # route.output should always be a String, File, or NilClass name = route.output.to_s name = File.basename(route.output) if route.output.is_a?(File) # name should always be a String content = if route.output.is_a?(File) || (route.output.is_a?(String) && !route.output.empty? && File.exist?(route.output) && !Dir.exist?(route.output)) File.read(route.output) elsif !route.output.nil? route.output else "" end # content should always be a String route.output = parse(@precompilers, name, content, opts) unless opts[:precompile] == false route.filename = name { content: parse(@precompilers, name, content, opts), name: route.filename } end |
Instance Method Details
#respond_to_missing?(method, include_private = false) ⇒ Boolean
6 7 8 |
# File 'lib/atd/routes.rb', line 6 def respond_to_missing?(method, include_private = false) ATD::Compilation.compilers.key?(method.to_sym) || super end |