Class: Mustermann::AST::ParamScanner::Capture

Inherits:
NodeTranslator
  • Object
show all
Defined in:
lib/mustermann/ast/param_scanner.rb

Instance Method Summary collapse

Instance Method Details

#converter(capture) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mustermann/ast/param_scanner.rb', line 30

def converter(capture)
  case capture
  when Hash   then return converter(capture[name.to_sym])
  when Class  then regexp, converter = CONVERTERS[capture.name]
  when Symbol then regexp, converter = CONVERTERS[capture]
  when Array
    entries = capture.map { |item| converter(item) }.compact
    regexp  = Regexp.union(entries.map(&:first))

    entries.map! { |r, c| [/\A#{r}\Z/, c] }

    converter = ->(string) do
      _, c = entries.find { |r, _| r.match?(string) }
      c&.call(string) || string
    end
  end

  return unless converter
  [regexp, converter.to_proc]
end

#translate(options) ⇒ Object



24
25
26
27
28
# File 'lib/mustermann/ast/param_scanner.rb', line 24

def translate(options)
  return { name => convert } if convert
  _, converter = converter(options[:capture])
  converter ? { name => converter } : {}
end