Class: Nero::FormatTag

Inherits:
BaseTag show all
Defined in:
lib/nero/format_tag.rb

Instance Method Summary collapse

Instance Method Details

#resolve(args, context:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nero/format_tag.rb', line 5

def resolve(args, context:)
  if args.is_a?(Hash)
    context.add_error("!format takes a sequence [template, args...], got a mapping " \
      "(the 0.6 map form with fmt: is gone)")
    return
  end

  template, *fmt_args = args
  unless template.is_a?(String)
    context.add_error("!format needs a string template as first item, got #{template.inspect}")
    return
  end

  if fmt_args.any? && fmt_args.all?(Hash)
    fmt_args = [fmt_args.reduce({}, :merge).transform_keys(&:to_sym)]
  end

  format(template, *fmt_args)
rescue KeyError, ArgumentError, TypeError => e
  context.add_error("format error: #{e.message}")
  nil
end