Module: Cog::Generator

Extended by:
Generator
Includes:
FileMethods, Filters, LanguageMethods
Included in:
DSL::Cogfile, Generator, GeneratorSandbox, Seed, Seed::Feature, Seed::Var
Defined in:
lib/cog/generator.rb,
lib/cog/generator/filters.rb,
lib/cog/generator/file_methods.rb,
lib/cog/generator/language_methods.rb,
lib/cog/generator/language_methods/scope.rb

Overview

This module defines an interface which can be used by generator objects. Specifically, it makes it easy to find ERB templates and render them into generated source code files, using the #stamp method.

Defined Under Namespace

Modules: FileMethods, Filters, LanguageMethods

Instance Method Summary collapse

Methods included from LanguageMethods

#end_all_scopes, #include_guard_begin, #named_scope_begin, #scope_begin, #scope_end, #use_named_scope, #warning

Methods included from Filters

#call_filter, #comment

Methods included from FileMethods

#copy_file_if_missing, #files_are_same?, #get_template, #touch_directory, #touch_file

Instance Method Details

#embed(hook) {|context| ... } ⇒ nil

Provide a value for embeds with the given hook

Parameters:

  • hook (String)

    hook name used in the embed statements

Yield Parameters:

  • context (EmbedContext)

    provides information about the environment in which the embed statement was found

Yield Returns:

  • The value which will be used to expand the embed (or replace the embedded content)

Returns:

  • (nil)


67
68
69
70
71
72
73
74
75
76
# File 'lib/cog/generator.rb', line 67

def embed(hook, &block)
  eaten = 0 # keep track of eaten statements so that the index can be adjusted
  Embeds.find(hook) do |c|
    c.eaten = eaten
    if Embeds.update c, &block
      eaten += 1 if c.once?
      STDOUT.write "Updated #{c.path.relative_to_project_root} - #{(c.index + 1).ordinalize} occurrence of embed '#{c.hook}'\n".color :white
    end
  end
end

#gcontextHash

Returns Generator context. Generator methods should place context into here instead of instance variables to avoid polluting the instance variable name space.

Returns:

  • (Hash)

    Generator context. Generator methods should place context into here instead of instance variables to avoid polluting the instance variable name space



20
21
22
23
24
25
26
27
# File 'lib/cog/generator.rb', line 20

def gcontext
  if @generator_context.nil?
    @generator_context = {
      :scopes => []
    }
  end
  @generator_context
end

#stamp(template_path, destination = nil, opt = {}) ⇒ nil or String

Stamp a template into a file or return it as a string

Parameters:

Options Hash (opt):

  • :absolute_template_path (Boolean) — default: false

    is the template_path absolute?

  • :absolute_destination (Boolean) — default: false

    is the destination absolute?

  • :once (Boolean) — default: false

    if true, the file will not be updated if it already exists

  • :binding (Binding) — default: nil

    an optional binding to use while evaluating the template

  • :filter (String, Array<String>) — default: nil

    name(s) of Filters

  • :quiet (Boolean) — default: false

    suppress writing to STDOUT?

Returns:

  • (nil or String)

    if destination is not provided, the stamped template is returned as a string



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cog/generator.rb', line 39

def stamp(template_path, destination=nil, opt={})
  # Ignore destination if its a hash, its meant to be opt
  opt, destination = destination, nil if destination.is_a? Hash
  
  # Render and filter
  r = find_and_render template_path, opt
  r = filter_through r, opt[:filter]
  return r if destination.nil?

  # Place it in a file
  write_scratch_file(destination, r, opt[:absolute_destination]) do |path, scratch|
    updated = File.exists? path
    Embeds.copy_keeps(path, scratch)
    if files_are_same?(path, scratch) || (opt[:once] && updated)
      FileUtils.rm scratch
    else
      FileUtils.mv scratch, path
      STDOUT.write "#{updated ? :Updated : :Created} #{path.relative_to_project_root}\n".color(updated ? :white : :green) unless opt[:quiet]
    end
  end
  nil
end