lib/atd/routes.rb in atd-0.4.0 vs lib/atd/routes.rb in atd-0.5.0
- old
+ new
@@ -1,69 +1,81 @@
-
module ATD
# This module holds everything related to the compilation of routes.
module Compilation
- # A module designed to hold all the precompilation methods
- module Precompiler
- extend self
+ include InternalHelpers
- # Lists all filestypes that have defined precompiler methods
- def filetypes
- instance_methods(true) - [:filetypes]
- end
+ def respond_to_missing?(method, include_private = false)
+ ATD::Compilation.compilers.key?(method.to_sym) || super
end
- # A module designed to hold all the compilation methods
- module Compiler
- extend self
-
- # Lists all file extentions which have defined compiler methods
- def filetypes
- instance_methods(true) - [:filetypes]
- end
+ 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
- # 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.
- def self.compile(name, contents)
- return contents if name.nil?
- contents = File.read(contents) if contents.is_a? File
- parse(Compiler, name, contents)
- end
+ class << self
+ attr_accessor :compilers, :precompilers
- # This method is responsible for precompilation. 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 precompilation method on it.
- # route.output is either a full, expanded file path, a file, or a string
- def self.precompile(route, *opts)
- return nil if route.output.nil?
- if route.output.is_a?(File)
- name = route.output.is_a?(File) ? File.basename(route.output) : route.output.dup
- file = route.output.is_a?(File) ? route.output.dup : File.new(route.output)
- route.output = parse(Precompiler, name, File.read(file)) if opts[0].nil? || opts[0]
- return name
+ # 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.
+ def compile(route, *opts)
+ { content: parse(@compilers, route.filename, route.output, opts), name: route.filename }
end
- route.output
- end
- class << self
- private
+ # 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.
+ 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
- def parse(type, name, contents)
- name = name.split(".")
- extensions = name - [name.first]
+ 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.filetypes.include? extension.to_sym
- contents = type.send(extension, contents)
+ 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
end
end
end
+ATD::Compilation.compilers = {}
+ATD::Compilation.precompilers = {}
+
+def to_compile(name, &block)
+ warn "You have overriden the #{name.to_sym} compiler" if ATD::Compilation.compilers.key? name.to_sym
+ ATD::Compilation.compilers[name.to_sym] = block
+end
+
+def to_precompile(name, &block)
+ warn "You have overriden the #{name.to_sym} precompiler" if ATD::Compilation.compilers.key? name.to_sym
+ ATD::Compilation.precompilers[name.to_sym] = block
+end
+
class Object
- include ATD::Compilation::Compiler
+ include ATD::Compilation
end