Module: FormatEngine::AttrParser

Defined in:
lib/format_engine/attr_parser.rb

Overview

This module adds the attr_parser extension to a class.

Instance Method Summary collapse

Instance Method Details

#attr_parser(method, library) ⇒ Object

Define a parser for the current class.
Parameters

  • method - A symbol used to name the parsing method created by this method.

  • library - A hash of parsing rules the define the parsing

capabilities supported by this parser.


Meta-effects

  • Creates a class method (named after the symbol in method) that parses in

a string and creates an instance of the class. The created method takes
two parameters:


Meta-method Parameters

  • src - A string of formatted data to be parsed.

  • spec_str - A format specification string with %x etc qualifiers.


Meta-method Returns

  • An instance of the host class.


Returns

  • The format engine used by this method.



22
23
24
25
26
27
28
29
30
31
# File 'lib/format_engine/attr_parser.rb', line 22

def attr_parser(method, library)
  engine = Engine.new(library)

  #Create a class method to do the parsing.
  define_singleton_method(method) do |src, spec_str|
    engine.do_parse(src, self, spec_str)
  end

  engine
end