Class: FormatEngine::FormatRgx
- Inherits:
-
Object
- Object
- FormatEngine::FormatRgx
- Defined in:
- lib/format_engine/format_spec/rgx.rb
Overview
A format engine set specification.
Instance Attribute Summary collapse
-
#long_name ⇒ Object
readonly
The full name of the set.
-
#regex ⇒ Object
readonly
The regular expression part of this set specification.
-
#short_name ⇒ Object
readonly
The short form name of the set.
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) ⇒ FormatRgx
constructor
Setup a variable format specification.
-
#inspect ⇒ Object
Inspect for debugging.
-
#validate(engine) ⇒ Object
Is this format item supported by the engine’s library?.
-
#width ⇒ Object
The width parameter.
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, _, = post.partition(/(?<=[^\\])\// ) opt = (.include?('x') ? Regexp::EXTENDED : 0) | (.include?('i') ? Regexp::IGNORECASE : 0) | (.include?('m') ? Regexp::MULTILINE : 0) @regex = Regexp.new(exp.gsub(/\\\//, '/'), opt) end |
Instance Attribute Details
#long_name ⇒ Object (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 |
#regex ⇒ Object (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_name ⇒ Object (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 |
#inspect ⇒ Object
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 |
#width ⇒ Object
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 |