Class: Generator::Context

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers, ActivesupportOverride, AssetHelper
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 AssetHelper

#file_exists?, #glyph_icon, #glyph_icon_classes, #headjs_javascript_include_bower_tag, #headjs_javascript_include_tag, #headjs_stylesheet_link_tag, #javascript_include_bower_tag, #path_to_bower, #select_partials, #with_coffee, #with_sass

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.



64
65
66
67
68
69
70
71
72
73
# 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

Instance Attribute Details

#example_booleanObject (readonly)

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



58
59
60
# File 'lib/generator/haml_generator.rb', line 58

def example_boolean
  @example_boolean
end

Instance Method Details

#load_helper(folder) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ 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.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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