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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/format_engine/format_spec/set.rb', line 21

def initialize(format)
  @raw = format

  if (match_data = /(\d+,)?(\d+)(?=\[)/.match(format))
    qualifier   = "{#{match_data[1] || "1,"}#{match_data[2]}}"
    @short_name = match_data.pre_match + "["
    @long_name  = match_data.pre_match + match_data.post_match
    set         = match_data.post_match
  elsif format =~ /\[/
    qualifier   = "+"
    @short_name = $PREMATCH + $MATCH
    @long_name  = format
    set         = $MATCH + $POSTMATCH
  else
    fail "Invalid set string #{format}"
  end

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

Instance Attribute Details

#long_nameObject (readonly)

The full name of the set.



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

def long_name
  @long_name
end

#regexObject (readonly)

The regular expression part of this set specification.



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

def regex
  @regex
end

#short_nameObject (readonly)

The short form name of the set.



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

def short_name
  @short_name
end

Instance Method Details

#do_format(spec_info) ⇒ Object

Format onto the output string



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

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



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

def do_parse(spec_info)
  spec_info.instance_exec(&@block)
end

#inspectObject

Inspect for debugging.



59
60
61
# File 'lib/format_engine/format_spec/set.rb', line 59

def inspect
  "Set(#{@long_name.inspect}, #{@short_name.inspect}, #{regex.inspect})"
end

#validate(engine) ⇒ Object

Is this format item supported by the engine’s library?



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

def validate(engine)
  @block = engine[@long_name] || engine[@short_name]
  fail "Unsupported tag = #{@raw.inspect}" unless @block
  true
end

#widthObject

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



16
17
18
# File 'lib/format_engine/format_spec/set.rb', line 16

def width
  0
end