Class: FormatEngine::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/format_engine/engine.rb

Overview

The engine class of the format engine.

Instance Method Summary collapse

Constructor Details

#initialize(library) ⇒ Engine

Set up base data structures.



7
8
9
10
11
12
13
14
# File 'lib/format_engine/engine.rb', line 7

def initialize(library)
  @lib = library

  #Set up defaults for pre and post amble blocks.

  nop = lambda { }
  @lib[:before] ||= nop
  @lib[:after]  ||= nop
end

Instance Method Details

#[](index) ⇒ Object

Get an entry from the library



17
18
19
# File 'lib/format_engine/engine.rb', line 17

def [](index)
  @lib[index]
end

#[]=(index, value) ⇒ Object

Set an entry in the library



22
23
24
# File 'lib/format_engine/engine.rb', line 22

def []=(index, value)
  @lib[index] = value
end

#do_format(src, format_spec_str) ⇒ Object

Do the actual work of building the formatted output.
Parameters

  • src - The source object being formatted.

  • format_spec_str - The format specification string.



30
31
32
33
34
35
36
# File 'lib/format_engine/engine.rb', line 30

def do_format(src, format_spec_str)
  spec_info = SpecInfo.new(src, "", nil, self, {})

  due_process(spec_info, format_spec_str) do |fmt|
    fmt.do_format(spec_info)
  end
end

#do_parse(src, dst, parse_spec_str) ⇒ Object

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.



43
44
45
46
47
48
49
# File 'lib/format_engine/engine.rb', line 43

def do_parse(src, dst, parse_spec_str)
  spec_info = SpecInfo.new(src, dst, nil, self, {})

  due_process(spec_info, parse_spec_str) do |fmt|
    fmt.do_parse(spec_info)
  end
end

#due_process(spec_info, spec_str, &block) ⇒ Object

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.



56
57
58
59
60
61
62
63
64
# File 'lib/format_engine/engine.rb', line 56

def due_process(spec_info, spec_str, &block)
  spec = FormatSpec.get_spec(spec_str).validate(self)

  spec_info.instance_exec(&self[:before])
  spec.specs.each(&block)
  spec_info.instance_exec(&self[:after])

  spec_info.dst
end