class FancyWriter::FancyIO

FancyIO is the main class used for creating formatted writers. See README.md for an exhaustive usage description.

Constants

DEFAULT_OPTIONS

This hash holds some default options that are used when no other options are passed to the constructor.

Attributes

caller[R]

An attribute holding the caller object.

custom_blocks[R]
custom_lines[R]

An attribute holding custom line configurations.

enum_quote[R]

The quote symbol to use for printing enumerables.

enum_separator[R]

The separator string to use for printing enumerables.

prefix_stack[R]

The stack for strings to be prepended to each line.

stream[R]

The internal stream (IO) to write formatted output to.

Public Class Methods

interpol(string, args) click to toggle source
# File lib/fancy_writer.rb, line 28
def self.interpol(string, args)
  result = string.gsub(/(?<!%)%(\w+)/) do
    args.has_key?($1.to_sym) ? args[$1.to_sym] : ''
  end
  result.gsub(/%%/, '%')
end
new(p_stream, opts={}, &block) click to toggle source

Initializes a new fancy writer instance that wraps around the given io object p_stream. The block contains the code for writing to that stream. @param p_stream [IO] The stream to write into. @option opts [String] :enum_separator The symbol to

be used to separate
values in enums.

@option opts [String] :enum_quote The symbol for quoting

values in enums.

@option opts [Object] :caller The calling object, useful

when a method of it must
be called inside FancyWriter's
blocks.

@param @block [Block] A block containing the

fancy writer code.
# File lib/fancy_writer.rb, line 79
def initialize(p_stream, opts={}, &block)
  @stream = p_stream
  @prefix_stack = []
  @custom_lines = Hash.new
  @custom_blocks = Hash.new
  effective_opts = DEFAULT_OPTIONS.merge(opts)
  @enum_separator = effective_opts[:enum_separator]
  @enum_quote = effective_opts[:enum_quote]
  @caller = effective_opts[:caller]
  if block_given?
    instance_eval &block
  end
end

Public Instance Methods

add_block_config(name, begin_pattern, end_pattern, indentation=2) click to toggle source
# File lib/fancy_writer.rb, line 113
def add_block_config(name, begin_pattern, end_pattern, indentation=2)
  @custom_blocks[name.to_sym] = [ begin_pattern, end_pattern, indentation ]
end
add_line_config(name, pattern) click to toggle source

Adds a new custom line configuration to the object.

# File lib/fancy_writer.rb, line 109
def add_line_config(name, pattern)
  @custom_lines[name.to_sym] = pattern
end
b(prefix, postfix, indentation=2, &block)

Adds an alias b for the block method.

Alias for: block
block(prefix, postfix, indentation=2, &block) click to toggle source
# File lib/fancy_writer.rb, line 181
def block(prefix, postfix, indentation=2, &block)
  line prefix
  indent indentation, &block
  line postfix                
end
Also aliased as: b
c(comment_string='

Adds an alias c for the comment method.

Alias for: comment
comment(comment_string=' click to toggle source

Prepends each line in the given block with Ruby-style comments (“# ”). Another comment character can be passed as a parameter. @param comment_string [String] The comment character(s) to

be prepended to each line in
the given block.
# File lib/fancy_writer.rb, line 135
def comment(comment_string='#', space_sep=true, &block)
  if block_given?
    if space_sep
      prepend("#{comment_string} ", &block)
    else
      prepend("#{comment_string}", &block)
    end
  end
end
Also aliased as: c
convert(&block) click to toggle source

Takes the given block and uses it to convert data. This method is useful if you want to do things to your FancyWriter object after initialization. @param @block [Block] A block containing the

fancy writer code.
# File lib/fancy_writer.rb, line 98
def convert(&block)
  if block_given?
    instance_eval &block
  end
end
e(p_enum)

Adds an alias e for the write_enum method.

Alias for: write_enum
i(number=2, &block)

Adds an alias i for the indent method.

Alias for: indent
indent(number=2, &block) click to toggle source

Indents each line in the given block with spaces. The default amount is 2, another number of spaces can be given as a parameter. @param number [Integer] The number of spaces to be used for

indentation in the given block.
# File lib/fancy_writer.rb, line 150
def indent(number=2, &block)
  prepend(' '*number, &block)
end
Also aliased as: i
line(*line)

Adds an alias line for the write method.

Alias for: write
prepend(prepend_string=' ') { || ... } click to toggle source

Adds a new string to the prepend stack. These strings will be added to each output line until the end of the block is reached. @param prepend_string [String] The string to be prepended

to each line in the given
block
# File lib/fancy_writer.rb, line 123
def prepend(prepend_string=' ', &block)
  @prefix_stack << prepend_string
  yield # &block
  @prefix_stack.pop
end
t(number=1, &block)

Adds an alias t for the tab_indent method.

Alias for: tab_indent
tab_indent(number=1, &block) click to toggle source

Indents each line in the given block with tabs. The default amount is 1, another number of tabs can be given as a parameter. @param number [Integer] The number of tabs to be used for

indentation in the given block.
# File lib/fancy_writer.rb, line 159
def tab_indent(number=1, &block)
  prepend("\t"*number, &block)
end
Also aliased as: t
w(*line)

Adds an alias w for the write method.

Alias for: write
write(*line) click to toggle source

Writes one or more lines to the output object, taking into account all strings on the prefix stack (such as comment symbols or indentations). @param line [Object] The object(s) to be formatted and written

to the underlying writer.
# File lib/fancy_writer.rb, line 168
def write(*line)
  lines = lines==[] ? [''] : line
  lines.each do |l|
    write_line(l)
  end
end
Also aliased as: w, line
write_enum(p_enum) click to toggle source

This is a helper method for writing a single enumerable. @param p_enum [Enumerable] the enumerable to format and write.

# File lib/fancy_writer.rb, line 177
def write_enum(p_enum)
  write_line(p_enum)
end
Also aliased as: e

Private Instance Methods

join_enumerable(enum) click to toggle source

Joins together an enumerable, using configuration options enum_quote to quote the single values, and enum_separator to glue them together. @param enum [Enumerable] the enumerable to format.

# File lib/fancy_writer.rb, line 250
def join_enumerable(enum)
  enum.collect{|e| "#{@enum_quote}#{e}#{@enum_quote}"}.join(@enum_separator)
end
method_missing(meth, *args, &block) click to toggle source

This method redirects failed message calls to the caller object, if present, in order to provide the user with method calls inside FancyWriter’s blocks.

Calls superclass method
# File lib/fancy_writer.rb, line 215
def method_missing(meth, *args, &block)
  if caller.respond_to?(meth)
    return caller.send(meth, *args, &block)
  end
  if @custom_lines.has_key?(meth)
    evaluated_pattern = FancyIO.interpol(@custom_lines[meth],args.first)
    return line(evaluated_pattern)
  end
  if @custom_blocks.has_key?(meth)
    evaluated_begin = FancyIO.interpol(@custom_blocks[meth][0], args.first)
    evaluated_end   = FancyIO.interpol(@custom_blocks[meth][1], args.first)
    return block(evaluated_begin, evaluated_end, @custom_blocks[meth][2], &block)
  end
  return super
end
write_line(line) click to toggle source

Internal method that performs the actual writing. @param line [Object] the line to be written.

# File lib/fancy_writer.rb, line 233
def write_line(line)
  if line.kind_of? String
    formatted_line = line
  elsif line.kind_of? Enumerable
    formatted_line = join_enumerable(line)
  else
    formatted_line = line.to_s
  end
  stream << @prefix_stack.join('')
  stream << formatted_line
  stream << "\n"
end