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



71
72
73
74
# File 'lib/format_engine/format_spec/variable.rb', line 71

def do_format(spec_info)
  spec_info.fmt = self
  spec_info.instance_exec(&spec_info.engine[self.format])
end

#do_parse(spec_info) ⇒ Object

Parse from the input string



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

def do_parse(spec_info)
  spec_info.fmt = self
  spec_info.instance_exec(&spec_info.engine[self.format])
end

#has_prec?Boolean

Has a precision been specified?

Returns:

  • (Boolean)


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

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

#has_width?Boolean

Has a width been specified?

Returns:

  • (Boolean)


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

def has_width?
  parms
end

#inspectObject

Inspect for debugging.



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

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

#precObject

Get the precision parameter.



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

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

#prec_strObject

Get the precision as a string



66
67
68
# File 'lib/format_engine/format_spec/variable.rb', line 66

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

#validate(engine) ⇒ Object

Is this variable supported by the engine?



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

def validate(engine)
  fail "Unsupported tag = #{format.inspect}" unless engine[format]
  self
end

#widthObject

Get the width parameter.



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

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

#width_strObject

Get the width as a string



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

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