Class: String

Inherits:
Object show all
Defined in:
lib/libis/tools/extend/string.rb,
lib/libis/tools/extend/empty.rb

Overview

Extension class

Constant Summary collapse

BLANK_REGEX =
/\A[[:space:]]^\z/.freeze

Instance Method Summary collapse

Instance Method Details

#align_leftObject

Align a multi-line string to the left by removing as much spaces from the left as possible.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/libis/tools/extend/string.rb', line 103

def align_left
  string = dup
  relevant_lines = string.split(/\r\n|\r|\n/).select { |line| line.size > 0 }
  indentation_levels = relevant_lines.map do |line|
    match = line.match(/^( +)[^ ]+/)
    match ? match[1].size : 0
  end
  indentation_level = indentation_levels.min
  string.gsub! /^#{' ' * indentation_level}/, '' if indentation_level > 0
  string
end

#blank?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/libis/tools/extend/empty.rb', line 23

def blank?
  empty? || BLANK_REGEX.match?(self)
end

#camelize(first_letter = :upper) ⇒ Object

from activesupport



5
6
7
8
9
10
11
# File 'lib/libis/tools/extend/string.rb', line 5

def camelize(first_letter = :upper)
  if first_letter == :upper
    gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    self[0..0].downcase + camelize[1..-1]
  end
end

#constantizeObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/libis/tools/extend/string.rb', line 13

def constantize
  names = split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#dasherizeObject



24
25
26
# File 'lib/libis/tools/extend/string.rb', line 24

def dasherize
  gsub(/_/, '-')
end

#decode_visualObject

Convert all not-printable characters encoded in hex format back to original



98
99
100
# File 'lib/libis/tools/extend/string.rb', line 98

def decode_visual
  self.gsub(/_x([0-9a-f]{4})_/i) { [$1.to_i(16)].pack('U') }
end

#demodulizeObject



28
29
30
# File 'lib/libis/tools/extend/string.rb', line 28

def demodulize
  gsub(/^.*::/, '')
end

#dot_net_cleanObject



82
83
84
# File 'lib/libis/tools/extend/string.rb', line 82

def dot_net_clean
  self.gsub /^(\d+|error|float|string);\\?#/, ''
end

#encode_visual(regex = nil) ⇒ Object

Escape all not-printabe characters in hex format



92
93
94
95
# File 'lib/libis/tools/extend/string.rb', line 92

def encode_visual(regex = nil)
  regex ||= /\W/
  self.gsub(regex) { |c| '_x' + '%04x' % c.unpack('U')[0] + '_'}
end

#escape_for_cmdObject

Escape double quotes for usage in passing through scripts



73
74
75
# File 'lib/libis/tools/extend/string.rb', line 73

def escape_for_cmd
  self.gsub(/"/) { |s| '\\\\\\' + s[0].to_s }
end

#escape_for_regexpObject

Escape string for use in Regular Expressions



63
64
65
# File 'lib/libis/tools/extend/string.rb', line 63

def escape_for_regexp
  self.gsub(/[\.\+\*\(\)\{\}\|\/\\\^\$"']/) { |s| '\\' + s[0].to_s }
end

#escape_for_sqlObject

Escape single quotes for usage in SQL statements



78
79
80
# File 'lib/libis/tools/extend/string.rb', line 78

def escape_for_sql
  self.gsub(/'/) { |s| ($` == '' || $' == '' ? '' : '\'') + s[0].to_s }
end

#escape_for_stringObject

Escape double quotes for usage in code strings.



68
69
70
# File 'lib/libis/tools/extend/string.rb', line 68

def escape_for_string
  self.gsub(/"/) { |s| '\\' + s[0].to_s }
end

#quoteObject

Quote string for command-line use.



58
59
60
# File 'lib/libis/tools/extend/string.rb', line 58

def quote
  '\"' + self.gsub(/"/) { |s| '\\' + s[0] } + '\"'
end

#remove_whitespaceObject

Convert whitespace into underscores



87
88
89
# File 'lib/libis/tools/extend/string.rb', line 87

def remove_whitespace
  self.gsub(/\s/, '_')
end

#sort_formObject

Create sortable object from string. Supports better natural sorting.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/libis/tools/extend/string.rb', line 42

def sort_form
  result = []
  matcher = /^(\D*)(\d*)(.*)$/
  self.split('.').each { |s|
    while !s.empty? and (x = matcher.match s)
      a = x[1].to_s.strip
      b = a.gsub(/[ _]/, '')
      result << [b.downcase, b, a]
      result << x[2].to_i
      s = x[3]
    end
  }
  result
end

#underscoreObject



32
33
34
35
36
37
38
# File 'lib/libis/tools/extend/string.rb', line 32

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