Class: FormatEngine::FormatVariable
- Inherits:
-
Object
- Object
- FormatEngine::FormatVariable
- Defined in:
- lib/format_engine/format_spec/variable.rb
Overview
A format engine variable specification.
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
The fixed part of this variable specification.
-
#parms ⇒ Object
readonly
The (optional) numeric format parameters.
Instance Method Summary collapse
-
#do_format(spec_info) ⇒ Object
Format onto the output string.
-
#do_parse(spec_info) ⇒ Object
Parse from the input string.
-
#initialize(format) ⇒ FormatVariable
constructor
Setup a variable format specification.
-
#inspect ⇒ Object
Inspect for debugging.
-
#prec ⇒ Object
Get the precision parameter.
-
#validate(engine) ⇒ Object
Is this variable supported by the engine?.
-
#width ⇒ Object
Get the width parameter.
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
#format ⇒ Object (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 |
#parms ⇒ Object (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 |
#inspect ⇒ Object
Inspect for debugging.
58 59 60 |
# File 'lib/format_engine/format_spec/variable.rb', line 58 def inspect "Variable(#{format.inspect}, #{parms.inspect})" end |
#prec ⇒ Object
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 |
#width ⇒ Object
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 |