Class: FancyWriter::FancyIO
- Inherits:
-
Object
- Object
- FancyWriter::FancyIO
- 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
-
#caller ⇒ Object
readonly
An attribute holding the caller object.
-
#custom_blocks ⇒ Object
readonly
Returns the value of attribute custom_blocks.
-
#custom_lines ⇒ Object
readonly
An attribute holding custom line configurations.
-
#enum_quote ⇒ Object
readonly
The quote symbol to use for printing enumerables.
-
#enum_separator ⇒ Object
readonly
The separator string to use for printing enumerables.
-
#prefix_stack ⇒ Object
readonly
The stack for strings to be prepended to each line.
-
#stream ⇒ Object
readonly
The internal stream (IO) to write formatted output to.
Class Method Summary collapse
Instance Method Summary collapse
- #add_block_config(name, begin_pattern, end_pattern, indentation = 2) ⇒ Object
-
#add_line_config(name, pattern) ⇒ Object
Adds a new custom line configuration to the object.
- #block(prefix, postfix, indentation = 2, &block) ⇒ Object (also: #b)
-
#comment(comment_string = '#', space_sep = true, &block) ⇒ Object
(also: #c)
Prepends each line in the given block with Ruby-style comments (“# ”).
-
#convert(&block) ⇒ Object
Takes the given block and uses it to convert data.
-
#indent(number = 2, &block) ⇒ Object
(also: #i)
Indents each line in the given block with spaces.
-
#initialize(p_stream, opts = {}, &block) ⇒ FancyIO
constructor
Initializes a new fancy writer instance that wraps around the given io object
p_stream
. -
#prepend(prepend_string = ' ', &block) ⇒ Object
Adds a new string to the prepend stack.
-
#tab_indent(number = 1, &block) ⇒ Object
(also: #t)
Indents each line in the given block with tabs.
-
#write(*line) ⇒ Object
(also: #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).
-
#write_enum(p_enum) ⇒ Object
(also: #e)
This is a helper method for writing a single enumerable.
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.
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
#caller ⇒ Object (readonly)
An attribute holding the caller object.
55 56 57 |
# File 'lib/fancy_writer.rb', line 55 def caller @caller end |
#custom_blocks ⇒ Object (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_lines ⇒ Object (readonly)
An attribute holding custom line configurations.
58 59 60 |
# File 'lib/fancy_writer.rb', line 58 def custom_lines @custom_lines end |
#enum_quote ⇒ Object (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_separator ⇒ Object (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_stack ⇒ Object (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 |
#stream ⇒ Object (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.
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.
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.
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.
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.
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).
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.
177 178 179 |
# File 'lib/fancy_writer.rb', line 177 def write_enum(p_enum) write_line(p_enum) end |