class Generator::Context
Calls to “render” can take a context object that will be accessible from the templates.
Attributes
example_boolean[R]
Any properties of this object are available in the Haml templates.
Public Class Methods
new(example_boolean, scope, options, input_folder, output_folder)
click to toggle source
# File lib/generator/haml_generator.rb, line 64 def initialize(example_boolean, scope, options, input_folder, output_folder) @example_boolean = example_boolean @scope = scope @options = options @input_folder = input_folder @output_folder = output_folder load_helper('./dev_root/shared/helper/*.rb') load_helper("./#{input_folder}/helper/*.rb") end
Public Instance Methods
load_helper(folder)
click to toggle source
# File lib/generator/haml_generator.rb, line 75 def load_helper(folder) Dir.glob(folder).each do |path| next unless Base.changed? path load path file_without_ext = path.split('/')[-1].split('.').first module_name = file_without_ext.classify STDERR.puts "->loading project helper: \e[32m#{module_name}\e[0m" self.class.send(:include, module_name.constantize) Base.cache path end end
render_partial(file_name)
click to toggle source
This function is no different from the “copyright_year” function above. It just uses some conventions to render another template file when it's called.
# File lib/generator/haml_generator.rb, line 89 def render_partial(file_name) # The "default" version of the partial. file_to_render = "#{@input_folder}/partials/#{file_name.to_s}.haml" if @scope # Look for a partial prefixed with the current "scope" (which is just the name of the # primary template being rendered). scope_file = "#{@input_folder}/partials/#{@scope.to_s}_#{file_name.to_s}.haml" # Use it if it's there. file_to_render = scope_file if File.exists? scope_file end # If we found a matching partial (either the scoped one or the default), render it now. if File.exists? file_to_render partial = Haml::Engine.new(File.read(file_to_render), @options) partial.render self else nil end rescue Exception => e raise $!, "#{$!} PARTIAL::#{file_name} ", $!.backtrace end