class Crygen::Types::Macro

Overview

A class that generates a macro

macro_type = CGT::Macro.new("example")
macro_type.add_arg("name")
puts macro_type.generate

Output:

macro example(name)
  puts {{ name }}
end

Defined in:

types/macro.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Crygen::Interfaces::GeneratorInterface

generate : String generate

Constructor Detail

def self.new(name : String) #

[View source]

Class Method Detail

def self.for_loop(name : String, iterator : String, &) : String #

Generates a for loop macro.

Crygen::Types::Macro.for_loop("item", "items") do |str, indent|
  str << indent << "puts {{ item }}\n"
end

Output:

{% for item in items %}
  puts {{ item }}
{% end %}

[View source]
def self.if(expression : String, &) : String #

Generates an if condition macro.

Crygen::Types::Macro.if("x > 0") do |str, indent|
  str << indent << "puts \"positive\"\n"
end

Output:

{% for item in items %}
  puts "positive"
{% end %}

[View source]
def self.unless(expression : String, &) : String #

Generates an unless condition macro.

Crygen::Types::Macro.unless("x > 0") do |str, indent|
  str << indent << "puts \"negative or zero\"\n"
end

Output:

{% unless x > 0 %}
  puts \"negative or zero\"
{% end %}

[View source]
def self.verbatim(&) : String #

Generates a verbatim macro.

Crygen::Types::Macro.verbatim do |str, indent|
  str << indent << "puts 123\n"
end

Output:

{% verbatim do %}
  puts 123
{% end %}

[View source]

Instance Method Detail

def add_arg(arg : String) : self #

Adds an argument to the macro.

macro_type = CGT::Macro.new("example")
macro_type.add_arg("name")

Output:

macro example(name)
end

[View source]
def add_body(line : String) : self #

Adds a new line into the macro body.

macro_type = CGT::Macro.new("example")
macro_type.add_arg("name")
macro_type.add_body("puts {{ name }}")

Output:

macro example(name)
  puts {{ name }}
end

[View source]
def body=(body : String) : self #

Write the macro body.

macro_type = CGT::Macro.new("example")
macro_type.add_arg("name")
macro_type.body = "puts {{ name }}"

Output:

macro example(name)
  puts {{ name }}
end

[View source]
def generate : String #

Generates the macro.


[View source]
def to_s(io : IO) : Nil #

Generate a macro thanks to #to_s method.


[View source]