Class: Contracts::Builtin::DescendantOf

Inherits:
CallableClass show all
Defined in:
lib/contracts/builtin_contracts.rb

Overview

Use this for specifying contracts for class arguments Example: DescendantOf[ e: Range, f: Optional[Num] ]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(parent_class) ⇒ DescendantOf

Returns a new instance of DescendantOf.



474
475
476
477
# File 'lib/contracts/builtin_contracts.rb', line 474

def initialize(parent_class)
  super()
  @parent_class = parent_class
end

Instance Method Details

#inspectObject



487
488
489
# File 'lib/contracts/builtin_contracts.rb', line 487

def inspect
  to_s
end

#to_sObject



483
484
485
# File 'lib/contracts/builtin_contracts.rb', line 483

def to_s
  "DescendantOf[#{parent_class}]"
end

#valid?(given_class) ⇒ Boolean

Returns:

  • (Boolean)


479
480
481
# File 'lib/contracts/builtin_contracts.rb', line 479

def valid?(given_class)
  given_class.is_a?(Class) && given_class.ancestors.include?(parent_class)
end