Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/utils.rb

Direct Known Subclasses

HammerCLI::CompleterWord

Instance Method Summary collapse

Instance Method Details

#camelizeObject



19
20
21
22
# File 'lib/hammer_cli/utils.rb', line 19

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

#constantizeObject

Raises:

  • (NameError)


38
39
40
41
# File 'lib/hammer_cli/utils.rb', line 38

def constantize
  raise NameError, "Can't constantize empty string" if self.empty?
  HammerCLI.constant_path(self)[-1]
end

#format(params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hammer_cli/utils.rb', line 5

def format(params)
  if params.is_a? Hash
    array_params = self.scan(/%[<{]([^>}]*)[>}]/).collect do |name|
      name = name[0]
      !params[name.to_s].nil? ? params[name.to_s].to_s : params[name.to_sym].to_s
    end
    self.gsub(/%[<]([^>]*)[>]/, '%')
        .gsub(/%[{]([^}]*)[}]/, '%s')
        .gsub(/\%(\W?[^bBdiouxXeEfgGaAcps])/, '%%\1') % array_params
  else
    self.gsub(/\%(\W?[^bBdiouxXeEfgGaAcps])/, '%%\1') % params
  end
end

#indent_with(indent_str) ⇒ Object



24
25
26
# File 'lib/hammer_cli/utils.rb', line 24

def indent_with(indent_str)
  gsub(/^/, indent_str)
end

#underscoreObject



28
29
30
31
32
33
34
35
36
# File 'lib/hammer_cli/utils.rb', line 28

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

#wrap(line_width: 80, break_sequence: "\n") ⇒ Object



44
45
46
47
48
# File 'lib/hammer_cli/utils.rb', line 44

def wrap(line_width: 80, break_sequence: "\n")
  split("\n").collect! do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1#{break_sequence}").strip : line
  end * break_sequence
end