Class: FormatEngine::FormatSpec

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

Overview

The format string parser.

Constant Summary collapse

VAR_REGEX =

The regex used to parse variable specifications.

%r{(?<flags> [~@#$^&*\=?_<>\\\/\.,\|!]*){0}
 (?<parms> [-+]?(\d+(\.\d+)?)?){0}
 (%\g<flags>\g<parms>[a-zA-Z])
}x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fmt_string) ⇒ FormatSpec

Set up an instance of a format specification.
Note This is a private method (rdoc gets it wrong). To create new instances of FormatSpec do not use FormatSpec.new, but use FormatSpec.get_spec instead.



42
43
44
45
# File 'lib/format_engine/format_spec.rb', line 42

def initialize(fmt_string)
  @specs = []
  scan_spec(fmt_string)
end

Instance Attribute Details

#specsObject (readonly)

The array of specifications that were extracted.



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

def specs
  @specs
end

Class Method Details

.get_spec(fmt_string) ⇒ Object

Either get a format specification from the pool or create one.



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

def self.get_spec(fmt_string)
  @spec_pool ||= {}
  @spec_pool[fmt_string] ||= new(fmt_string)
end

Instance Method Details

#scan_spec(fmt_string) ⇒ Object

Scan the format string extracting literals and variables.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/format_engine/format_spec.rb', line 48

def scan_spec(fmt_string)
  until fmt_string.empty?
    if fmt_string =~ VAR_REGEX
      @specs << FormatLiteral.new($PREMATCH) unless $PREMATCH.empty?
      @specs << FormatVariable.new($MATCH)
      fmt_string  =  $POSTMATCH
    else
      @specs << FormatLiteral.new(fmt_string)
      fmt_string = ""
    end
  end
end

#validate(engine) ⇒ Object

Validate the specs of this format against the engine.



62
63
64
65
# File 'lib/format_engine/format_spec.rb', line 62

def validate(engine)
  specs.each {|item| item.validate(engine)}
  self
end