Class: FormatEngine::FormatSet

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

Overview

A format engine set specification.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format) ⇒ FormatSet

Setup a variable format specification.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/format_engine/format_spec/set.rb', line 18

def initialize(format)
  @raw = format

  if format =~ /(\d+)(?=\[)/
    qualifier = "{1,#{$MATCH}}"
    @format   = $PREMATCH + "["
    set       = $POSTMATCH
  elsif format =~ /\[/
    qualifier = "+"
    @format   = $PREMATCH + $MATCH
    set       = $MATCH + $POSTMATCH
  else
    fail "Invalid set string #{format}"
  end

  @regex = Regexp.new("#{set}#{qualifier}")
end

Instance Attribute Details

#formatObject (readonly)

The fixed part of this set specification.



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

def format
  @format
end

#regexObject (readonly)

The regular expression part of this set specification.



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

def regex
  @regex
end

Instance Method Details

#do_format(spec_info) ⇒ Object

Format onto the output string



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

def do_format(spec_info)
  fail "The tag %{@raw} may not be used in formatting."
end

#do_parse(spec_info) ⇒ Object

Parse from the input string



48
49
50
# File 'lib/format_engine/format_spec/set.rb', line 48

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

#inspectObject

Inspect for debugging.



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

def inspect
  "Set(#{format.inspect}, #{regex.inspect})"
end

#validate(engine) ⇒ Object

Is this variable supported by the engine?



37
38
39
40
# File 'lib/format_engine/format_spec/set.rb', line 37

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

#widthObject

The width parameter. Handled internally so this is always zero.



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

def width
  0
end