Module: Cog::Generator::LanguageMethods

Included in:
Cog::Generator
Defined in:
lib/cog/generator/language_methods.rb,
lib/cog/generator/language_methods/scope.rb

Overview

Methods to help with generating language constructs

Defined Under Namespace

Classes: Scope

Instance Method Summary collapse

Instance Method Details

#end_all_scopesString

End all scope currently on the stack

Returns:



36
37
38
39
40
41
42
# File 'lib/cog/generator/language_methods.rb', line 36

def end_all_scopes
  lines = []
  while line = scope_end(:safe_pop => true)
    lines << line
  end
  lines.join "\n"
end

#include_guard_begin(name = nil) ⇒ String

Returns an include guard statement for the active language.

Parameters:

  • name (String) (defaults to: nil)

    name of the module

Returns:

  • (String)

    an include guard statement for the active language



60
61
62
# File 'lib/cog/generator/language_methods.rb', line 60

def include_guard_begin(name = nil)
  scope_begin Scope.new(:include_guard, name)
end

#named_scope_begin(name = nil) ⇒ String

Returns a scope begin statement.

Parameters:

  • name (String) (defaults to: nil)

    name of the scope

Returns:

  • (String)

    a scope begin statement



52
53
54
# File 'lib/cog/generator/language_methods.rb', line 52

def named_scope_begin(name = nil)
  scope_begin Scope.new(:named_scope, name)
end

#scope_begin(scope) ⇒ String

Begin a scope, pushing it onto the scope stack

Parameters:

  • scope (Scope)

    the scope to begin

Returns:

  • (String)

    the scope begin statement



17
18
19
20
# File 'lib/cog/generator/language_methods.rb', line 17

def scope_begin(scope)
  gcontext[:scopes] << scope
  Cog.active_language.method("#{scope.type}_begin").call(scope.name)
end

#scope_end(opt = {}) ⇒ String? Also known as: named_scope_end, include_guard_end

End the scope, popping it off the scope stack

Parameters:

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :safe_pop (Boolean) — default: false

    do not throw an exception if the stack is empty, instead, return nil

Returns:

  • (String, nil)

    the scope end statement, or nil if :safe_pop and the stack is empty



25
26
27
28
29
30
31
32
# File 'lib/cog/generator/language_methods.rb', line 25

def scope_end(opt={})
  if gcontext[:scopes].empty?
    raise Errors::ScopeStackUnderflow.new(self) unless opt[:safe_pop]
    return nil
  end
  scope = gcontext[:scopes].pop
  Cog.active_language.method("#{scope.type}_end").call(scope.name)
end

#use_named_scope(name) ⇒ String

Returns a using statement for the named scope.

Parameters:

  • name (String)

    name of the scope to use

Returns:

  • (String)

    a using statement for the named scope



46
47
48
# File 'lib/cog/generator/language_methods.rb', line 46

def use_named_scope(name)
  Cog.active_language.use_named_scope(name)
end

#warningString

Returns a warning comment not to edit the generated file.

Returns:

  • (String)

    a warning comment not to edit the generated file



10
11
12
# File 'lib/cog/generator/language_methods.rb', line 10

def warning
  stamp 'warning', :filter => 'comment'
end