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



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

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

#constantizeObject

Raises:

  • (NameError)


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

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

#format(params) ⇒ Object

string formatting for ruby 1.8



5
6
7
8
9
10
11
12
13
14
15
16
# 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] || params[name.to_sym]
    end

    self.gsub(/%[<]([^>]*)[>]/, '%').gsub(/%[{]([^}]*)[}]/, '%s') % array_params
  else
    self % params
  end
end

#indent_with(indent_str) ⇒ Object



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

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

#underscoreObject



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

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