class FormatEngine::FormatVariable

A format engine variable specification.

Attributes

format[R]

The fixed part of this variable specification.

parm_str[R]

The (optional) parameter data as a string or an empty string.

parms[R]

The (optional) numeric format parameters or nil.

Public Class Methods

new(format) click to toggle source

Setup a variable format specification.

# File lib/format_engine/format_spec/variable.rb, line 16
def initialize(format)
  if format =~ /[+-]?(\d+(\.\d+)?)/
    @format   = $PREMATCH + $POSTMATCH
    @parm_str = $MATCH

    if (@parm_str) =~ /\./
      @parms = [$PREMATCH, $POSTMATCH]
    else
      @parms = [@parm_str]
    end

  else
    @format   = format
    @parm_str = ""
    @parms    = nil
  end
end

Public Instance Methods

do_format(spec_info) click to toggle source

Format onto the output string

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

Parse from the input string

# File lib/format_engine/format_spec/variable.rb, line 77
def do_parse(spec_info)
  spec_info.instance_exec(&@block)
end
has_prec?() click to toggle source

Has a precision been specified?

# File lib/format_engine/format_spec/variable.rb, line 50
def has_prec?
  has_width? && parms.length > 1
end
has_width?() click to toggle source

Has a width been specified?

# File lib/format_engine/format_spec/variable.rb, line 35
def has_width?
  parms
end
inspect() click to toggle source

Inspect for debugging.

# File lib/format_engine/format_spec/variable.rb, line 82
def inspect
  "Variable(#{format.inspect}, #{parms.inspect})"
end
prec() click to toggle source

Get the precision parameter.

# File lib/format_engine/format_spec/variable.rb, line 55
def prec
  has_prec? ? parms[1].to_i : 0
end
prec_str() click to toggle source

Get the precision as a string

# File lib/format_engine/format_spec/variable.rb, line 60
def prec_str
  has_prec? ? parms[1] : ""
end
validate(engine) click to toggle source

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

# File lib/format_engine/format_spec/variable.rb, line 65
def validate(engine)
  @block = engine[format]
  fail "Unsupported tag = #{format.inspect}" unless @block
  true
end
width() click to toggle source

Get the width parameter.

# File lib/format_engine/format_spec/variable.rb, line 40
def width
  has_width? ? parms[0].to_i : 0
end
width_str() click to toggle source

Get the width as a string

# File lib/format_engine/format_spec/variable.rb, line 45
def width_str
  has_width? ? parms[0] : ""
end