diff options
Diffstat (limited to 'src/guff/template.cr')
-rw-r--r-- | src/guff/template.cr | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/guff/template.cr b/src/guff/template.cr index 4b54fb6..f5a1693 100644 --- a/src/guff/template.cr +++ b/src/guff/template.cr @@ -7,16 +7,19 @@ module Guff @has_keys = @tokens.select { |t| t.type == :key }.size > 0 end - def run(args = nil : Hash(String, String)?) : String + def run(args : Nil) : String + raise_missing if @has_keys + + # return literal string + @string + end + + def run( + args = {} of String => String : Hash(String, String) + ) : String if @has_keys # check template args - if !args || args.size == 0 - raise "missing template args: %s" % [@tokens.select { |t| - t.type == :key - }.map { |t| - t.value - }.sort.join(", ")] - end + raise_missing if args.size == 0 # build result String.build do |r| @@ -30,6 +33,14 @@ module Guff end end + private def raise_missing + raise "missing template args: %s" % [@tokens.select { |t| + t.type == :key + }.map { |t| + t.value + }.sort.join(", ")] + end + SCAN_RE = %r{ # match key (?:%\{(?<key>[^\}]+)\}) |