class FormatEngine::Engine
The engine class of the format engine.
Attributes
The parse library
Any un-parsed string data.
Public Class Methods
Set up base data structures.
# File lib/format_engine/engine.rb, line 13 def initialize(library) @library = library @spec_pool = {} @unparsed = "" #Set up defaults for pre and post amble blocks. nop = lambda { } @library[:before] ||= nop @library[:after] ||= nop end
Public Instance Methods
Get an entry from the library
# File lib/format_engine/engine.rb, line 25 def [](index) @library[index] end
Set an entry in the library
# File lib/format_engine/engine.rb, line 30 def []=(index, value) @library[index] = value end
Do the actual work of building the formatted output.
Parameters
-
src - The source object being formatted.
-
format_spec_str - The format specification string.
# File lib/format_engine/engine.rb, line 38 def do_format(src, format_spec_str) spec_info = SpecInfo.new(src, "", self) due_process(spec_info, format_spec_str) do |format| spec_info.do_format(format) end end
Do the actual work of parsing the formatted input.
Parameters
-
src - The source string being parsed.
-
dst - The class of the object being created.
-
parse_spec_str - The format specification string.
# File lib/format_engine/engine.rb, line 51 def do_parse(src, dst, parse_spec_str) spec_info = SpecInfo.new(src, dst, self) due_process(spec_info, parse_spec_str) do |format| spec_info.do_parse(format) end ensure @unparsed = spec_info.src end
Private Instance Methods
Do the actual work of parsing the formatted input.
Parameters
-
spec_info - The state of the process.
-
spec_str - The format specification string.
-
block - A code block performed for each format specification.
# File lib/format_engine/engine.rb, line 69 def due_process(spec_info, spec_str) format_spec = get_spec(spec_str) spec_info.instance_exec(&self[:before]) format_spec.specs.each do |format| break if yield(format) == :break end spec_info.instance_exec(&self[:after]) spec_info.dst end
Get a format specification with caching.
# File lib/format_engine/engine.rb, line 84 def get_spec(spec_str) @spec_pool[spec_str] ||= FormatSpec.new(spec_str).validate(self) end