module FormatEngine::AttrFormatter

This module adds the attr_formatter extension to a class.

Public Instance Methods

attr_formatter(method, library) click to toggle source

Define a formatter for instances of the current class.
Parameters

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

  • library - A hash of formatting rules that define the formatting capabilities supported by this formatter.


Meta-effects

  • Creates a method (named after the symbol in method) that formats the instance of the class. The created method takes one parameter:


Meta-method Parameters

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


Meta-method Returns

  • A formatted string


Returns

  • The format engine used by this method.

# File lib/format_engine/attr_formatter.rb, line 20
def attr_formatter(method, library)
  engine = Engine.new(library)

  #Create an instance method to do the formatting.
  define_method(method) do |spec_str|
    engine.do_format(self, spec_str)
  end

  engine
end