Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Created October 7, 2016 20:55
macro inherited
def call_action(action_name)
case action_name
\{% for method in @type.methods.map(&.name) %}
when :\{{method.id}}
\{{method.id}}
\{% end %}
end
end
end
@cjgajard
Copy link

cjgajard commented Oct 7, 2016

I think is a parser error not allowing {% for %} inside case ... what do you think of reporting it?

Here's a workaround:

macro inherited
  def call_action(action_name)
    \{% for method in @type.methods.map(&.name) %}
      return \{{method.id}} if action_name == :\{{method}}
    \{% end %}
    raise ArgumentError.new "undefined method `#{action_name}` for #{\{{@type.name}}}"
  end
end

I don't recomend you using macro inherited tho. For things with @type, in my opinion, macro defs are better (just because is more clear code)

class Foo
  macro def call_action(action_name) # Omitting `macro` is also allowed 
    {% for method in @type.methods.map(&.name) %}
      return {{method.id}} if action_name == :{{method}}
    {% end %}
    raise ArgumentError.new "undefined method `#{action_name}` for #{{{@type.name}}}"
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment