class FormatEngine::FormatSet

A format engine set specification.

Attributes

long_name[R]

The full name of the set.

regex[R]

The regular expression part of this set specification.

short_name[R]

The short form name of the set.

Public Class Methods

new(format) click to toggle source

Setup a variable format specification.

# File lib/format_engine/format_spec/set.rb, line 21
def initialize(format)
  @raw = format

  if (match_data = /(\d+,)?(\d+)(?=\[)/.match(format))
    qualifier   = "{#{match_data[1] || "1,"}#{match_data[2]}}"
    @short_name = match_data.pre_match + "["
    @long_name  = match_data.pre_match + match_data.post_match
    set         = match_data.post_match
  elsif format =~ /\[/
    qualifier   = "+"
    @short_name = $PREMATCH + $MATCH
    @long_name  = format
    set         = $MATCH + $POSTMATCH
  else
    fail "Invalid set string #{format}"
  end

  @regex = Regexp.new("#{set}#{qualifier}")
end

Public Instance Methods

do_format(spec_info) click to toggle source

Format onto the output string

# File lib/format_engine/format_spec/set.rb, line 49
def do_format(spec_info)
  fail "The tag %{@raw} may not be used in formatting."
end
do_parse(spec_info) click to toggle source

Parse from the input string

# File lib/format_engine/format_spec/set.rb, line 54
def do_parse(spec_info)
  spec_info.instance_exec(&@block)
end
inspect() click to toggle source

Inspect for debugging.

# File lib/format_engine/format_spec/set.rb, line 59
def inspect
  "Set(#{@long_name.inspect}, #{@short_name.inspect}, #{regex.inspect})"
end
validate(engine) click to toggle source

Is this format item supported by the engine's library?

# File lib/format_engine/format_spec/set.rb, line 42
def validate(engine)
  @block = engine[@long_name] || engine[@short_name]
  fail "Unsupported tag = #{@raw.inspect}" unless @block
  true
end
width() click to toggle source

The width parameter. Handled internally so this is always zero.

# File lib/format_engine/format_spec/set.rb, line 16
def width
  0
end