1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
# frozen_string_literal: true
require "contracts/formatters"
require "set"
# rdoc
# This module contains all the builtin contracts.
# If you want to use them, first:
#
# import Contracts
#
# And then use these or write your own!
#
# A simple example:
#
# Contract Num, Num => Num
# def add(a, b)
# a + b
# end
#
# The contract is <tt>Contract Num, Num, Num</tt>.
# That says that the +add+ function takes two numbers and returns a number.
module Contracts
module Builtin
# Check that an argument is +Numeric+.
class Num
def self.valid? val
val.is_a? Numeric
end
end
# Check that an argument is a positive number.
class Pos
def self.valid? val
val.is_a?(Numeric) && val.positive?
end
end
# Check that an argument is a negative number.
class Neg
def self.valid? val
val.is_a?(Numeric) && val.negative?
end
end
# Check that an argument is an +Integer+.
class Int
def self.valid? val
val.is_a?(Integer)
end
end
# Check that an argument is a natural number (includes zero).
class Nat
def self.valid? val
val.is_a?(Integer) && val >= 0
end
end
# Check that an argument is a positive natural number (excludes zero).
class NatPos
def self.valid? val
val.is_a?(Integer) && val.positive?
end
end
# Passes for any argument.
class Any
def self.valid? val
true
end
end
# Fails for any argument.
class None
def self.valid? val
false
end
end
# Use this when you are writing your own contract classes.
# Allows your contract to be called with <tt>[]</tt> instead of <tt>.new</tt>:
#
# Old: <tt>Or.new(param1, param2)</tt>
#
# New: <tt>Or[param1, param2]</tt>
#
# Of course, <tt>.new</tt> still works.
class CallableClass
include ::Contracts::Formatters
def self.[](*vals)
new(*vals)
end
end
# Takes a variable number of contracts.
# The contract passes if any of the contracts pass.
# Example: <tt>Or[Integer, Float]</tt>
class Or < CallableClass
def initialize(*vals)
super()
@vals = vals
end
def valid?(val)
@vals.any? do |contract|
res, _ = Contract.valid?(val, contract)
res
end
end
def to_s
# rubocop:disable Style/StringConcatenation
@vals[0, @vals.size-1].map do |x|
InspectWrapper.create(x)
end.join(", ") + " or " + InspectWrapper.create(@vals[-1]).to_s
# rubocop:enable Style/StringConcatenation
end
end
# Takes a variable number of contracts.
# The contract passes if exactly one of those contracts pass.
# Example: <tt>Xor[Integer, Float]</tt>
class Xor < CallableClass
def initialize(*vals)
super()
@vals = vals
end
def valid?(val)
results = @vals.map do |contract|
res, _ = Contract.valid?(val, contract)
res
end
results.count(true) == 1
end
def to_s
# rubocop:disable Style/StringConcatenation
@vals[0, @vals.size-1].map do |x|
InspectWrapper.create(x)
end.join(", ") + " xor " + InspectWrapper.create(@vals[-1]).to_s
# rubocop:enable Style/StringConcatenation
end
end
# Takes a variable number of contracts.
# The contract passes if all contracts pass.
# Example: <tt>And[Integer, Float]</tt>
class And < CallableClass
def initialize(*vals)
super()
@vals = vals
end
def valid?(val)
@vals.all? do |contract|
res, _ = Contract.valid?(val, contract)
res
end
end
def to_s
# rubocop:disable Style/StringConcatenation
@vals[0, @vals.size-1].map do |x|
InspectWrapper.create(x)
end.join(", ") + " and " + InspectWrapper.create(@vals[-1]).to_s
# rubocop:enable Style/StringConcatenation
end
end
# Takes a variable number of method names as symbols.
# The contract passes if the argument responds to all
# of those methods.
# Example: <tt>RespondTo[:password, :credit_card]</tt>
class RespondTo < CallableClass
def initialize(*meths)
super()
@meths = meths
end
def valid?(val)
@meths.all? do |meth|
val.respond_to? meth
end
end
def to_s
"a value that responds to #{@meths.inspect}"
end
end
# Takes a variable number of method names as symbols.
# Given an argument, all of those methods are called
# on the argument one by one. If they all return true,
# the contract passes.
# Example: <tt>Send[:valid?]</tt>
class Send < CallableClass
def initialize(*meths)
super()
@meths = meths
end
def valid?(val)
@meths.all? do |meth|
val.send(meth)
end
end
def to_s
"a value that returns true for all of #{@meths.inspect}"
end
end
# Takes a class +A+. If argument is object of type +A+, the contract passes.
# If it is a subclass of A (or not related to A in any way), it fails.
# Example: <tt>Exactly[Numeric]</tt>
class Exactly < CallableClass
def initialize(cls)
super()
@cls = cls
end
def valid?(val)
val.instance_of?(@cls)
end
def to_s
"exactly #{@cls.inspect}"
end
end
# Takes a list of values, e.g. +[:a, :b, :c]+. If argument is included in
# the list, the contract passes.
#
# Example: <tt>Enum[:a, :b, :c]</tt>?
class Enum < CallableClass
def initialize(*vals)
super()
@vals = vals
end
def valid?(val)
@vals.include? val
end
end
# Takes a value +v+. If the argument is +.equal+ to +v+, the contract passes,
# otherwise the contract fails.
# Example: <tt>Eq[Class]</tt>
class Eq < CallableClass
def initialize(value)
super()
@value = value
end
def valid?(val)
@value.equal?(val)
end
def to_s
"to be equal to #{@value.inspect}"
end
end
# Takes a variable number of contracts. The contract
# passes if all of those contracts fail for the given argument.
# Example: <tt>Not[nil]</tt>
class Not < CallableClass
def initialize(*vals)
super()
@vals = vals
end
def valid?(val)
@vals.all? do |contract|
res, _ = Contract.valid?(val, contract)
!res
end
end
def to_s
"a value that is none of #{@vals.inspect}"
end
end
# @private
# Takes a collection(responds to :each) type and a contract.
# The related argument must be of specified collection type.
# Checks the contract against every element of the collection.
# If it passes for all elements, the contract passes.
# Example: <tt>CollectionOf[Array, Num]</tt>
class CollectionOf < CallableClass
def initialize(collection_class, contract)
super()
@collection_class = collection_class
@contract = contract
end
def valid?(vals)
return false unless vals.is_a?(@collection_class)
vals.all? do |val|
res, _ = Contract.valid?(val, @contract)
res
end
end
def to_s
"a collection #{@collection_class} of #{@contract}"
end
class Factory
def initialize(collection_class, &before_new)
@collection_class = collection_class
@before_new = before_new
end
def new(contract)
@before_new&.call
CollectionOf.new(@collection_class, contract)
end
alias_method :[], :new
end
end
# Takes a contract. The related argument must be an array.
# Checks the contract against every element of the array.
# If it passes for all elements, the contract passes.
# Example: <tt>ArrayOf[Num]</tt>
ArrayOf = CollectionOf::Factory.new(Array)
# Takes a contract. The related argument must be a set.
# Checks the contract against every element of the set.
# If it passes for all elements, the contract passes.
# Example: <tt>SetOf[Num]</tt>
SetOf = CollectionOf::Factory.new(Set)
# Used for <tt>*args</tt> (variadic functions). Takes a contract
# and uses it to validate every element passed in
# through <tt>*args</tt>.
# Example: <tt>Args[Or[String, Num]]</tt>
class Args < CallableClass
attr_reader :contract
def initialize(contract)
super()
@contract = contract
end
def to_s
"Args[#{@contract}]"
end
end
class Bool
def self.valid? val
val.is_a?(TrueClass) || val.is_a?(FalseClass)
end
end
# Use this to specify a Range object of a particular datatype.
# Example: <tt>RangeOf[Nat]</tt>, <tt>RangeOf[Date]</tt>, ...
class RangeOf < CallableClass
def initialize(contract)
super()
@contract = contract
end
def valid?(val)
val.is_a?(Range) &&
Contract.valid?(val.first, @contract) &&
Contract.valid?(val.last, @contract)
end
def to_s
"a range of #{@contract}"
end
end
# Use this to specify the Hash characteristics. Takes two contracts,
# one for hash keys and one for hash values.
# Example: <tt>HashOf[Symbol, String]</tt>
class HashOf < CallableClass
INVALID_KEY_VALUE_PAIR = "You should provide only one key-value pair to HashOf contract"
def initialize(key, value = nil)
super()
if value
@key = key
@value = value
else
validate_hash(key)
@key = key.keys.first
@value = key[@key]
end
end
def valid?(hash)
return false unless hash.is_a?(Hash)
keys_match = hash.keys.map { |k| Contract.valid?(k, @key) }.all?
vals_match = hash.values.map { |v| Contract.valid?(v, @value) }.all?
[keys_match, vals_match].all?
end
def to_s
"Hash<#{@key}, #{@value}>"
end
private
def validate_hash(hash)
fail ArgumentError, INVALID_KEY_VALUE_PAIR unless hash.count == 1
end
end
# Use this to specify the Hash characteristics. This contracts fails
# if there are any extra keys that don't have contracts on them.
# Example: <tt>StrictHash[{ a: String, b: Bool }]</tt>
class StrictHash < CallableClass
attr_reader :contract_hash
def initialize(contract_hash)
super()
@contract_hash = contract_hash
end
def valid?(arg)
return false unless arg.is_a?(Hash)
return false unless arg.keys.sort.eql?(contract_hash.keys.sort)
contract_hash.all? do |key, contract|
contract_hash.key?(key) && Contract.valid?(arg[key], contract)
end
end
end
# Use this for specifying contracts for keyword arguments
# Example: <tt>KeywordArgs[ e: Range, f: Optional[Num] ]</tt>
class KeywordArgs < CallableClass
def initialize(options)
super()
@options = options
end
def valid?(hash)
return false unless hash.is_a?(Hash)
return false unless hash.keys - options.keys == []
options.all? do |key, contract|
Optional._valid?(hash, key, contract)
end
end
def to_s
"KeywordArgs[#{options}]"
end
def inspect
to_s
end
private
attr_reader :options
end
# Use this for specifying contracts for class arguments
# Example: <tt>DescendantOf[ e: Range, f: Optional[Num] ]</tt>
class DescendantOf < CallableClass
def initialize(parent_class)
super()
@parent_class = parent_class
end
def valid?(given_class)
given_class.is_a?(Class) && given_class.ancestors.include?(parent_class)
end
def to_s
"DescendantOf[#{parent_class}]"
end
def inspect
to_s
end
private
attr_reader :parent_class
end
# Use this for specifying optional keyword argument
# Example: <tt>Optional[Num]</tt>
class Optional < CallableClass
UNABLE_TO_USE_OUTSIDE_OF_OPT_HASH =
"Unable to use Optional contract outside of KeywordArgs contract"
def self._valid?(hash, key, contract)
return Contract.valid?(hash[key], contract) unless contract.is_a?(Optional)
contract.within_opt_hash!
!hash.key?(key) || Contract.valid?(hash[key], contract)
end
def initialize(contract)
super()
@contract = contract
@within_opt_hash = false
end
def within_opt_hash!
@within_opt_hash = true
self
end
def valid?(value)
ensure_within_opt_hash
Contract.valid?(value, contract)
end
def to_s
"Optional[#{formatted_contract}]"
end
def inspect
to_s
end
private
attr_reader :contract, :within_opt_hash
def ensure_within_opt_hash
return if within_opt_hash
fail ArgumentError, UNABLE_TO_USE_OUTSIDE_OF_OPT_HASH
end
def formatted_contract
Formatters::InspectWrapper.create(contract)
end
end
# Takes a Contract.
# The contract passes if the contract passes or the given value is nil.
# Maybe(foo) is equivalent to Or[foo, nil].
class Maybe < Or
def initialize(*vals)
super(*(vals + [nil]))
end
def include_proc?
@vals.include? Proc
end
end
# Used to define contracts on functions passed in as arguments.
# Example: <tt>Func[Num => Num] # the function should take a number and return a number</tt>
class Func < CallableClass
attr_reader :contracts
def initialize(*contracts)
super()
@contracts = contracts
end
end
end
# Users can still include `Contracts::Core` & `Contracts::Builtin`
include Builtin
end
|