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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/format_engine/format_spec/variable.rb', line 13

def initialize(format)
  if format =~ /(\d+(\.\d+)?)/
    @format = $PREMATCH + $POSTMATCH

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

  else
    @parms  = nil
    @format = format
  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

#parmsObject (readonly)

The (optional) numeric format parameters.



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



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

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



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

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

#inspectObject

Inspect for debugging.



58
59
60
# File 'lib/format_engine/format_spec/variable.rb', line 58

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

#precObject

Get the precision parameter.



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

def prec
  (parms && parms.length > 1) ? parms[1].to_i : 0
end

#validate(engine) ⇒ Object

Is this variable supported by the engine?



30
31
32
33
# File 'lib/format_engine/format_spec/variable.rb', line 30

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

#widthObject

Get the width parameter.



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

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