Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active May 23, 2018 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulcsmith/7973eae4f6fa5584e7e26f4163d3b026 to your computer and use it in GitHub Desktop.
Save paulcsmith/7973eae4f6fa5584e7e26f4163d3b026 to your computer and use it in GitHub Desktop.
# Here is what I have now
def self.create{% if with_bang %}!{% end %}(
{% if with_params %}params,{% end %}
{% for type_declaration in (NEEDS_ON_CREATE + NEEDS_ON_INITIALIZE) %}
{{ type_declaration }},
{% end %}
{% if @type.constant :FIELDS %}
{% for field in FIELDS %}
{{ field[:name] }} : {{ field[:type] }} | Nothing{% if field[:nilable] %} | Nil{% end %} = Nothing.new,
{% end %}
{% end %}
{% for field in VIRTUAL_FIELDS %}
{{ field.var }} : {{ field.type }} | Nothing = Nothing.new,
{% end %}
)
# Here's what I'd *love* to do
def self.create{% if with_bang %}!{% end %}(
{% if with_params %}params,{% end %}
{{ args_for_needs() }}
{{ args_for_fields() }}
{{ args_for_virtual_fields() }}
# Or maybe a method like this
{{ run_macro args_for_fields() }}
)
macro args_for_needs
{% for type_declaration in (NEEDS_ON_CREATE + NEEDS_ON_INITIALIZE) %}
{{ type_declaration }},
{% end %}
end
macro args_for_fields
{% if @type.constant :FIELDS %}
{% for field in FIELDS %}
{{ field[:name] }} : {{ field[:type] }} | Nothing{% if field[:nilable] %} | Nil{% end %} = Nothing.new,
{% end %}
{% end %}
end
macro args_for_virtual_fields
{% for field in VIRTUAL_FIELDS %}
{{ field.var }} : {{ field.type }} | Nothing = Nothing.new,
{% end %}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment