Class: FormatEngine::SpecInfo
- Inherits:
-
Object
- Object
- FormatEngine::SpecInfo
- Defined in:
- lib/format_engine/spec_info.rb
Overview
A little package of info about the engine’s progress.
Instance Attribute Summary collapse
-
#dst ⇒ Object
readonly
The destination of the process.
-
#engine ⇒ Object
readonly
The formatting engine.
-
#fmt ⇒ Object
readonly
The format specifier currently being processed.
-
#src ⇒ Object
readonly
The source data for formatting, string input for parsing.
-
#tmp ⇒ Object
readonly
A hash for state storage for the formatting/parsing process.
Instance Method Summary collapse
-
#cat(str) ⇒ Object
Concatenate onto the formatted output string.
-
#do_format(fmt) ⇒ Object
Pass the formatting action along to the current format element.
-
#do_parse(fmt) ⇒ Object
Pass the parsing action along to the current format element.
-
#found ⇒ Object
What was found by the last parse?.
-
#found? ⇒ Boolean
Was the last parse a success?.
-
#grab ⇒ Object
Grab some text.
-
#initialize(src, dst, engine) ⇒ SpecInfo
constructor
Set up the spec info.
-
#parse(target) ⇒ Object
Parse the source string for a target string or regex or return nil.
-
#parse!(target, msg = "#{target.inspect} not found") ⇒ Object
Parse the source string for a target string or regex or raise error.
-
#set(obj) ⇒ Object
Set the result of this parsing operation.
Constructor Details
#initialize(src, dst, engine) ⇒ SpecInfo
Set up the spec info.
22 23 24 |
# File 'lib/format_engine/spec_info.rb', line 22 def initialize(src, dst, engine) @src, @dst, @engine, @tmp = src, dst, engine, {} end |
Instance Attribute Details
#dst ⇒ Object (readonly)
The destination of the process.
10 11 12 |
# File 'lib/format_engine/spec_info.rb', line 10 def dst @dst end |
#engine ⇒ Object (readonly)
The formatting engine.
13 14 15 |
# File 'lib/format_engine/spec_info.rb', line 13 def engine @engine end |
#fmt ⇒ Object (readonly)
The format specifier currently being processed.
19 20 21 |
# File 'lib/format_engine/spec_info.rb', line 19 def fmt @fmt end |
#src ⇒ Object (readonly)
The source data for formatting, string input for parsing.
7 8 9 |
# File 'lib/format_engine/spec_info.rb', line 7 def src @src end |
#tmp ⇒ Object (readonly)
A hash for state storage for the formatting/parsing process.
16 17 18 |
# File 'lib/format_engine/spec_info.rb', line 16 def tmp @tmp end |
Instance Method Details
#cat(str) ⇒ Object
Concatenate onto the formatted output string.
27 28 29 |
# File 'lib/format_engine/spec_info.rb', line 27 def cat(str) @dst << str end |
#do_format(fmt) ⇒ Object
Pass the formatting action along to the current format element.
37 38 39 |
# File 'lib/format_engine/spec_info.rb', line 37 def do_format(fmt) (@fmt = fmt).do_format(self) end |
#do_parse(fmt) ⇒ Object
Pass the parsing action along to the current format element.
42 43 44 |
# File 'lib/format_engine/spec_info.rb', line 42 def do_parse(fmt) (@fmt = fmt).do_parse(self) end |
#found ⇒ Object
What was found by the last parse?
96 97 98 |
# File 'lib/format_engine/spec_info.rb', line 96 def found @match end |
#found? ⇒ Boolean
Was the last parse a success?
91 92 93 |
# File 'lib/format_engine/spec_info.rb', line 91 def found? @prematch.empty? && !@match.empty? end |
#grab ⇒ Object
Grab some text
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/format_engine/spec_info.rb', line 74 def grab width = fmt.width if width > 0 result, @src = src[0...width], src[width..-1] || "" elsif width == 0 result, @src = src[0...1], src[1..-1] || "" elsif width == -1 result, @src = src, "" else result, @src = src[0..width], src[(width+1)..-1] || "" end result end |
#parse(target) ⇒ Object
Parse the source string for a target string or regex or return nil.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/format_engine/spec_info.rb', line 47 def parse(target) #Handle the width option if specified. if (width = fmt.width) > 0 head, tail = src[0...width], src[width..-1] || "" else head, tail = src, "" end #Do the parse on the input string or regex. @prematch, @match, @postmatch = head.partition(target) #Analyze the results. if found? @src = @postmatch + tail @match else nil end end |
#parse!(target, msg = "#{target.inspect} not found") ⇒ Object
Parse the source string for a target string or regex or raise error.
68 69 70 71 |
# File 'lib/format_engine/spec_info.rb', line 68 def parse!(target, msg = "#{target.inspect} not found") fail "Parse error: #{msg}" unless parse(target) @match end |
#set(obj) ⇒ Object
Set the result of this parsing operation.
32 33 34 |
# File 'lib/format_engine/spec_info.rb', line 32 def set(obj) @dst = obj end |