Class: String

Inherits:
Object show all
Defined in:
lib/git_topic/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/git_topic/core_ext.rb', line 22

def blank?
  nil? || empty?
end

#onelineObject



18
19
20
# File 'lib/git_topic/core_ext.rb', line 18

def oneline
    strip.gsub( /\n\s+/, ' ' )
end

#pluralize_with_count(count) ⇒ Object

Annoyingly, the useful version of pluralize in texthelpers isn’t in the string core extensions.



28
29
30
# File 'lib/git_topic/core_ext.rb', line 28

def pluralize_with_count( count )
  count > 1 ? pluralize_without_count : singularize
end

#unindentObject



12
13
14
15
16
# File 'lib/git_topic/core_ext.rb', line 12

def unindent
    indent = (index /^([ \t]+)/; $1) || ''
    regex = /^#{Regexp::escape( indent )}/
    strip.gsub regex, ''
end

#wrap(width, heading_indentation = nil, heading = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/git_topic/core_ext.rb', line 33

def wrap( width, heading_indentation=nil, heading=nil )
  # TODO 2: if indentation is nil, compute indentation
  heading_indentation   ||= 0
  heading               ||= ""
  
  indentation = heading.size + heading_indentation
  indent      = ' ' * indentation
  sbuf        = "#{indent}"
  line_w      = indentation

  original_indentation = nil

  new_line = lambda do
    sbuf << "\n#{indent}"
    line_w  = indentation
  end

  start_of_line = lambda { line_w == indentation }

  split( "\n" ).each do |line|
    if original_indentation.nil?
      line =~ /^(\s*)/
      original_indentation = $1.size
    end

    # If the line has more than the original indentation, it is ‘indented’
    # and to be left unformatted
    if line =~ /^\s{#{original_indentation}}\s/
      sbuf << "\n#{indent}" unless start_of_line.call
      sbuf << line.slice( original_indentation, line.size )
      new_line.call
      next
    # Leave line breaks intact to allow paragraph separataion.
    elsif line.strip.empty?
      sbuf << "\n"
      new_line.call
      next
    end

    leading_space = 
      start_of_line.call  ? '' : ' '
    line.gsub!( %r/^\s{,#{original_indentation}}/, leading_space )
    line.split( /(\s+)/ ).each do |word|
      next if start_of_line.call && word.strip.empty?

      if line_w + word.size <= width
        sbuf << word
        line_w += word.size
      elsif start_of_line.call
        sbuf << word[0...(size - indentation)]
        new_line.call
        word.slice! (size - indentation), word.size
      else
        new_line.call
        redo
      end
    end
  end
 
  sbuf[ 0...heading.size ] = heading

  sbuf.rstrip
end

#wrap!(width, heading_indentation = nil, heading = nil) ⇒ Object



97
98
99
# File 'lib/git_topic/core_ext.rb', line 97

def wrap!( width, heading_indentation=nil, heading=nil )
  self.replace wrap( width, heading_indentation, heading )
end