Class: String

Inherits:
Object show all
Includes:
Fancypath::Helpers
Defined in:
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

Instance Method Summary collapse

Methods included from Fancypath::Helpers

#to_expanded_fancypath, #to_fancypath, #to_tilde_expanded_path

Instance Method Details

#/(other) ⇒ Object


44
45
46
# File 'lib/babushka/core_patches/string.rb', line 44

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

#colourise(description) ⇒ Object Also known as: colorize

Wrap this string in ANSI color codes according to description. See the Babushka::ANSI docs for details.


66
67
68
# File 'lib/babushka/core_patches/string.rb', line 66

def colourise description
  Babushka::ANSI.wrap(self, description)
end

#end_with(other) ⇒ Object

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


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

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)

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

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.


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

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)

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

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

#to_utf8Object


53
54
55
56
57
58
59
60
61
62
# File 'lib/babushka/core_patches/string.rb', line 53

def to_utf8
  if !respond_to?(:encoding) # Skip on ruby-1.8.
    self
  elsif valid_encoding?
    encode("utf-8")
  else
    # Round-trip to force a conversion, stripping invalid chars.
    encode("utf-16be", :invalid => :replace, :replace => "?").encode("utf-8")
  end
end

#to_versionObject

Create a VersionStr from this string.


49
50
51
# File 'lib/babushka/core_patches/string.rb', line 49

def to_version
  Babushka::VersionStr.new self
end

#val_for(key) ⇒ Object

Extracts the value corresponding to the supplied key in an arbitrary, multiline string. Most common config formats are handled. When there are multiple matches, the first is returned. If there is no match, nil is returned.

See Array#val_for, and the cases in core_patches_spec.rb, for examples.


40
41
42
# File 'lib/babushka/core_patches/string.rb', line 40

def val_for key
  split("\n").val_for(key)
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#run_requirements).

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

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

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