Class: String

Inherits:
Object show all
Includes:
Fancypath::Helpers
Defined in:
lib/babushka/colorizer.rb,
lib/babushka/xml_string.rb,
lib/fancypath/fancypath.rb,
lib/babushka/core_patches/blank.rb,
lib/babushka/core_patches/string.rb

Direct Known Subclasses

Babushka::XMLString

Defined Under Namespace

Classes: Colorizer

Instance Method Summary collapse

Methods included from Fancypath::Helpers

#to_expanded_fancypath, #to_fancypath, #to_tilde_expanded_path

Instance Method Details

#/(other) ⇒ Object



79
80
81
# File 'lib/babushka/core_patches/string.rb', line 79

def / other
  (empty? ? other.p : (p / other))
end

#blank?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/babushka/core_patches/blank.rb', line 13

def blank?
  empty?
end

#colorize(description = '', start_at = nil) ⇒ Object

Return a new string with the contents of this string surrounded in escape sequences such that it will render as described in description. Some examples:

'Hello world!'.colorize('green')   #=> "\e[0;32;29mHello world!\e[0m"
'Hello world!'.colorize('on red')  #=> "\e[0;29;41mHello world!\e[0m"
'Hello world!'.colorize('reverse') #=> "\e[0;29;7mHello world!\e[0m"


98
99
100
101
102
103
104
# File 'lib/babushka/core_patches/string.rb', line 98

def colorize description = '', start_at = nil
  if start_at.nil? || (cut_point = index(start_at)).nil?
    Colorizer.instance.colorize self, description
  else
    self[0...cut_point] + Colorizer.instance.colorize(self[cut_point..-1], description)
  end
end

#colorize!(description = nil, start_at = nil) ⇒ Object

As colorize, but modify this string in-place instead of returning a new one.



107
108
109
# File 'lib/babushka/core_patches/string.rb', line 107

def colorize! description = nil, start_at = nil
  replace colorize(description, start_at) unless description.nil?
end

#colorized?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/babushka/core_patches/string.rb', line 88

def colorized?
  self[/\e\[\d/]
end

#decolorizeObject

Return a new string with all color-related escape sequences removed.



112
113
114
# File 'lib/babushka/core_patches/string.rb', line 112

def decolorize
  dup.decolorize!
end

#decolorize!Object

Remove all color-related escape sequences from this string in-place.



117
118
119
120
# File 'lib/babushka/core_patches/string.rb', line 117

def decolorize!
  gsub!(/\e\[\d+[;\d]*m/, '')
  self
end

#end_with(other) ⇒ Object

Return a duplicate of self, with other appended to it if it doesn’t already end with other.



31
32
33
# File 'lib/babushka/core_patches/string.rb', line 31

def end_with other
  ends_with?(other) ? self : self + other
end

#ends_with?(other) ⇒ Boolean

Returns true iff other appears exactly at the end of self.

Returns:

  • (Boolean)


21
22
23
# File 'lib/babushka/core_patches/string.rb', line 21

def ends_with? other
  self[-other.length, other.length] == other
end

#start_with(other) ⇒ Object

Return a duplicate of self, with other prepended to it if it doesn’t already start with other.



26
27
28
# File 'lib/babushka/core_patches/string.rb', line 26

def start_with other
  starts_with?(other) ? self : other + self
end

#starts_with?(other) ⇒ Boolean

Returns true iff other appears exactly at the start of self.

Returns:

  • (Boolean)


16
17
18
# File 'lib/babushka/core_patches/string.rb', line 16

def starts_with? other
  self[0, other.length] == other
end

#to_versionObject

Create a VersionStr from this string.



84
85
86
# File 'lib/babushka/core_patches/string.rb', line 84

def to_version
  Babushka::VersionStr.new self
end

#val_for(key) ⇒ Object

Extracts specified values from arbitrary, multiline strings. Most common formats are handled. When there are multiple matches across a multi-line string, the first is returned. If there is no match, the empty string is returned.

With a simple key/value format:

'key: value'.val_for('key')  #=> 'value'
'key = value'.val_for('key') #=> 'value'
'key value'.val_for('key')   #=> 'value'

Whitespace is handled correctly:

'  key: value '.val_for('key') #=> 'value'
'  key value '.val_for('key')  #=> 'value'

Leading non-word characters form part of the key:

'*key: value'.val_for('*key') #=> 'value'
'-key: value'.val_for('-key') #=> 'value'
'-key: value'.val_for('key')  #=> nil

But not if they’re separated from the key:

'* key: value'.val_for('key') #=> 'value'

Spaces within the key are handled properly:

'key with spaces: value'.val_for('key with spaces')         #=> 'value'
'- key with spaces: value'.val_for('key with spaces')       #=> 'value'
' --  key with spaces: value'.val_for('key with spaces')    #=> 'value'
'space-separated key: value'.val_for('space-separated key') #=> 'value'

As are values containing spaces:

'key: space-separated value'.val_for('key')                         #=> 'space-separated value'
'key with spaces: space-separated value'.val_for('key with spaces') #=> 'space-separated value'


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/babushka/core_patches/string.rb', line 66

def val_for key
  split("\n").grep(
    # The key we're after, maybe preceded by non-word chars and spaces, and
    # followed either by a word/non-word boundary or whitespace.
    key.is_a?(Regexp) ? key : /(^|^[^\w]*\s+)#{Regexp.escape(key)}(\b|(?=\s))/
  ).map {|l|
    l.sub(/^[^\w]*\s+/, '').
      sub(key.is_a?(Regexp) ? key : /^#{Regexp.escape(key)}(\b|(?=\s))\s*[:=]?/, '').
      sub(/[;,]\s*$/, '').
      strip
  }.first
end

#with(*args) ⇒ Object

Return a DepRequirement that specifies the dep that should later be called, and the arguments that should be passed. This allows requiring deps with a less noisy syntax, and the lookup is lazy (it happens at the point the dep is invoked, from its parent dep in Dep#process_requirements).

dep 'user has a password', :username do
  requires 'user exists'.with(username)
end


11
12
13
# File 'lib/babushka/core_patches/string.rb', line 11

def with *args
  Babushka::DepRequirement.new(self, args)
end

#xml_val_for(key) ⇒ Object



25
26
27
# File 'lib/babushka/xml_string.rb', line 25

def xml_val_for key
  Babushka::XMLString.new(self).val_for(key)
end