Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/pg-verify/core/extensions/string_extensions.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 45

def blank?
    self.empty?
end

#camel_caseObject



17
18
19
20
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 17

def camel_case
    return self if self !~ /_/ && self =~ /[A-Z]+.*/
    split('_').map{|e| e.capitalize}.join
end

#delete_prefix(prefix) ⇒ Object



77
78
79
80
81
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 77

def delete_prefix(prefix)
    self.respond_to?(:delete_prefix)
    return unless self.start_with?(prefix)
    return self[prefix.length, self.length - 1]
end

#directory?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 71

def directory?()
    File.directory?(self)
end

#display_lengthObject



26
27
28
29
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 26

def display_length()
    str = PgVerify::Colorizer.uncolorize(self)
    str.length() + ( str.count("\t") * 4 )
end

#file?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 67

def file?()
    File.file?(self)
end

#grep(str, e) ⇒ Object



22
23
24
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 22

def grep(str, e)
    self.split("\n").select { |l| l.include?(str) }
end

#indented(num: 1, str: " " * 4) ⇒ Object



35
36
37
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 35

def indented(num: 1, str: " " * 4)
    PgVerify::StringUtil.indented(self, num_indents: num, indent_string: str)
end

#integer?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 3

def integer?
    self.to_i.to_s == self
end

#labelize(bg: :darkgreen, fg: :white) ⇒ Object



63
64
65
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 63

def labelize(bg: :darkgreen, fg: :white)
    "".send(:"c_#{bg}") + " #{self} ".send(:"bg_#{bg}").send(:"c_#{fg}").c_bold + "".send(:"c_#{bg}")
end

#limit_lines(num_lines, separator: "...") ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 49

def limit_lines(num_lines, separator: "...")
    return "" if num_lines == 0
    split = self.split("\n")
    return self unless split.length > num_lines
    first_part = split[0, num_lines / 2]
    second_part = split[split.length - (num_lines - first_part.length), split.length]
    return first_part.join("\n") + "\n#{separator}\n" + second_part.join("\n")
end

#line_combine(other, separator: " ") ⇒ Object



31
32
33
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 31

def line_combine(other, separator: " ")
    PgVerify::StringUtil.line_combine(self, other, separator: separator)
end

#remove_before(substring) ⇒ Object



39
40
41
42
43
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 39

def remove_before(substring)
    split = self.split(substring)
    return "" if split.length == 1
    return split[1, split.length].join(substring)
end

#shorten(length) ⇒ Object



58
59
60
61
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 58

def shorten(length)
    return self if self.length <= length
    return self[0, [length - 3, 1].max] + "..."
end

#snake_caseObject



7
8
9
10
11
12
13
14
15
# File 'lib/pg-verify/core/extensions/string_extensions.rb', line 7

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