indentation 0.0.7 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.rdoc +22 -5
- data/lib/indentation.rb +0 -3
- data/lib/indentation/array_mod.rb +21 -0
- data/lib/indentation/meta_info.rb +1 -1
- data/spec/indentation_spec.rb +14 -93
- data/spec/rdoc_examples_spec.rb +9 -0
- data/spec/spec_helper.rb +47 -0
- metadata +7 -7
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDFlODVkZWY2ZTI0ZDA0NzJiN2M4MWJlM2JlZTViYzY4NGExY2QxZg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
Y2E4YTVkNDNlMTQyNTUzNTA5NDlkY2EzYTIzNTUxZmE2N2FkOGVjMw==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NjQwNTAyN2Y1OGZjZTVkNWQ5MzZiYmFjNTY0NGY4MWViZmFmODRkYTMyZWRm
|
10
|
+
YWZhN2ViNTg2ZGYwMDkzOTQxMGE3YjA5ODExYjAxZmJmNTVjMGVlNTA2OTJj
|
11
|
+
NTE4Y2I1NjAwODlkZTEyNThhYTAyY2Q5NjgxMTczMDY1ZTNmZDQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OTNkMTViZWVmMDU2MmEyNzMzYzIzZTIxY2E3ZjhlYzI4NjdkNzQxNzJlODg2
|
14
|
+
ZTQ2NTI4NjI1ZGI4NmVkZGMwOTJkNTI0ZjQ0MWM5YTAzMDhmMmUxNzhmNDgy
|
15
|
+
N2RlYWU2MzUyMGI4MjZkYWY2NTY3MWVkMzE5ZWFkNjg4YmM0NWM=
|
data/README.rdoc
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
A small library of extensions to Ruby's Array and String classes that allow indentation manipulation of Strings and Arrays of Strings. Has the capability of working with multi-line strings. If you frequently use String arrays to manipulate text, see synopsis (In README.rdoc) for examples of how indentation can make your life easier.
|
8
8
|
|
9
|
-
==
|
9
|
+
== EXAMPLES:
|
10
10
|
|
11
11
|
=== Indent
|
12
12
|
|
@@ -53,7 +53,7 @@ A small library of extensions to Ruby's Array and String classes that allow inde
|
|
53
53
|
|
54
54
|
# Accepts an indentation modifier
|
55
55
|
" one\n two".reset_indentation(1) # => " one\n two"
|
56
|
-
" one\n two".reset_indentation(0) # => " one\ntwo"
|
56
|
+
" one\n two".reset_indentation(0) # => " one\ntwo" # Default behavior
|
57
57
|
" one\n two".reset_indentation(-1) # => "one\ntwo"
|
58
58
|
|
59
59
|
|
@@ -111,12 +111,29 @@ A small library of extensions to Ruby's Array and String classes that allow inde
|
|
111
111
|
" three\n".find_least_indentation(:ignore_blank_lines => false) # => 0
|
112
112
|
|
113
113
|
# Option to ignore both blank lines (no characters whatsoever) and empty lines (whitespace-only)
|
114
|
-
#
|
114
|
+
# Implies :ignore_blank_lines => true when enabled
|
115
|
+
# Default => true
|
115
116
|
" three\n ".find_least_indentation # => 3
|
116
117
|
" three\n ".find_least_indentation(:ignore_empty_lines => false) # => 1
|
117
|
-
" three\n".find_least_indentation
|
118
|
-
" three\n".find_least_indentation(:ignore_empty_lines => false
|
118
|
+
" three\n".find_least_indentation # => 3
|
119
|
+
" three\n".find_least_indentation(:ignore_empty_lines => false) # => 0
|
120
|
+
|
121
|
+
# To ignore blank, but not empty, lines:
|
122
|
+
" three\n ".find_least_indentation(:ignore_blank_lines => true, :ignore_empty_lines => false) # => 1
|
123
|
+
" three\n".find_least_indentation(:ignore_blank_lines => true, :ignore_empty_lines => false) # => 3
|
124
|
+
|
125
|
+
=== English Array Join
|
126
|
+
|
127
|
+
# Given an Array of Strings and an optional conjunction, joins them using English list punctuation
|
128
|
+
# to find the least indentation within a multi-line String
|
129
|
+
['one', 'two'].english_join # => "one and two"
|
130
|
+
['one', 'two', 'three'].english_join # => "one, two, and three"
|
131
|
+
|
132
|
+
# Different conjunction
|
133
|
+
['one', 'two', 'three'].english_join('or') # => "one, two, or three"
|
119
134
|
|
135
|
+
# Different separator
|
136
|
+
['one', 'two', 'three'].english_join('or', ' ') # => "one two or three"
|
120
137
|
|
121
138
|
== REQUIREMENTS:
|
122
139
|
|
data/lib/indentation.rb
CHANGED
@@ -71,4 +71,25 @@ class Array
|
|
71
71
|
indent!(-find_least_indentation + modifier)
|
72
72
|
end
|
73
73
|
|
74
|
+
# Join an array of strings using English list punctuation.
|
75
|
+
def english_join(conjunction = 'and', separator = ', ', oxford_comma = true)
|
76
|
+
len = self.length
|
77
|
+
return '' if len == 0
|
78
|
+
return self[0].to_s if len == 1
|
79
|
+
return "#{self[0].to_s} #{conjunction} #{self[1].to_s}" if len == 2
|
80
|
+
join_str = ''
|
81
|
+
self.each_with_index{|ele, i|
|
82
|
+
str = if !oxford_comma && i == len - 2
|
83
|
+
"#{ele} #{conjunction} "
|
84
|
+
elsif i == len - 2
|
85
|
+
"#{ele}#{separator}#{conjunction} "
|
86
|
+
elsif i == len - 1
|
87
|
+
"#{ele}"
|
88
|
+
else
|
89
|
+
"#{ele}#{separator}"
|
90
|
+
end
|
91
|
+
join_str << str
|
92
|
+
}
|
93
|
+
join_str
|
94
|
+
end
|
74
95
|
end
|
data/spec/indentation_spec.rb
CHANGED
@@ -279,104 +279,25 @@ describe "Reset Indentation function" do
|
|
279
279
|
array.reset_indentation!.should == ["This", " is", "a test"]
|
280
280
|
array.should == ["This", " is", "a test"]
|
281
281
|
end
|
282
|
-
|
283
282
|
end
|
284
283
|
|
285
|
-
describe "
|
286
|
-
it "should
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
# Amount of indentation can be changed
|
292
|
-
"test".indent(3).should == " test"
|
293
|
-
|
294
|
-
# Indentation character (or string) is set as the second parameter of indent.
|
295
|
-
"test".indent(2, "\t").should == "\t\ttest"
|
296
|
-
|
297
|
-
# Operates on multi-line strings
|
298
|
-
"this\nis\na\ntest".indent.should == " this\n is\n a\n test"
|
299
|
-
|
300
|
-
# Indent method accepts negative values (Removes tabs, spaces, and supplied indentation string)
|
301
|
-
" test".indent(-1).should == " test"
|
302
|
-
"\t test".indent(-5).should == "test"
|
303
|
-
"\t-- Test".indent(-10).should == "-- Test"
|
304
|
-
"\t-- Test".indent(-10, '-').should == "Test"
|
305
|
-
"--- Test".indent(-2, '--').should == "- Test"
|
306
|
-
|
307
|
-
# Operates on arrays
|
308
|
-
["one", "two"].indent.should == [" one", " two"]
|
309
|
-
[["one", " two"], ["uno", "\t\tdos"]].indent.should == [[" one", " two"], [" uno", " \t\tdos"]]
|
310
|
-
|
284
|
+
describe "English Join function" do
|
285
|
+
it "should use the given conjunction to join the words with comma separation" do
|
286
|
+
[].english_join.should == ''
|
287
|
+
['one'].english_join.should == 'one'
|
288
|
+
['one', 'two'].english_join.should == 'one and two'
|
289
|
+
['one', 'two', 'three'].english_join.should == 'one, two, and three'
|
311
290
|
end
|
312
291
|
|
313
|
-
it "should
|
314
|
-
|
315
|
-
# 'resets' the indentation of a string by finding the least amount of indentation in the String/Array, and
|
316
|
-
# removing that amount from every line.
|
317
|
-
" def method_name\n # Do stuff\n end".reset_indentation.should == "def method_name\n # Do stuff\nend"
|
318
|
-
|
319
|
-
# Operates on arrays
|
320
|
-
[" def method_name", " # Do stuff", " end"].reset_indentation.should == ["def method_name", " # Do stuff", "end"]
|
321
|
-
|
322
|
-
# Useful for heredocs - must chomp to remove trailing newline
|
323
|
-
my_string = <<-EOS.chomp.reset_indentation
|
324
|
-
def method_name
|
325
|
-
# Do stuff
|
326
|
-
end
|
327
|
-
EOS
|
328
|
-
my_string.should == "def method_name\n # Do stuff\nend"
|
329
|
-
|
330
|
-
# Accepts an indentation modifier
|
331
|
-
" one\n two".reset_indentation(1).should == " one\n two"
|
332
|
-
" one\n two".reset_indentation(0).should == " one\ntwo" # Default behavior
|
333
|
-
" one\n two".reset_indentation(-1).should == "one\ntwo"
|
292
|
+
it "should override the default conjunction with the passed conjunction" do
|
293
|
+
['one', 'two', 'three'].english_join('or').should == 'one, two, or three'
|
334
294
|
end
|
335
295
|
|
336
|
-
it "should
|
337
|
-
|
338
|
-
# Given an Array of Arrays or an Array of Strings, appends a separator object to all but the last element in the Array.
|
339
|
-
# NOTE: For an Array of Strings the separator object must be a String, since it is appended to other Strings
|
340
|
-
["arg1", "arg2", "arg3"].append_separator("!").should == ["arg1!", "arg2!", "arg3"]
|
341
|
-
[["line1"], ["line2"], ["line3"]].append_separator("").should == [["line1", ""], ["line2", ""], ["line3"]]
|
342
|
-
[["line1", "line2"], ["line3", "line4"]].append_separator("").should == [["line1", "line2", ""], ["line3", "line4"]]
|
343
|
-
|
344
|
-
# Useful combined with indent and join
|
345
|
-
vars = ["var1", "var2", "var3", "var4"]
|
346
|
-
method_def = ["def add_up(#{vars.join(', ')})"]
|
347
|
-
method_body = vars.append_separator(" + ")
|
348
|
-
method_def += method_body.indent
|
349
|
-
method_def << "end"
|
350
|
-
method_def.join("\n").should == "def add_up(var1, var2, var3, var4)\n var1 + \n var2 + \n var3 + \n var4\nend"
|
351
|
-
|
352
|
-
# Handy for separating arrays of string arrays
|
353
|
-
test_array = [["this", "is", "a", "test"], ["quick", "brown", "fox"], ["lazy", "typist"]]
|
354
|
-
test_array.append_separator("").join("\n").should == "this\nis\na\ntest\n\nquick\nbrown\nfox\n\nlazy\ntypist"
|
296
|
+
it "should override the default separator with the passed separator" do
|
297
|
+
['one', 'two', 'three'].english_join('or', ' ').should == 'one two or three'
|
355
298
|
end
|
356
299
|
|
357
|
-
it "should
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
" test".find_least_indentation # => 2
|
362
|
-
" three\n two \n one".find_least_indentation # => 1
|
363
|
-
[" two", " three", " four"].find_least_indentation # => 2
|
364
|
-
[" two", " three", [" four", " five\n one"]].find_least_indentation # => 1
|
365
|
-
|
366
|
-
# Option to ignore blank (no characters whatsoever) lines.
|
367
|
-
# Note, disabling this option will automatically disable :ignore_empty_lines
|
368
|
-
# Default => true
|
369
|
-
" three\n".find_least_indentation # => 3
|
370
|
-
" three\n".find_least_indentation(:ignore_blank_lines => false) # => 0
|
371
|
-
|
372
|
-
# Option to not only ignore both blank lines (no characters whatsoever) and empty lines (whitespace-only)
|
373
|
-
# Default => true
|
374
|
-
" three\n ".find_least_indentation # => 3
|
375
|
-
" three\n ".find_least_indentation(:ignore_empty_lines => false) # => 1
|
376
|
-
" three\n".find_least_indentation(:ignore_empty_lines => false) # => 3
|
377
|
-
" three\n".find_least_indentation(:ignore_empty_lines => false, :ignore_blank_lines => false) # => 0
|
378
|
-
end
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
end
|
300
|
+
it "should allow turning off the oxford comma" do
|
301
|
+
['one', 'two', 'three'].english_join('and', ', ', false).should == 'one, two and three'
|
302
|
+
end
|
303
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,3 +8,50 @@ end
|
|
8
8
|
|
9
9
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
10
10
|
require 'indentation'
|
11
|
+
|
12
|
+
def rdoc_examples(examples_header = /^Examples:/i)
|
13
|
+
readme_rdoc = File.join(File.dirname(__FILE__), '../README.rdoc')
|
14
|
+
rdoc_content = File.read(readme_rdoc)
|
15
|
+
example_content = rdoc_content.split(/^== /).select{|p| p =~ examples_header}.first
|
16
|
+
raise "Couldn't find Examples header matching: #{examples_header}" unless example_content
|
17
|
+
examples_content = example_content.split('=== ')
|
18
|
+
examples = {}
|
19
|
+
# Skipping first 'example' since it is "Examples:\n"
|
20
|
+
examples_content[1..-1].each do |excon|
|
21
|
+
lines = excon.split("\n")
|
22
|
+
example_name = lines.shift.delete(':')
|
23
|
+
example_code = []
|
24
|
+
example_code << "$stdout = StringIO.new"
|
25
|
+
lines.each do |line|
|
26
|
+
l = line
|
27
|
+
if l =~ / # => /
|
28
|
+
code, check = line.split(/ # => /)
|
29
|
+
# Could make this more sophisticated, but just going with ==
|
30
|
+
l = code + ".should == " + check
|
31
|
+
elsif l =~ /(.*) ?# :([^ ]*) => (.*)$/
|
32
|
+
code = $~[1]
|
33
|
+
var = $~[2].to_sym
|
34
|
+
value = $~[3]
|
35
|
+
case var
|
36
|
+
when :stdout
|
37
|
+
l = "#{code}\n $stdout.string.chomp.split(%!\n!).last.should == %!#{value}!"
|
38
|
+
else
|
39
|
+
raise "Unkown variable type: #{var}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
example_code << l
|
43
|
+
end
|
44
|
+
# Debugging to show all of stdout during example
|
45
|
+
#example_code << "stdoutput = $stdout.string"
|
46
|
+
example_code << "$stdout = STDOUT"
|
47
|
+
#example_code << "puts stdoutput"
|
48
|
+
examples[example_name.to_sym] = example_code.join("\n")
|
49
|
+
end
|
50
|
+
examples
|
51
|
+
end
|
52
|
+
|
53
|
+
def time
|
54
|
+
t = Time.now
|
55
|
+
yield
|
56
|
+
Time.now - t
|
57
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: indentation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Sam Dana
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-10-09 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description: A library of extensions to Ruby's Array and String classes that allow
|
15
14
|
indentation manipulation of Strings and Arrays of Strings.
|
@@ -27,33 +26,34 @@ files:
|
|
27
26
|
- lib/indentation/string_mod.rb
|
28
27
|
- spec/.rspec
|
29
28
|
- spec/indentation_spec.rb
|
29
|
+
- spec/rdoc_examples_spec.rb
|
30
30
|
- spec/spec_helper.rb
|
31
31
|
homepage: http://samueldana.github.com/indentation/
|
32
32
|
licenses: []
|
33
|
+
metadata: {}
|
33
34
|
post_install_message:
|
34
35
|
rdoc_options: []
|
35
36
|
require_paths:
|
36
37
|
- lib
|
37
38
|
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
-
none: false
|
39
39
|
requirements:
|
40
40
|
- - ! '>='
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
version: '0'
|
43
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
-
none: false
|
45
44
|
requirements:
|
46
45
|
- - ! '>='
|
47
46
|
- !ruby/object:Gem::Version
|
48
47
|
version: '0'
|
49
48
|
requirements: []
|
50
49
|
rubyforge_project:
|
51
|
-
rubygems_version:
|
50
|
+
rubygems_version: 2.0.3
|
52
51
|
signing_key:
|
53
|
-
specification_version:
|
52
|
+
specification_version: 4
|
54
53
|
summary: A library of extensions to Ruby's Array and String classes that allow indentation
|
55
54
|
manipulation of Strings and Arrays of Strings.
|
56
55
|
test_files:
|
57
56
|
- spec/indentation_spec.rb
|
57
|
+
- spec/rdoc_examples_spec.rb
|
58
58
|
- spec/spec_helper.rb
|
59
59
|
has_rdoc:
|