class FormatEngine::Engine

The engine class of the format engine.

Attributes

library[R]

The parse library

unparsed[R]

Any un-parsed string data.

Public Class Methods

new(library) click to toggle source

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

[](index) click to toggle source

Get an entry from the library

# File lib/format_engine/engine.rb, line 25
def [](index)
  @library[index]
end
[]=(index, value) click to toggle source

Set an entry in the library

# File lib/format_engine/engine.rb, line 30
def []=(index, value)
  @library[index] = value
end
do_format(src, format_spec_str) click to toggle source

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_parse(src, dst, parse_spec_str) click to toggle source

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

due_process(spec_info, spec_str) { |format| ... } click to toggle source

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_spec(spec_str) click to toggle source

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