Sha256: b27f2a822c12fd6918f3ccac6d7eab94dbbf6ebe6927f6ecf8c6f30ddbca55fd
Contents?: true
Size: 1.72 KB
Versions: 3
Compression:
Stored size: 1.72 KB
Contents
require 'ostruct' module Pieces class RouteCompiler attr_reader :path attr_reader :env attr_reader :globals def initialize(config) @path = config[:path] || Dir.pwd @env = config[:env] @globals = config[:globals] || {} end def compile(files, name, route) files.merge("#{name}.html" => { contents: yield_pieces(route), type: 'html' }) end private def piece_path(piece) Dir["#{path}/app/views/{#{piece},#{piece}/#{piece},application/#{piece}}.html.*"].first end def route_globals(route) globals.merge(route['_global'] || {}) end def merge_globals(data, route) data.merge('_global' => route_globals(route).merge(data['_global'] || {})) end def pieces(data) (data['_pieces'] || []).map do |piece| [piece.keys.first, merge_globals(piece.values.first, data)] end end def compile_piece(piece, data) view_model = ViewModel.new(data['_global'].merge(data)) view_model.env = env ::Tilt.new(piece_path(piece)).render(view_model) { yield_pieces(data) } end def yield_pieces(data) pieces(data).reduce('') do |contents, (piece, data)| contents << compile_piece(piece, data) end end class ViewModel < OpenStruct attr_accessor :env begin require 'action_view' include ActionView::Context include ActionView::Helpers rescue LoadError => e end def initialize(*) super _prepare_context if respond_to?(:_prepare_context) end def compute_asset_path(path, options = {}) if env.resolve!(path) File.join('/assets', path) else super end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
pieces-0.4.5 | lib/pieces/compilers/route_compiler.rb |
pieces-0.4.4 | lib/pieces/compilers/route_compiler.rb |
pieces-0.4.3 | lib/pieces/compilers/route_compiler.rb |