Class: FancyWriter::FancyIO

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

Overview

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

Constant Summary collapse

DEFAULT_OPTIONS =

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

{
  enum_separator: ',',
  enum_quote: ''
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p_stream, opts = {}, &block) ⇒ FancyIO

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.

Parameters:

  • p_stream (IO)

    The stream to write into.

  • @block (Block)

    A block containing the fancy writer code.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :enum_separator (String)

    The symbol to be used to separate values in enums.

  • :enum_quote (String)

    The symbol for quoting values in enums.

  • :caller (Object)

    The calling object, useful when a method of it must be called inside FancyWriter’s blocks.



79
80
81
82
83
84
85
86
87
88
89
90
91
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

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.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# 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

Instance Attribute Details

#callerObject (readonly)

An attribute holding the caller object.



55
56
57
# File 'lib/fancy_writer.rb', line 55

def caller
  @caller
end

#custom_blocksObject (readonly)

Returns the value of attribute custom_blocks.



60
61
62
# File 'lib/fancy_writer.rb', line 60

def custom_blocks
  @custom_blocks
end

#custom_linesObject (readonly)

An attribute holding custom line configurations.



58
59
60
# File 'lib/fancy_writer.rb', line 58

def custom_lines
  @custom_lines
end

#enum_quoteObject (readonly)

The quote symbol to use for printing enumerables.



52
53
54
# File 'lib/fancy_writer.rb', line 52

def enum_quote
  @enum_quote
end

#enum_separatorObject (readonly)

The separator string to use for printing enumerables.



49
50
51
# File 'lib/fancy_writer.rb', line 49

def enum_separator
  @enum_separator
end

#prefix_stackObject (readonly)

The stack for strings to be prepended to each line.



46
47
48
# File 'lib/fancy_writer.rb', line 46

def prefix_stack
  @prefix_stack
end

#streamObject (readonly)

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



43
44
45
# File 'lib/fancy_writer.rb', line 43

def stream
  @stream
end

Class Method Details

.interpol(string, args) ⇒ Object



28
29
30
31
32
33
# 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

Instance Method Details

#add_block_config(name, begin_pattern, end_pattern, indentation = 2) ⇒ Object



113
114
115
# 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) ⇒ Object

Adds a new custom line configuration to the object.



109
110
111
# File 'lib/fancy_writer.rb', line 109

def add_line_config(name, pattern)
  @custom_lines[name.to_sym] = pattern
end

#block(prefix, postfix, indentation = 2, &block) ⇒ Object Also known as: b



181
182
183
184
185
# File 'lib/fancy_writer.rb', line 181

def block(prefix, postfix, indentation=2, &block)
  line prefix
  indent indentation, &block
  line postfix                
end

#comment(comment_string = '#', space_sep = true, &block) ⇒ Object Also known as: c

Prepends each line in the given block with Ruby-style comments (“# ”). Another comment character can be passed as a parameter.

Parameters:

  • comment_string (String) (defaults to: '#')

    The comment character(s) to be prepended to each line in the given block.



135
136
137
138
139
140
141
142
143
# 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

#convert(&block) ⇒ Object

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.

Parameters:

  • @block (Block)

    A block containing the fancy writer code.



98
99
100
101
102
# File 'lib/fancy_writer.rb', line 98

def convert(&block)
  if block_given?
    instance_eval &block
  end
end

#indent(number = 2, &block) ⇒ Object Also known as: i

Indents each line in the given block with spaces. The default amount is 2, another number of spaces can be given as a parameter.

Parameters:

  • number (Integer) (defaults to: 2)

    The number of spaces to be used for indentation in the given block.



150
151
152
# File 'lib/fancy_writer.rb', line 150

def indent(number=2, &block)
  prepend(' '*number, &block)
end

#prepend(prepend_string = ' ', &block) ⇒ Object

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.

Parameters:

  • prepend_string (String) (defaults to: ' ')

    The string to be prepended to each line in the given block



123
124
125
126
127
# File 'lib/fancy_writer.rb', line 123

def prepend(prepend_string=' ', &block)
  @prefix_stack << prepend_string
  yield # &block
  @prefix_stack.pop
end

#tab_indent(number = 1, &block) ⇒ Object Also known as: t

Indents each line in the given block with tabs. The default amount is 1, another number of tabs can be given as a parameter.

Parameters:

  • number (Integer) (defaults to: 1)

    The number of tabs to be used for indentation in the given block.



159
160
161
# File 'lib/fancy_writer.rb', line 159

def tab_indent(number=1, &block)
  prepend("\t"*number, &block)
end

#write(*line) ⇒ Object Also known as: w, line

Writes one or more lines to the output object, taking into account all strings on the prefix stack (such as comment symbols or indentations).

Parameters:

  • line (Object)

    The object(s) to be formatted and written to the underlying writer.



168
169
170
171
172
173
# File 'lib/fancy_writer.rb', line 168

def write(*line)
  lines = lines==[] ? [''] : line
  lines.each do |l|
    write_line(l)
  end
end

#write_enum(p_enum) ⇒ Object Also known as: e

This is a helper method for writing a single enumerable.

Parameters:

  • p_enum (Enumerable)

    the enumerable to format and write.



177
178
179
# File 'lib/fancy_writer.rb', line 177

def write_enum(p_enum)
  write_line(p_enum)
end