Class: String

Inherits:
Object show all
Defined in:
lib/mini_sanity/string.rb,
lib/mini_sanity/util/string.rb

Instance Method Summary collapse

Instance Method Details

#assert_empty!(name = nil) ⇒ self

Checks that the String is empty, and returns the String unmodified. If the String fails this check, an exception is raised.

Examples:

"".assert_empty!     # == ""
"bad".assert_empty!  # raises exception

Parameters:

  • name (String, Symbol) (defaults to: nil)

    optional name to include in the error message

Returns:

  • (self)

Raises:



15
16
17
18
19
20
21
22
# File 'lib/mini_sanity/string.rb', line 15

def assert_empty!(name = nil)
  if !self.empty?
    raise MiniSanity::Error.new(name,
      "empty #{self.class}",
      self.inspect)
  end
  self
end

#assert_length!(length, name = nil) ⇒ self

Checks that the String matches a given length, and returns the String unmodified. If the String fails this check, an exception is raised.

Examples:

"password".assert_length!(8)           # == "password"
"long password".assert_length!(8..64)  # == "long password"
"pass".assert_length!(8..64)           # == raises exception

Parameters:

  • length (Integer, Range<Integer>)

    length to match

  • name (String, Symbol) (defaults to: nil)

    optional name to include in the error message

Returns:

  • (self)

Raises:



61
62
63
64
65
66
67
68
# File 'lib/mini_sanity/string.rb', line 61

def assert_length!(length, name = nil)
  if !(length === self.length)
    raise MiniSanity::Error.new(name,
      "#{self.class} having #{length} characters",
      self.inspect)
  end
  self
end

#assert_match!(regexp, name = nil) ⇒ self

Checks that the String matches a given regular expression, and returns the String unmodified. If the String fails this check, an exception is raised.

Examples:

"good result".assert_match!(/^good/)  # == "good result"
"bad result".assert_match!(/^good/)   # raises exception

Parameters:

  • regexp (Regexp)

    regular expression to check

  • name (String, Symbol) (defaults to: nil)

    optional name to include in the error message

Returns:

  • (self)

Raises:



110
111
112
113
114
115
116
117
# File 'lib/mini_sanity/string.rb', line 110

def assert_match!(regexp, name = nil)
  if regexp !~ self
    raise MiniSanity::Error.new(name,
      "#{self.class} matching #{regexp.inspect}",
      self.inspect)
  end
  self
end

#change(pattern, replacement = nil, &replacement_block) ⇒ String

Like String#sub, but raises an exception if no substitution was performed.

Examples:

"author: YOUR_NAME".change(/\bYOUR_NAME\b/, "Me")  # == "author: Me"
"author: TODO".change(/\bYOUR_NAME\b/, "Me")       # raises exception

Parameters:

  • pattern (Regexp, String)

    pattern to search for

  • replacement (String, Hash, &block) (defaults to: nil)

    substitution value (see String#sub documentation for full details)

Returns:

Raises:



73
74
75
# File 'lib/mini_sanity/util/string.rb', line 73

def change(pattern, replacement = nil, &replacement_block)
  self.dup.change!(pattern, replacement, &replacement_block)
end

#change!(pattern, replacement = nil, &replacement_block) ⇒ String

Like String#sub!, but raises an exception if no substitution was performed.

Examples:

"author: YOUR_NAME".change!(/\bYOUR_NAME\b/, "Me")  # == "author: Me"
"author: TODO".change!(/\bYOUR_NAME\b/, "Me")       # raises exception

Parameters:

  • pattern (Regexp, String)

    pattern to search for

  • replacement (String, Hash, &block) (defaults to: nil)

    substitution value (see String#sub! documentation for full details)

Returns:

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mini_sanity/util/string.rb', line 42

def change!(pattern, replacement = nil, &replacement_block)
  result = if replacement
    self.sub!(pattern, replacement)
  else
    self.sub!(pattern, &replacement_block)
  end

  if !result
    raise MiniSanity::Error.new(nil,
      "String matching #{pattern.inspect}",
      self.inspect)
  end

  result
end

#match!(pattern, pos = 0) ⇒ MatchData

Like String#match, but raises an exception if the match fails.

Examples:

"[email protected]".match!(/^([^@]+)@(.+)$/)  # === MatchData
"@user".match!(/^([^@]+)@(.+)$/)             # raises exception

Parameters:

  • pattern (Regexp)

    pattern to search for

  • pos (Integer) (defaults to: 0)

    position in the String to begin the search

Returns:

  • (MatchData)

Raises:



17
18
19
20
21
22
23
24
25
# File 'lib/mini_sanity/util/string.rb', line 17

def match!(pattern, pos = 0)
  result = self.match(pattern, pos)
  if result.nil?
    raise MiniSanity::Error.new(nil,
      "String matching #{pattern.inspect}#{" from position #{pos}" if pos != 0}",
      self.inspect)
  end
  result
end

#refute_empty!(name = nil) ⇒ self

Checks that the String is not empty, and returns the String unmodified. If the String fails this check, an exception is raised.

Examples:

"result".refute_empty!  # == "result"
"".refute_empty!        # raises exception

Parameters:

  • name (String, Symbol) (defaults to: nil)

    optional name to include in the error message

Returns:

  • (self)

Raises:



36
37
38
39
40
41
42
43
# File 'lib/mini_sanity/string.rb', line 36

def refute_empty!(name = nil)
  if self.empty?
    raise MiniSanity::Error.new(name,
      "non-empty #{self.class}",
      self.inspect)
  end
  self
end

#refute_length!(length, name = nil) ⇒ self

Checks that the String does not match a given length, and returns the String unmodified. If the String fails this check, an exception is raised.

Examples:

"password".refute_length!(0)           # == "password"
"long password".refute_length!(0...8)  # == "long password"
"pass".refute_length!(0...8)           # == raises exception

Parameters:

  • length (Integer, Range<Integer>)

    length to not match

  • name (String, Symbol) (defaults to: nil)

    optional name to include in the error message

Returns:

  • (self)

Raises:



86
87
88
89
90
91
92
93
# File 'lib/mini_sanity/string.rb', line 86

def refute_length!(length, name = nil)
  if length === self.length
    raise MiniSanity::Error.new(name,
      "#{self.class} not having #{length} characters",
      self.inspect)
  end
  self
end

#refute_match!(regexp, name = nil) ⇒ self

Checks that the String does not match a given regular expression, and returns the String unmodified. If the String fails this check, an exception is raised.

Examples:

"good result".refute_match!(/^bad/)  # == "good result"
"bad result".refute_match!(/^bad/)   # raises exception

Parameters:

  • regexp (Regexp)

    regular expression to check

  • name (String, Symbol) (defaults to: nil)

    optional name to include in the error message

Returns:

  • (self)

Raises:



134
135
136
137
138
139
140
141
# File 'lib/mini_sanity/string.rb', line 134

def refute_match!(regexp, name = nil)
  if regexp =~ self
    raise MiniSanity::Error.new(name,
      "#{self.class} not matching #{regexp.inspect}",
      self.inspect)
  end
  self
end