Class: FormatEngine::FormatRgx

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

Overview

A format engine set specification.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format) ⇒ FormatRgx

Setup a variable format specification.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/format_engine/format_spec/rgx.rb', line 21

def initialize(format)
  @long_name = format
  pre, _, post = format.partition('/')
  @short_name = pre + '/'

  exp, _, options = post.partition(/(?<=[^\\])\// )
  opt = (options.include?('x') ? Regexp::EXTENDED   : 0) |
        (options.include?('i') ? Regexp::IGNORECASE : 0) |
        (options.include?('m') ? Regexp::MULTILINE  : 0)

  @regex = Regexp.new(exp.gsub(/\\\//, '/'), opt)
end

Instance Attribute Details

#long_nameObject (readonly)

The full name of the set.



7
8
9
# File 'lib/format_engine/format_spec/rgx.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/rgx.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/rgx.rb', line 10

def short_name
  @short_name
end

Instance Method Details

#do_format(spec_info) ⇒ Object

Format onto the output string



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

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



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

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

#inspectObject

Inspect for debugging.



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

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

#validate(engine) ⇒ Object

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



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

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/rgx.rb', line 16

def width
  0
end