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. In the context of a formatting / parsing block, the “self” of that block is an instance of SpecInfo.
The components of that instance Struct are:
When Formatting:
-
src - The object that is the source of the data.
-
dst - A string that receives the formatted output.
-
fmt - The format specification currently being processed.
-
engine - The formatting engine. Mostly for access to the library.
-
tmp - A utility hash so that the formatting process can retain state.
Methods
-
cat - Append the string that follows to the formatted output.
When Parsing:
-
src - A string that is the source of formatted input.
-
dst - The class of the object being created.
-
fmt - The parse specification currently being processed.
-
engine - The parsing engine. Mostly for access to the library.
-
tmp - A utility hash so that the parsing process can retain state.
Methods
-
set - Set the return value of the parsing operation to the value that follows.
-
parse - Look for the string or regex parm that follows. Return the data found or nil.
-
parse! - Like parse but raises an exception (with optional msg) if not found.
-
found? - Did the last parse succeed?
-
found - The text found by the last parse (or parse!) operation.
Instance Attribute Summary collapse
-
#dst ⇒ Object
readonly
General readers.
-
#engine ⇒ Object
readonly
General readers.
-
#fmt ⇒ Object
General accessors.
-
#src ⇒ Object
readonly
General readers.
-
#tmp ⇒ Object
readonly
General readers.
Instance Method Summary collapse
-
#cat(str) ⇒ Object
Concatenate onto the formatted output string.
-
#found ⇒ Object
What was found by the last parse?.
-
#found? ⇒ Boolean
Was the last parse a success?.
-
#initialize(src, dst, fmt, engine, tmp = {}) ⇒ SpecInfo
constructor
Set up the spec info.
-
#parse(tgt) ⇒ Object
Parse the source string for a string or regex.
-
#parse!(tgt, msg = "#{tgt.inspect} not found") ⇒ Object
Parse the source string for a string or regex or raise error.
-
#set(obj) ⇒ Object
Set the result of this parsing operation.
Constructor Details
#initialize(src, dst, fmt, engine, tmp = {}) ⇒ SpecInfo
Set up the spec info.
39 40 41 |
# File 'lib/format_engine/spec_info.rb', line 39 def initialize(src, dst, fmt, engine, tmp = {}) @src, @dst, @fmt, @engine, @tmp = src, dst, fmt, engine, tmp end |
Instance Attribute Details
#dst ⇒ Object (readonly)
General readers
33 34 35 |
# File 'lib/format_engine/spec_info.rb', line 33 def dst @dst end |
#engine ⇒ Object (readonly)
General readers
33 34 35 |
# File 'lib/format_engine/spec_info.rb', line 33 def engine @engine end |
#fmt ⇒ Object
General accessors
36 37 38 |
# File 'lib/format_engine/spec_info.rb', line 36 def fmt @fmt end |
#src ⇒ Object (readonly)
General readers
33 34 35 |
# File 'lib/format_engine/spec_info.rb', line 33 def src @src end |
#tmp ⇒ Object (readonly)
General readers
33 34 35 |
# File 'lib/format_engine/spec_info.rb', line 33 def tmp @tmp end |
Instance Method Details
#cat(str) ⇒ Object
Concatenate onto the formatted output string.
44 45 46 |
# File 'lib/format_engine/spec_info.rb', line 44 def cat(str) @dst << str end |
#found ⇒ Object
What was found by the last parse?
77 78 79 |
# File 'lib/format_engine/spec_info.rb', line 77 def found @result[1] end |
#found? ⇒ Boolean
Was the last parse a success?
72 73 74 |
# File 'lib/format_engine/spec_info.rb', line 72 def found? @result[0].empty? && !@result[1].empty? end |
#parse(tgt) ⇒ Object
Parse the source string for a string or regex
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/format_engine/spec_info.rb', line 54 def parse(tgt) @result = src.partition(tgt) if found? @src = @result[2] @result[1] else nil end end |
#parse!(tgt, msg = "#{tgt.inspect} not found") ⇒ Object
Parse the source string for a string or regex or raise error.
66 67 68 69 |
# File 'lib/format_engine/spec_info.rb', line 66 def parse!(tgt, msg = "#{tgt.inspect} not found") fail "Parse error: #{msg}" unless parse(tgt) found end |
#set(obj) ⇒ Object
Set the result of this parsing operation.
49 50 51 |
# File 'lib/format_engine/spec_info.rb', line 49 def set(obj) @dst = obj end |