Class: String

Inherits:
Object show all
Defined in:
lib/String/blankQ.rb,
lib/String/split_csv.rb,
lib/String/underscore.rb

Overview

Changes since 0.0

  1. Updated with changes from active_support 3.0.0.

  2. Will now successfully handle strings with spaces.

  3. Removed the dup’ed word since once the transformational methods were chained this became unnecessary.

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/String/blankQ.rb', line 14

def blank?
  self !~ /\S/
end

#split_csv(quote = nil, column_separator = ',', row_separator = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/String/split_csv.rb', line 20

def split_csv(quote = nil, column_separator = ',', row_separator = nil)
  case quote
  when :none, :unquoted
    if row_separator
      self.chomp(row_separator).split(column_separator)
    else
      self.chomp.split(column_separator)
    end
  when :double, :double_quoted, :double_quotes
    if row_separator
      self.chomp(row_separator).split(column_separator).collect{|e| e.sub(/^"/, '').sub(/"$/, '')}
    else
      self.chomp.split(column_separator).collect{|e| e.sub(/^"/, '').sub(/"$/, '')}
    end
  else
    split_row = []
    assembling_column = false
    buffer = ''
    if row_separator
      self.chomp!(row_separator)
    else
      self.chomp!
    end
    self.split(column_separator).each do |e|
      if assembling_column && !(e =~ /"$/) # e.not_closing_quotes?
        buffer << e
      elsif assembling_column && e =~ /"$/ # e.closing_quotes?
        buffer << e.sub(/"$/, '') # remove the trailing quote
        split_row << buffer
        assembling_column = false
      elsif (e =~ /^"/) && !(e =~ /"$/) # e.opening_quotes_but_not_closing_quotes?
        buffer = ''
        buffer << e.sub(/^"/, '') + column_separator # remove leading quote and replace the column_separator
        assembling_column = true
      else
        if (e =~ /^"/) && (e =~ /"$/) # e.both_opening_and_closing_quotes?
          split_row << e.sub(/^"/, '').sub(/"$/, '')
        else
          split_row << e
        end
      end
    end
    split_row
  end
end

#underscoreObject



23
24
25
26
27
28
29
30
# File 'lib/String/underscore.rb', line 23

def underscore
  gsub(' ', '_').
  gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end