class
Crygen::Types::Macro
- Crygen::Types::Macro
- Crygen::Interfaces::GeneratorInterface
- Reference
- Object
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.crConstructors
Class Method Summary
-
.for_loop(name : String, iterator : String, &) : String
Generates a for loop macro.
-
.if(expression : String, &) : String
Generates an if condition macro.
-
.unless(expression : String, &) : String
Generates an unless condition macro.
-
.verbatim(&) : String
Generates a verbatim macro.
Instance Method Summary
-
#add_arg(arg : String) : self
Adds an argument to the macro.
-
#add_body(line : String) : self
Adds a new line into the macro body.
-
#body=(body : String) : self
Write the macro body.
-
#generate : String
Generates the macro.
-
#to_s(io : IO) : Nil
Generate a macro thanks to #to_s method.
Instance methods inherited from class Crygen::Interfaces::GeneratorInterface
generate : String
generate
Constructor Detail
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 %}
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 %}
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 %}
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 %}
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
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
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