Class: Generator::Context

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers, ActivesupportOverride
Defined in:
lib/generator/haml_generator.rb

Overview

Calls to “render” can take a context object that will be accessible from the templates.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActivesupportOverride

#external_path?, #javascript_include_tag, #link_tag, #meta_tag, #meta_tag_http, #path_to_css, #path_to_image, #path_to_js, #stylesheet_link_tag, #url_for

Constructor Details

#initialize(example_boolean, scope, options, input_folder, output_folder) ⇒ Context

Returns a new instance of Context.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/generator/haml_generator.rb', line 66

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
  Dir.glob("./#{input_folder}/helper/*.rb").each do |path|
    require path
    file_without_ext = path.split('/')[-1].split('.').first
    module_name      = file_without_ext.classify
    STDERR.puts 'loading project helper -> '+module_name
    self.class.send(:include, module_name.constantize)
  end
end

Instance Attribute Details

#example_booleanObject (readonly)

Any properties of this object are available in the Haml templates.



54
55
56
# File 'lib/generator/haml_generator.rb', line 54

def example_boolean
  @example_boolean
end

Instance Method Details

#render_partial(file_name) ⇒ Object

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generator/haml_generator.rb', line 83

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
end