Class: String

Inherits:
Object show all
Defined in:
lib/sugar-high/file.rb,
lib/sugar-high/path.rb,
lib/sugar-high/blank.rb,
lib/sugar-high/regexp.rb,
lib/sugar-high/string.rb,
lib/sugar-high/arguments.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.letters(type = :lower) ⇒ Object



8
9
10
11
# File 'lib/sugar-high/string.rb', line 8

def self.letters type = :lower
  letters = ('a'..'z').to_a
  type == :upper ? letters.map!(&:upcase) : letters
end

.random_letters(count, type = :lower) ⇒ Object



4
5
6
# File 'lib/sugar-high/string.rb', line 4

def self.random_letters count, type = :lower
 letters(type).pick(count)
end

Instance Method Details

#alpha_numericObject

remove prefixed ‘-’ signs, then allow any letter, number, underscore ‘_’ or dash ‘-’



26
27
28
# File 'lib/sugar-high/string.rb', line 26

def alpha_numeric
  self.gsub(/^\-+/, '').gsub(/[^0-9a-zA-Z_\-]+/i, '')
end

#argsObject



33
34
35
# File 'lib/sugar-high/arguments.rb', line 33

def args
  (self =~ /\w+\s+\w+/) ? self.split : self
end

#as_filenameObject



40
41
42
# File 'lib/sugar-high/file.rb', line 40

def as_filename
  self.underscore
end

#blank?Boolean

Returns:



29
30
31
# File 'lib/sugar-high/blank.rb', line 29

def blank?
  self !~ /\S/
end

#concats(*args) ⇒ Object



18
19
20
21
22
23
# File 'lib/sugar-high/string.rb', line 18

def concats *args
  args.inject(self) do |res, arg|
    res << arg.to_s
    res
  end
end

#dirObject



53
54
55
56
# File 'lib/sugar-high/file.rb', line 53

def dir
  return ::Dir.new(self) if ::File.directory?(self)
  raise "No file found at #{self}"
end

#fileObject



48
49
50
51
# File 'lib/sugar-high/file.rb', line 48

def file
  return ::File.new(self) if ::File.file?(self)
  raise "No file found at #{self}"
end

#insert_before_last(str, marker = 'end') ⇒ Object



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
# File 'lib/sugar-high/string.rb', line 30

def insert_before_last str, marker = 'end'
  res = []
  found = false
  marker = case marker
  when Symbol, String
    marker.to_s
  when Hash
    marker[:marker].to_s
  else
    raise ArgumentException, "last argument is the marker and must be a String, Symbol or even Hash with a :marker option pointing to the marker (String or Symbol)"
  end

  marker = Regexp.escape(marker.to_s.reverse)
  nl = Regexp.escape("\n")
  self.reverse.each_line do |x|
    x.gsub! /#{nl}/, ''
    if !found && x =~ /#{marker}/
      replace = "#{str}\n" << x.reverse
      res << replace
      found = true
    else
      res << x.reverse
    end
  end
  res = res.reverse.join("\n")
end

#new_fileObject



58
59
60
61
62
63
64
# File 'lib/sugar-high/file.rb', line 58

def new_file
  begin
    file
  rescue
    File.open(self, 'w')
  end
end

#pathObject



2
3
4
# File 'lib/sugar-high/path.rb', line 2

def path
  self.extend PathString
end

#to_regexpObject



2
3
4
# File 'lib/sugar-high/regexp.rb', line 2

def to_regexp
  /#{Regexp.escape(self)}/
end

#trimObject



13
14
15
# File 'lib/sugar-high/string.rb', line 13

def trim
  self.strip
end

#valid_file_command?Boolean

Returns:



44
45
46
# File 'lib/sugar-high/file.rb', line 44

def valid_file_command?
  self.to_sym.valid_file_command?
end