fancy_writer 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/README.md +176 -3
- data/lib/fancy_writer.rb +62 -4
- data/lib/fancy_writer/version.rb +1 -1
- data/spec/fancy_writer/custom_lines_spec.rb +99 -0
- data/spec/fancy_writer/fancy_writer_spec.rb +63 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MmRkNGEyOWE1NGM5MjNiNWQzMGI5NmQ0YzVlMWNiYmMzZGNmZTdkMw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YTg5OTRjNjg5MjQxNDkwYTJhOTU0M2Y2YWFlOWQ5ZWRlMWI4NjFmNw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
M2Q5NTMyNzUxN2RhYzEwZTljZTczNWY4YjY2ODVjZjA0NGI4MGE4MDE0YTA2
|
10
|
+
YWUwMDA1OGQ0ZWY0ZmUyNGNkMGQ2MDU2MjVlNGE0MzEzMjk5MjIwN2FmNjMy
|
11
|
+
NjgxNzQzMThhNGY3NmMwNWRiM2EyOTBhOGI1OTc0OWEzNDAyNGQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OTU0YTBlMDMzY2JhYTA5MTI1YmZkNTlhYTYxMWU5ZDJhZTFmMWFlNDcyYTM5
|
14
|
+
YTgxZjE0MjFmMjIwMTQxZDE3NDgyYmFkNGIyYWFiYWJkNzlhYjczNGUzNGJh
|
15
|
+
NDBjNjZhMzM4MWEzYzFlNDIzODkyM2QzMWM3YmI0OThjYWM3NDg=
|
data/README.md
CHANGED
@@ -27,9 +27,9 @@ Or install it yourself as:
|
|
27
27
|
|
28
28
|
$ gem install fancy_writer
|
29
29
|
|
30
|
-
##
|
30
|
+
## Basic usage
|
31
31
|
|
32
|
-
###
|
32
|
+
### Writing lines
|
33
33
|
|
34
34
|
Given an IO object that is suitable for writing (such as a file or stdout),
|
35
35
|
FancyWriter can use this IO object for creating formatted output:
|
@@ -167,6 +167,50 @@ end
|
|
167
167
|
Additionally, eight spaces.
|
168
168
|
````
|
169
169
|
|
170
|
+
### Blocks
|
171
|
+
|
172
|
+
With "blocks" I refer to segments of text that have one single line that begins it and one line that ends it, and an additional body in between. Often, this
|
173
|
+
body is indented. The following three-line samples show different examples of
|
174
|
+
such blocks from different languages. Each first line is the beginning of a
|
175
|
+
block, each second line its body, each third line its ending:
|
176
|
+
|
177
|
+
```` HTML
|
178
|
+
<div>
|
179
|
+
<p>Lorem ipsum dolor sit amet...</p>
|
180
|
+
</div>
|
181
|
+
````
|
182
|
+
```` Latex
|
183
|
+
\begin{section}
|
184
|
+
Lorem ipsum dolor sit amet...
|
185
|
+
\end{section}
|
186
|
+
````
|
187
|
+
```` Bibtex
|
188
|
+
@article{User1999,
|
189
|
+
author = {Joe User}
|
190
|
+
}
|
191
|
+
````
|
192
|
+
|
193
|
+
FancyWriter provides the `block` method (shortcut: `b`) to ease the process
|
194
|
+
of creating such blocks. This method expects two or three parameters and a
|
195
|
+
block. The first two parameters indicate the beginning and ending line,
|
196
|
+
respectively. The third parameter is optional, it indicates the number of
|
197
|
+
spaces to be used for indentation (its default is 2 spaces).
|
198
|
+
|
199
|
+
So, the first example above (the HTML one) can be produced with FancyWriter
|
200
|
+
as follows:
|
201
|
+
|
202
|
+
```` Ruby
|
203
|
+
FancyWriter::FancyIO.new(io) do
|
204
|
+
block '<div>', '</div>' do
|
205
|
+
line '<p>Lorem ipsum dolor sit amet...</p>'
|
206
|
+
end
|
207
|
+
end
|
208
|
+
````
|
209
|
+
|
210
|
+
While this feature in general does not appear to be very helpful, it can
|
211
|
+
be used in combination with custom patterns (see below) to produce blocks
|
212
|
+
dynamically.
|
213
|
+
|
170
214
|
### Symbol-separated values
|
171
215
|
|
172
216
|
If you pass an Enumerable to `write_enum` (short version: `e`), the underlying
|
@@ -201,7 +245,7 @@ Note that also for this method, surrounding `comment` or indentation methods
|
|
201
245
|
are evaluated, meaning that also formatted number sequences will be indented
|
202
246
|
or commented out.
|
203
247
|
|
204
|
-
## An exhaustive example
|
248
|
+
## An exhaustive example of basic usage
|
205
249
|
|
206
250
|
This is a somewhat artifical example, but it contains most of the methods
|
207
251
|
in action.
|
@@ -242,6 +286,114 @@ config:
|
|
242
286
|
5,6,7,8
|
243
287
|
````
|
244
288
|
|
289
|
+
## Advanced usage
|
290
|
+
|
291
|
+
### Custom line patterns
|
292
|
+
|
293
|
+
If you are working with file formats that use repeated patterns (usually
|
294
|
+
resulting from the language used), you can make your work even easier
|
295
|
+
by defining *named patterns*. These are basically strings with some
|
296
|
+
placeholders in them, which you can use to generate lines where these
|
297
|
+
placeholders are substituted with parameters.
|
298
|
+
|
299
|
+
With the possibility to generate named patterns, you can create your
|
300
|
+
own small DSL for the file format you are working with. This can
|
301
|
+
save time and make your code more legible.
|
302
|
+
|
303
|
+
Let me give an example with the following segment of an Apache configuration
|
304
|
+
file (let us further assume that you somehow need to generate these
|
305
|
+
config files in your Ruby scripts from some source):
|
306
|
+
|
307
|
+
```` conf
|
308
|
+
User wwwrun
|
309
|
+
Listen 80
|
310
|
+
DocumentRoot "/srv/www/htdocs"
|
311
|
+
LoadModule cgi_module modules/mod_cgi.so
|
312
|
+
LoadModule mime_module modules/mod_mime.so
|
313
|
+
LoadModule negotiation_module modules/mod_negotiation.so
|
314
|
+
LoadModule status_module modules/mod_status.so
|
315
|
+
````
|
316
|
+
|
317
|
+
Two things are important here: this configuration file follows a predefined syntax, and there are elements that repeat themselves. For these, you could define named patterns which serve several purposes: You do not need to spell out the complete line, you avoid repetition of formatting, and you can reduce errors. If you look at the end of the sample, you see many similar lines. If you define one pattern for them, you follow the DRY principle, you reduce the number of places to look for if a bug occurs, and you get a cleaner way of generating the file contents.
|
318
|
+
|
319
|
+
If you want to use named patterns, you need to delay the calling of the formatting method, because you need to insert the configuration fist. Thus, you do not call the formatting method directly on the newly created object as in the examples above, or like this:
|
320
|
+
|
321
|
+
```` Ruby
|
322
|
+
FancyWriter::FancyIO.new(io) do
|
323
|
+
# calls to line, block, comment, etc.
|
324
|
+
end
|
325
|
+
````
|
326
|
+
|
327
|
+
Instead, you assign the FancyWriter instance to a variable, perform the configuration, and finally call `convert` on the instance and give it a block with your formatting wishes:
|
328
|
+
|
329
|
+
```` Ruby
|
330
|
+
@writer = FancyWriter::FancyIO.new(io)
|
331
|
+
# Do your named pattern configuration here
|
332
|
+
@writer.add_line_config :load_module, "LoadModule %module modules/%file.so")
|
333
|
+
@writer.convert do
|
334
|
+
# NOW, we can start writing!
|
335
|
+
# insert your calls to line, block, comment, etc.
|
336
|
+
end
|
337
|
+
````
|
338
|
+
|
339
|
+
In this example, you can see how a named pattern can be inserted using the
|
340
|
+
`add_line_config` method. It expects a symbol (containing the name of the pattern) and the pattern itself. The symbol should follow the rules for method names in Ruby. You will see why in a few moments.
|
341
|
+
|
342
|
+
The named pattern follows a convention similar to the one used for the `sprintf` string formatting. The difference is that *names* are used, such as `%method`. These names will, during text generation, be replaced with matching values from the hash passed to your custom line method. Thus, if you pass a hash containing the entry `method: "mime"`, then all occurrences of `%method` in that pattern will be replaced with `mime`. Literal `%` characters can be expressed by doubling them in the pattern: `%%`.
|
343
|
+
|
344
|
+
The pattern defined above basically inserts a line starting with 'LoadModule', and then a formatting of two parameters that you can give it during calling. You will be able to call this named pattern as if you call the pattern name as a method (that's why the pattern name should follow these rules).
|
345
|
+
|
346
|
+
You can generate the four module configurations from the example with this code:
|
347
|
+
|
348
|
+
```` Ruby
|
349
|
+
@writer = FancyWriter::FancyIO.new(io)
|
350
|
+
# Do your named pattern configuration here
|
351
|
+
@writer.add_line_config :load_module, "LoadModule %module modules/%file.so"
|
352
|
+
@writer.convert do
|
353
|
+
load_module module: 'cgi_module', file: 'mod_cgi'
|
354
|
+
load_module module: 'mime_module', file: 'mod_mime'
|
355
|
+
load_module module: 'negotiation_module', file: 'mod_negotiation'
|
356
|
+
load_module module: 'status_module', file: 'mod_status'
|
357
|
+
end
|
358
|
+
````
|
359
|
+
|
360
|
+
In a similar fashion, you could add patterns for the other lines in the configuration file.
|
361
|
+
|
362
|
+
### Custom block patterns
|
363
|
+
|
364
|
+
Similar to this, you can also add patterns that create custom blocks. In these blocks, the beginning *and* ending text will be interpolated with the values you pass to the block. The functionality is similar to the one used for custom line patterns, with the following exceptions:
|
365
|
+
|
366
|
+
You configure a new block as follows:
|
367
|
+
```` Ruby
|
368
|
+
@writer.add_block_config block_name, begin_pattern, end_pattern, indentation
|
369
|
+
````
|
370
|
+
|
371
|
+
- `block_name` is the symbol to be used for the method call.
|
372
|
+
- `begin_pattern` and `end_pattern` are patterns for the strings that you want to use to surround your block. Here, you can add `%field` definitions in places where you want to put a variable value.
|
373
|
+
- `indentation` declares the number of spaces to use for block indentation.
|
374
|
+
|
375
|
+
The difference in calling a block pattern is that you pass a (Ruby) block to it that contains rules for creating the body of the block:
|
376
|
+
|
377
|
+
```` Ruby
|
378
|
+
@writer.add_block_config :xml_tag, '<%tagname>', '</%tagname>', 2
|
379
|
+
@writer.convert do1
|
380
|
+
xml_tag tagname: 'div' do
|
381
|
+
xml_tag tagname: 'p' do
|
382
|
+
line 'Hello world!'
|
383
|
+
end
|
384
|
+
end
|
385
|
+
end
|
386
|
+
````
|
387
|
+
thus produces
|
388
|
+
````XML
|
389
|
+
<div>
|
390
|
+
<p>
|
391
|
+
Hello World!
|
392
|
+
</p>
|
393
|
+
</div>
|
394
|
+
````
|
395
|
+
|
396
|
+
*(Of course there are dozens of better and more efficient ways to actually output XML, this is supposed to be just a comprehensible example.)*
|
245
397
|
|
246
398
|
## Issues
|
247
399
|
|
@@ -275,6 +427,27 @@ ideally by creating an issue on Github.
|
|
275
427
|
|
276
428
|
Please browse existing issues first, to avoid double postings.
|
277
429
|
|
430
|
+
## Release history
|
431
|
+
|
432
|
+
### 1.0.2 – named pattern support (6 Feb 2015)
|
433
|
+
|
434
|
+
- __[#4](https://github.com/pmenke/fancy_writer/issues/4) implemented.__
|
435
|
+
Adds support for custom line patterns
|
436
|
+
- __[#2](https://github.com/pmenke/fancy_writer/issues/2) implemented.__
|
437
|
+
Adds support for custom block patterns
|
438
|
+
|
439
|
+
|
440
|
+
### 1.0.1 – block support (6 Feb 2015)
|
441
|
+
|
442
|
+
- __[#1](https://github.com/pmenke/fancy_writer/issues/1) implemented.__
|
443
|
+
Adds support for blocks
|
444
|
+
- __[#3](https://github.com/pmenke/fancy_writer/issues/3) implemented.__
|
445
|
+
Minor code improvements
|
446
|
+
|
447
|
+
### 1.0.0 – initial version (28 Mar 2014)
|
448
|
+
|
449
|
+
- First version of the gem, basic support for lines, comments, indentation.
|
450
|
+
|
278
451
|
## Contributing
|
279
452
|
|
280
453
|
Any ideas are welcome! Either
|
data/lib/fancy_writer.rb
CHANGED
@@ -25,6 +25,13 @@ module FancyWriter
|
|
25
25
|
# See README.md for an exhaustive usage description.
|
26
26
|
class FancyIO
|
27
27
|
|
28
|
+
def self.interpol(string, args)
|
29
|
+
result = string.gsub(/(?<!%)%(\w+)/) do
|
30
|
+
args.has_key?($1.to_sym) ? args[$1.to_sym] : ''
|
31
|
+
end
|
32
|
+
result.gsub(/%%/, '%')
|
33
|
+
end
|
34
|
+
|
28
35
|
# This hash holds some default options that are used when
|
29
36
|
# no other options are passed to the constructor.
|
30
37
|
DEFAULT_OPTIONS = {
|
@@ -47,6 +54,11 @@ module FancyWriter
|
|
47
54
|
# An attribute holding the caller object.
|
48
55
|
attr_reader :caller
|
49
56
|
|
57
|
+
# An attribute holding custom line configurations.
|
58
|
+
attr_reader :custom_lines
|
59
|
+
|
60
|
+
attr_reader :custom_blocks
|
61
|
+
|
50
62
|
|
51
63
|
# Initializes a new fancy writer instance that wraps
|
52
64
|
# around the given io object +p_stream+.
|
@@ -67,6 +79,8 @@ module FancyWriter
|
|
67
79
|
def initialize(p_stream, opts={}, &block)
|
68
80
|
@stream = p_stream
|
69
81
|
@prefix_stack = []
|
82
|
+
@custom_lines = Hash.new
|
83
|
+
@custom_blocks = Hash.new
|
70
84
|
effective_opts = DEFAULT_OPTIONS.merge(opts)
|
71
85
|
@enum_separator = effective_opts[:enum_separator]
|
72
86
|
@enum_quote = effective_opts[:enum_quote]
|
@@ -76,6 +90,30 @@ module FancyWriter
|
|
76
90
|
end
|
77
91
|
end
|
78
92
|
|
93
|
+
# Takes the given block and uses it to convert data. This
|
94
|
+
# method is useful if you want to do things to your
|
95
|
+
# FancyWriter object after initialization.
|
96
|
+
# @param @block [Block] A block containing the
|
97
|
+
# fancy writer code.
|
98
|
+
def convert(&block)
|
99
|
+
if block_given?
|
100
|
+
instance_eval &block
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# # # # # # # # # # # # # # # # # #
|
105
|
+
# Further setup of the object
|
106
|
+
# # # # # # # # # # # # # # # # # #
|
107
|
+
|
108
|
+
# Adds a new custom line configuration to the object.
|
109
|
+
def add_line_config(name, pattern)
|
110
|
+
@custom_lines[name.to_sym] = pattern
|
111
|
+
end
|
112
|
+
|
113
|
+
def add_block_config(name, begin_pattern, end_pattern, indentation=2)
|
114
|
+
@custom_blocks[name.to_sym] = [ begin_pattern, end_pattern, indentation ]
|
115
|
+
end
|
116
|
+
|
79
117
|
# Adds a new string to the prepend stack. These strings
|
80
118
|
# will be added to each output line until the end of the
|
81
119
|
# block is reached.
|
@@ -140,6 +178,13 @@ module FancyWriter
|
|
140
178
|
write_line(p_enum)
|
141
179
|
end
|
142
180
|
|
181
|
+
def block(prefix, postfix, indentation=2, &block)
|
182
|
+
line prefix
|
183
|
+
indent indentation, &block
|
184
|
+
line postfix
|
185
|
+
end
|
186
|
+
|
187
|
+
|
143
188
|
# Adds an alias +w+ for the +write+ method.
|
144
189
|
alias :w :write
|
145
190
|
|
@@ -158,6 +203,9 @@ module FancyWriter
|
|
158
203
|
# Adds an alias +e+ for the +write_enum+ method.
|
159
204
|
alias :e :write_enum
|
160
205
|
|
206
|
+
# Adds an alias +b+ for the +block+ method.
|
207
|
+
alias :b :block
|
208
|
+
|
161
209
|
|
162
210
|
private
|
163
211
|
|
@@ -166,10 +214,18 @@ module FancyWriter
|
|
166
214
|
# user with method calls inside FancyWriter's blocks.
|
167
215
|
def method_missing(meth, *args, &block)
|
168
216
|
if caller.respond_to?(meth)
|
169
|
-
caller.send(meth, *args, &block)
|
170
|
-
|
171
|
-
|
217
|
+
return caller.send(meth, *args, &block)
|
218
|
+
end
|
219
|
+
if @custom_lines.has_key?(meth)
|
220
|
+
evaluated_pattern = FancyIO.interpol(@custom_lines[meth],args.first)
|
221
|
+
return line(evaluated_pattern)
|
222
|
+
end
|
223
|
+
if @custom_blocks.has_key?(meth)
|
224
|
+
evaluated_begin = FancyIO.interpol(@custom_blocks[meth][0], args.first)
|
225
|
+
evaluated_end = FancyIO.interpol(@custom_blocks[meth][1], args.first)
|
226
|
+
return block(evaluated_begin, evaluated_end, @custom_blocks[meth][2], &block)
|
172
227
|
end
|
228
|
+
return super
|
173
229
|
end
|
174
230
|
|
175
231
|
# Internal method that performs the actual writing.
|
@@ -182,7 +238,9 @@ module FancyWriter
|
|
182
238
|
else
|
183
239
|
formatted_line = line.to_s
|
184
240
|
end
|
185
|
-
stream <<
|
241
|
+
stream << @prefix_stack.join('')
|
242
|
+
stream << formatted_line
|
243
|
+
stream << "\n"
|
186
244
|
end
|
187
245
|
|
188
246
|
# Joins together an enumerable, using configuration options
|
data/lib/fancy_writer/version.rb
CHANGED
@@ -0,0 +1,99 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# This file is part of the fancy_writer gem.
|
3
|
+
# Copyright (c) 2014 Peter Menke.
|
4
|
+
# http://www.petermenke.de
|
5
|
+
#
|
6
|
+
# fancy_writer is free software: you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation, either
|
9
|
+
# version 3 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# fancy_writer is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with fancy_writer. If not, see
|
18
|
+
# <http://www.gnu.org/licenses/>.
|
19
|
+
require 'spec_helper'
|
20
|
+
|
21
|
+
include FancyWriter
|
22
|
+
|
23
|
+
describe FancyWriter::FancyIO do
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
@string = String.new
|
27
|
+
@writer = FancyIO.new(@string)
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'Custom lines' do
|
31
|
+
|
32
|
+
it 'can store custom line configurations' do
|
33
|
+
@writer.add_line_config(:custom, "Hi %s")
|
34
|
+
@writer.custom_lines.should_not be nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'stores correct info for custom line configurations' do
|
38
|
+
@writer.add_line_config(:custom, "Hi %s")
|
39
|
+
@writer.custom_lines.has_key?(:custom).should be true
|
40
|
+
@writer.custom_lines[:custom].should eq "Hi %s"
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'uses a stored line to produce correct output' do
|
44
|
+
@writer.add_line_config(:custom, "Hi %who")
|
45
|
+
@writer.convert do
|
46
|
+
custom who: 'ho'
|
47
|
+
end
|
48
|
+
@string.should eq "Hi ho\n"
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'converts also multi-value, complex patterns' do
|
52
|
+
@writer.add_line_config :load_module, "LoadModule %module modules/%file.so"
|
53
|
+
@writer.convert do
|
54
|
+
load_module module: 'mime_module', file: 'mod_mime'
|
55
|
+
end
|
56
|
+
@string.should eq "LoadModule mime_module modules/mod_mime.so\n"
|
57
|
+
puts @string
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'converts patterns with special characters' do
|
61
|
+
@writer.add_line_config :node, "\\node at (%x,%y) (%name) {%label};"
|
62
|
+
@writer.convert do
|
63
|
+
node x: 4, y: 2, name: 'node1', label: 'Source'
|
64
|
+
end
|
65
|
+
@string.should eq "\\node at (4,2) (node1) {Source};\n"
|
66
|
+
puts @string
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'Custom blocks' do
|
72
|
+
|
73
|
+
it 'can store custom block configurations' do
|
74
|
+
@writer.add_block_config(:custom2, "begin", "end", 4)
|
75
|
+
@writer.custom_blocks.should_not be nil
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'stores correct info for custom block configurations' do
|
79
|
+
@writer.add_block_config(:custom2, "begin", "end", 4)
|
80
|
+
@writer.custom_blocks.has_key?(:custom2).should be true
|
81
|
+
@writer.custom_blocks[:custom2][0].should eq "begin"
|
82
|
+
@writer.custom_blocks[:custom2][1].should eq "end"
|
83
|
+
@writer.custom_blocks[:custom2][2].should eq 4
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'uses a stored block to produce correct output' do
|
87
|
+
@writer.add_block_config(:custom2, "begin %name", "end %name", 4)
|
88
|
+
@writer.convert do
|
89
|
+
custom2 name: 'Heidi' do
|
90
|
+
line 'Peter'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
@string.should eq "begin Heidi\n Peter\nend Heidi\n"
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -30,6 +30,44 @@ describe FancyWriter::FancyIO do
|
|
30
30
|
|
31
31
|
context 'FancyWriter' do
|
32
32
|
|
33
|
+
context 'Static methods' do
|
34
|
+
|
35
|
+
it 'performs basic interpolation correctly' do
|
36
|
+
@pattern = 'The %speed %color fox'
|
37
|
+
@args = { speed: 'quick', color: 'brown'}
|
38
|
+
@result = FancyIO.interpol(@pattern, @args)
|
39
|
+
@result.should eq "The quick brown fox"
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'respects escaped percent signs' do
|
43
|
+
@pattern = 'The %%speed %color fox'
|
44
|
+
@args = { speed: 'quick', color: 'brown'}
|
45
|
+
@result = FancyIO.interpol(@pattern, @args)
|
46
|
+
@result.should eq "The %speed brown fox"
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'respects escaped percent signs even in the very beginning' do
|
50
|
+
@pattern = '%%speed %color fox'
|
51
|
+
@args = { speed: 'quick', color: 'brown'}
|
52
|
+
@result = FancyIO.interpol(@pattern, @args)
|
53
|
+
@result.should eq "%speed brown fox"
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'Initialization' do
|
59
|
+
|
60
|
+
it 'can take additional config and then be run with convert method' do
|
61
|
+
@writer = FancyIO.new(@string)
|
62
|
+
in_between = @writer.class.name
|
63
|
+
@writer.convert do
|
64
|
+
w "convert"
|
65
|
+
end
|
66
|
+
@string.should eq "convert\n"
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
33
71
|
context 'Basics' do
|
34
72
|
it 'writes lines without method as they are' do
|
35
73
|
@writer = FancyIO.new(@string) do
|
@@ -115,6 +153,31 @@ describe FancyWriter::FancyIO do
|
|
115
153
|
|
116
154
|
end
|
117
155
|
|
156
|
+
context 'Blocks' do
|
157
|
+
|
158
|
+
it 'produces correct blocks' do
|
159
|
+
@writer = FancyIO.new(@string) do
|
160
|
+
line 'before'
|
161
|
+
block 'begin', 'end' do
|
162
|
+
line 'inside'
|
163
|
+
end
|
164
|
+
line 'after'
|
165
|
+
end
|
166
|
+
@string.should eq "before\nbegin\n inside\nend\nafter\n"
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'produces correct blocks with custom indentation settings' do
|
170
|
+
@writer = FancyIO.new(@string) do
|
171
|
+
line 'before'
|
172
|
+
block 'begin', 'end', 4 do
|
173
|
+
line 'inside'
|
174
|
+
end
|
175
|
+
line 'after'
|
176
|
+
end
|
177
|
+
@string.should eq "before\nbegin\n inside\nend\nafter\n"
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
118
181
|
|
119
182
|
it 'exports a complex example correctly' do
|
120
183
|
@writer = FancyIO.new(@string) do
|
@@ -138,7 +201,6 @@ describe FancyWriter::FancyIO do
|
|
138
201
|
end
|
139
202
|
end
|
140
203
|
end
|
141
|
-
puts @string
|
142
204
|
end
|
143
205
|
end
|
144
206
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fancy_writer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Menke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- fancy_writer.gemspec
|
70
70
|
- lib/fancy_writer.rb
|
71
71
|
- lib/fancy_writer/version.rb
|
72
|
+
- spec/fancy_writer/custom_lines_spec.rb
|
72
73
|
- spec/fancy_writer/fancy_writer_spec.rb
|
73
74
|
- spec/spec_helper.rb
|
74
75
|
homepage: https://github.com/pmenke/fancy_writer
|
@@ -91,10 +92,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
92
|
version: '0'
|
92
93
|
requirements: []
|
93
94
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.
|
95
|
+
rubygems_version: 2.4.2
|
95
96
|
signing_key:
|
96
97
|
specification_version: 4
|
97
98
|
summary: A simple IO wrapper for easier comment blocks, indentation and CSV output.
|
98
99
|
test_files:
|
100
|
+
- spec/fancy_writer/custom_lines_spec.rb
|
99
101
|
- spec/fancy_writer/fancy_writer_spec.rb
|
100
102
|
- spec/spec_helper.rb
|
103
|
+
has_rdoc:
|