Module: Recourse::Helpers::Examples

Included in:
Recourse::Helpers
Defined in:
lib/recourse/helpers/examples.rb

Overview

Reads an example of an accepted value off the pattern that accepts it.

Constant Summary collapse

PATTERN_TOKENS =

One token of a pattern and the {n} that may repeat it, so \d{5} reads as a single digit five times over rather than as five separate characters. \d, a bracket class and a literal are all it knows; nothing else is used.

/(\\d|\[[^\]]*\]|[^{])(?:\{(\d+),?\d*\})?/
CLASS_TOKEN =

A token that is a bracket class, so [2-9] counts as one rather than five.

/\A\[/
CLASS_SAMPLE =

First character a bracket class offers, past the bracket and any negation.

/[^\[^]/

Instance Method Summary collapse

Instance Method Details

#pattern_example(pattern) ⇒ Object

What the pattern accepts, so a field can name the shape it wants instead of only reporting that what was typed is wrong.



18
19
20
21
22
# File 'lib/recourse/helpers/examples.rb', line 18

def pattern_example(pattern)
  tokens = pattern.scan PATTERN_TOKENS

  tokens.map { |token, count| pattern_sample(token) * (count || 1).to_i }.join
end