heredoc_unindent 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/History.rdoc CHANGED
@@ -1,3 +1,11 @@
1
+ === 1.0.5 / 2011-01-27
2
+
3
+ * 1 minor enhancement
4
+
5
+ * Refactoring and minor optimization
6
+ * Improved documentation with YARD tags
7
+ * New examples in README
8
+
1
9
  === 1.0.4 / 2011-01-27
2
10
 
3
11
  * 1 minor enhancement
data/README.rdoc CHANGED
@@ -17,22 +17,25 @@ will probably be an issue and hence this gem.
17
17
  == SYNOPSIS:
18
18
 
19
19
  if true
20
- s1 = <<-EOS
20
+ s = <<-EOS
21
21
  How sad it is
22
22
  to be unable
23
23
  to unindent heredocs
24
24
  EOS
25
- s2 = <<-EOS.unindent
25
+ t = <<-EOS.unindent
26
26
  How wonderful it is
27
27
  to be able
28
28
  to do it!
29
29
  EOS
30
30
  end
31
31
 
32
- s1[0, 13] #=> " How sad"
33
- s2[0, 13] #=> "How wonderful"
32
+ s[0..12] #=> " How sad"
33
+ t[0..12] #=> "How wonderful"
34
34
 
35
- " foo\n bar\n baz".unindent #=> "foo\n bar\nbaz"
35
+ s = " foo" #=> " foo"
36
+ s.unindent! #=> "foo"
37
+ s.unindent! #=> nil
38
+ s #=> "foo"
36
39
 
37
40
  == FEATURES:
38
41
 
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'hoe'
6
6
  Hoe.spec 'heredoc_unindent' do
7
7
  developer('Adriano Mitre', 'adriano.mitre@gmail.com')
8
8
 
9
- self.version = '1.0.4'
9
+ self.version = '1.0.5'
10
10
 
11
11
  self.readme_file = 'README.rdoc'
12
12
  self.history_file = 'History.rdoc'
@@ -1,37 +1,89 @@
1
1
  module CoreExt
2
2
  module String
3
- module HeredocUnindent
3
+
4
+ # This module, which is automatically included in ::String, define in-place
5
+ # and out-of-place unindentation methods.
4
6
  #
5
- # This module, which is included in String, define the heredoc unindentation method
6
- # +heredoc_unindent+ and alias it as +unindent+.
7
- ##
7
+ module HeredocUnindent
8
8
 
9
- # Removes common margin from indented strings such as the ones produced by
10
- # heredocs, i.e., strips out leading whitespace chars at the beggining of
11
- # each line (but only as much as the line with the smallest margin).
12
- #
13
- # Unless the optional argument +warn_first_dif_min+ is set to false or nil, a
14
- # warning is produced when the margin of the first line differs from the minimum.
15
- #
16
- def heredoc_unindent(warn_first_dif_min=true)
17
- min_margin = self.scan(/^\s*/).map(&:size).min
18
- if warn_first_dif_min
19
- first_margin = self[/^\s*/].size
20
- if first_margin != min_margin
21
- puts "warning: margin of the first line differs from minimum margin"
22
- end
9
+ private
10
+
11
+ # Actual implementation of the unindentation mechanism,
12
+ # both for in and out-of-place processing.
13
+ #
14
+ # @param [Boolean] warn_first_not_min
15
+ # @param [Boolean] in_place
16
+ # @return [String, nil]
17
+ #
18
+ def unindent_base(in_place, warn_first_not_min)
19
+ margins = self.scan(/^\s*/).map(&:size)
20
+ margins_min = margins.min
21
+ if margins.first != margins_min && warn_first_not_min
22
+ puts "warning: margin of the first line differs from minimum margin"
23
23
  end
24
- re = Regexp.new('^\s{0,' + min_margin.to_s + '}' ) # omitting the lower limit produces warnings and wrong behavior in ruby-1.8.7-p330 and ree-1.8.7-2010.02
25
- self.gsub(re, '')
24
+ return in_place ? nil : self unless margins_min != 0
25
+ re = Regexp.new('^\s{0,' + margins_min.to_s + '}' ) # omitting the lower limit produces warnings and wrong behavior in ruby-1.8.7-p330 and ree-1.8.7-2010.02
26
+ in_place ? gsub!(re, '') : gsub(re, '')
27
+ end
28
+
29
+ public
30
+
31
+ # Removes common margin from indented strings, such as the ones produced
32
+ # by indented heredocs. In other words, it strips out leading whitespace
33
+ # chars at the beggining of each line, but only as much as the line with
34
+ # the smallest margin.
35
+ #
36
+ # Unless the optional argument +warn_first_dif_min+ is set to +false+ (or
37
+ # +nil+), a warning is produced when the margin of the first line differs
38
+ # from the minimum.
39
+ #
40
+ # @example
41
+ #
42
+ # if true
43
+ # s = <<-EOS
44
+ # How sad it is to be unable to unindent heredocs
45
+ # EOS
46
+ # t = <<-EOS.unindent
47
+ # How wonderful it is to be able to do it!
48
+ # EOS
49
+ # end
50
+ #
51
+ # s[0..12] #=> " How sad"
52
+ # t[0..12] #=> "How wonderful"
53
+ #
54
+ # @param [Boolean] warn_first_not_min toggle warning if the margin of the first line differs from minimum margin
55
+ # @return [::String] unindented string
56
+ #
57
+ def heredoc_unindent(warn_first_not_min=true)
58
+ unindent_base(false, warn_first_not_min)
26
59
  end
27
60
  alias unindent heredoc_unindent
28
61
 
29
- # Performs HeredocUnindent#heredoc_unindent in place, returning self, or nil if no changes were made
62
+ # Same as #heredoc_unindent, but works in-place. Returns self, or nil if
63
+ # no changes were made
64
+ #
65
+ # @note Avoid attributing the return value of this method because it
66
+ # may be nil (see Example 2).
67
+ #
68
+ # @example 1 Recommended
69
+ #
70
+ # s = " foo" #=> " foo"
71
+ # s.unindent! #=> "foo"
72
+ # s.unindent! #=> nil
73
+ #
74
+ # @example 2 Disencouraged
75
+ #
76
+ # s = <<-EOS.unindent!
77
+ # the result would be as intended
78
+ # if this line weren't unindented
79
+ # EOS
80
+ # s #=> nil
81
+ #
82
+ # @param [Boolean] warn_first_dif_min toggle warning if the margin of the first line differs from minimum margin
83
+ # @return [::String, NilClass] unindented self, or nil if no changes were made
30
84
  #
31
- def heredoc_unindent!(warn_first_dif_min=true)
32
- orig = self.dup
33
- self.replace(self.heredoc_unindent(warn_first_dif_min))
34
- self != orig ? self : nil
85
+ def heredoc_unindent!(warn_first_not_min=true)
86
+ unindent_base(true, warn_first_not_min)
35
87
  end
36
88
  alias unindent! heredoc_unindent!
37
89
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 4
9
- version: 1.0.4
8
+ - 5
9
+ version: 1.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Adriano Mitre
metadata.gz.sig CHANGED
@@ -1,2 +1,2 @@
1
- LuOk�
2
- rs��z����VJlA���[�d�S����"��"���F�v���GX�p���d��<3b1��N�6}W��X\�9��sL���qc���� ˺2�[�H*:��)�Z�vӏ2�L�l�E⛑������8�����~H�f�-������W���x]II�&�l5��g���Q������|�*����+R�v\r�\+i�������V�n��(`IJ
1
+ JZi�����yZDĕaKA5�-oF �����?V�3hQ�Ź'GB[}����<�D�n��p�Q�Α�rI��,W���-��oW�NPv��b9�
2
+ Z��̡`� aE��A��=kƖ�!A7Y����/i��Q���