Class: Object

Inherits:
BasicObject
Defined in:
lib/mini_sanity/object.rb

Instance Method Summary collapse

Instance Method Details

#assert_equal!(expect, name = nil) ⇒ self

Checks that the Object equals a given expected value, and returns the Object unmodified. If the Object fails this check, an exception is raised.

Examples:

"good".assert_equal!("good")  # == "good"
"bad".assert_equal!("good")   # raises exception

Parameters:

  • expect (Object)

    value to expect

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



63
64
65
66
67
68
69
70
# File 'lib/mini_sanity/object.rb', line 63

def assert_equal!(expect, name = nil)
  if self != expect
    raise MiniSanity::Error.new(name,
      expect.inspect,
      self.inspect)
  end
  self
end

#assert_in!(permitted, name = nil) ⇒ self

Checks that the Object is included in a given collection, and returns the Object unmodified. If the Object fails this check, an exception is raised.

Examples:

"good".assert_in!(["ok", "good", "great"])  # == "good"
"bad".assert_in!(["ok", "good", "great"])   # raises exception

Parameters:

  • permitted (Enumerable, #include?)

    collection of permitted values

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



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

def assert_in!(permitted, name = nil)
  if !permitted.include?(self)
    raise MiniSanity::Error.new(name,
      "value included in #{permitted.inspect}",
      self.inspect)
  end
  self
end

#assert_instance_of!(klass, name = nil) ⇒ self

Checks that the Object is an instance of a given class, and returns the Object unmodified. If the Object fails this check, an exception is raised.

Examples:

123.assert_instance_of!(Numeric)  # == 123
123.assert_instance_of!(String)   # raises exception

Parameters:

  • klass (Class)

    class to check

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



159
160
161
162
163
164
165
166
# File 'lib/mini_sanity/object.rb', line 159

def assert_instance_of!(klass, name = nil)
  if !self.instance_of?(klass)
    raise MiniSanity::Error.new(name,
      "instance of #{klass}",
      "instance of #{self.class}: #{self.inspect}")
  end
  self
end

#assert_kind_of!(klass, name = nil) ⇒ self

Checks that the Object is an instance of a given class or one of its subclasses, and returns the Object unmodified. If the Object fails this check, an exception is raised.

Examples:

123.assert_kind_of!(Numeric)  # == 123
123.assert_kind_of!(Float)    # raises exception

Parameters:

  • klass (Class)

    class to check

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

    optional name to include in the error message

Returns:

  • (self)

Raises:

  • (MiniSanity::Error)

    if the Object is not an instance of klass or its subclasses



183
184
185
186
187
188
189
190
# File 'lib/mini_sanity/object.rb', line 183

def assert_kind_of!(klass, name = nil)
  if !self.kind_of?(klass)
    raise MiniSanity::Error.new(name,
      "instance of #{klass} or one of its subclasses",
      "instance of #{self.class}: #{self.inspect}")
  end
  self
end

#assert_nil!(name = nil) ⇒ self

Checks that the Object is nil, and returns nil. If the Object fails this check, an exception is raised.

Examples:

result = {}
result[:error].assert_nil!  # == nil

result = { error: "something went wrong" }
result[:error].assert_nil!  # == raises exception

Parameters:

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



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

def assert_nil!(name = nil)
  if !self.nil?
    raise MiniSanity::Error.new(name,
      "nil",
      self.inspect)
  end
  self
end

#assert_respond_to!(method_name, name = nil) ⇒ self

Checks that the Object responds to a given method, and returns the Object unmodified. If the Object fails this check, an exception is raised.

Examples:

"abc".assert_respond_to!(:empty?)  # == "abc"
"abc".assert_respond_to!(:pop)     # raises exception

Parameters:

  • method_name (String, Symbol)

    name of method to check

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



207
208
209
210
211
212
213
214
# File 'lib/mini_sanity/object.rb', line 207

def assert_respond_to!(method_name, name = nil)
  if !self.respond_to?(method_name)
    raise MiniSanity::Error.new(name,
      "object responding to method #{method_name}",
      "instance of #{self.class}: #{self.inspect}")
  end
  self
end

#refute_equal!(reject, name = nil) ⇒ self

Checks that the Object does not equal a given reject value, and returns the Object unmodified. If the Object fails this check, an exception is raised.

Examples:

"good".refute_equal!("bad")  # == "good"
"bad".refute_equal!("bad")   # raises exception

Parameters:

  • reject (Object)

    value to reject

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



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

def refute_equal!(reject, name = nil)
  if self == reject
    raise MiniSanity::Error.new(name,
      "not #{reject.inspect}",
      self.inspect)
  end
  self
end

#refute_in!(prohibited, name = nil) ⇒ self

Checks that the Object is not included in a given collection, and returns the Object unmodified. If the Object fails this check, an exception is raised.

Examples:

"good".refute_in!(["bad", "poor", "fail"])  # == "good"
"bad".refute_in!(["bad", "poor", "fail"])   # raises exception

Parameters:

  • prohibited (Enumerable, #include?)

    collection of prohibited values

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



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

def refute_in!(prohibited, name = nil)
  if prohibited.include?(self)
    raise MiniSanity::Error.new(name,
      "value not included in #{prohibited.inspect}",
      self.inspect)
  end
  self
end

#refute_nil!(name = nil) ⇒ self

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

Examples:

["result 1"].first.refute_nil!  # == "result 1"
[].first.refute_nil!            # raises exception

Parameters:

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

    optional name to include in the error message

Returns:

  • (self)

Raises:



39
40
41
42
43
44
45
46
# File 'lib/mini_sanity/object.rb', line 39

def refute_nil!(name = nil)
  if self.nil?
    raise MiniSanity::Error.new(name,
      "non-nil value",
      "nil")
  end
  self
end