Class: FormatEngine::FormatVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/format_engine/format_spec/variable.rb

Overview

A format engine variable specification.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format) ⇒ FormatVariable

Setup a variable format specification.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#formatObject (readonly)

The fixed part of this variable specification.



7
8
9
# File 'lib/format_engine/format_spec/variable.rb', line 7

def format
  @format
end

#parm_strObject (readonly)

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



13
14
15
# File 'lib/format_engine/format_spec/variable.rb', line 13

def parm_str
  @parm_str
end

#parmsObject (readonly)

The (optional) numeric format parameters or nil.



10
11
12
# File 'lib/format_engine/format_spec/variable.rb', line 10

def parms
  @parms
end

Instance Method Details

#do_format(spec_info) ⇒ Object

Format onto the output string



72
73
74
# 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) ⇒ Object

Parse from the input string



77
78
79
# File 'lib/format_engine/format_spec/variable.rb', line 77

def do_parse(spec_info)
  spec_info.instance_exec(&@block)
end

#has_prec?Boolean

Has a precision been specified?

Returns:

  • (Boolean)


50
51
52
# File 'lib/format_engine/format_spec/variable.rb', line 50

def has_prec?
  has_width? && parms.length > 1
end

#has_width?Boolean

Has a width been specified?

Returns:

  • (Boolean)


35
36
37
# File 'lib/format_engine/format_spec/variable.rb', line 35

def has_width?
  parms
end

#inspectObject

Inspect for debugging.



82
83
84
# File 'lib/format_engine/format_spec/variable.rb', line 82

def inspect
  "Variable(#{format.inspect}, #{parms.inspect})"
end

#precObject

Get the precision parameter.



55
56
57
# File 'lib/format_engine/format_spec/variable.rb', line 55

def prec
  has_prec? ? parms[1].to_i : 0
end

#prec_strObject

Get the precision as a string



60
61
62
# File 'lib/format_engine/format_spec/variable.rb', line 60

def prec_str
  has_prec? ? parms[1] : ""
end

#validate(engine) ⇒ Object

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



65
66
67
68
69
# 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

#widthObject

Get the width parameter.



40
41
42
# File 'lib/format_engine/format_spec/variable.rb', line 40

def width
  has_width? ? parms[0].to_i : 0
end

#width_strObject

Get the width as a string



45
46
47
# File 'lib/format_engine/format_spec/variable.rb', line 45

def width_str
  has_width? ? parms[0] : ""
end