Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/mini_sanity/object.rb
Instance Method Summary collapse
-
#assert_equal!(expect, name = nil) ⇒ self
Checks that the Object equals a given expected value, and returns the Object unmodified.
-
#assert_in!(permitted, name = nil) ⇒ self
Checks that the Object is included in a given collection, and returns the Object unmodified.
-
#assert_instance_of!(klass, name = nil) ⇒ self
Checks that the Object is an instance of a given class, and returns the Object unmodified.
-
#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.
-
#assert_nil!(name = nil) ⇒ self
Checks that the Object is nil, and returns nil.
-
#assert_respond_to!(method_name, name = nil) ⇒ self
Checks that the Object responds to a given method, and returns the Object unmodified.
-
#refute_equal!(reject, name = nil) ⇒ self
Checks that the Object does not equal a given reject value, and returns the Object unmodified.
-
#refute_in!(prohibited, name = nil) ⇒ self
Checks that the Object is not included in a given collection, and returns the Object unmodified.
-
#refute_nil!(name = nil) ⇒ self
Checks that the Object is not nil, and returns the Object unmodified.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |