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
An attribute holding the caller object.
An attribute holding custom line configurations.
The quote symbol to use for printing enumerables.
The separator string to use for printing enumerables.
The stack for strings to be prepended to each line.
The internal stream (IO) to write formatted output to.
Public Class Methods
# 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
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
# 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
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
# File lib/fancy_writer.rb, line 181 def block(prefix, postfix, indentation=2, &block) line prefix indent indentation, &block line postfix end
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
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
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
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
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
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
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
Private Instance Methods
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
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.
# 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
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