Class: Types::Class
- Inherits:
-
Object
- Object
- Types::Class
- Extended by:
- Generic
- Includes:
- Generic
- Defined in:
- lib/types/class.rb
Overview
Represents a class type, optionally constrained to a base class.
“‘ruby type = Types::Class(String) type.parse(“Array”) # => Array “`
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Class Method Summary collapse
- .composite? ⇒ Boolean
-
.parse(input) ⇒ Object
Parses the input as a class, raising if not a class.
-
.resolve ⇒ Object
Resolves to the actual Ruby Class class.
Instance Method Summary collapse
- #composite? ⇒ Boolean
-
#initialize(base) ⇒ Class
constructor
A new instance of Class.
-
#parse(input) ⇒ Object
Parses the input as a class, optionally checking the base class constraint.
-
#resolve ⇒ Object
Resolve the base class if possible.
- #to_rbs ⇒ Object
Methods included from Generic
Constructor Details
#initialize(base) ⇒ Class
Returns a new instance of Class.
21 22 23 |
# File 'lib/types/class.rb', line 21 def initialize(base) @base = base end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
26 27 28 |
# File 'lib/types/class.rb', line 26 def base @base end |
Class Method Details
.parse(input) ⇒ Object
Parses the input as a class, raising if not a class.
70 71 72 73 74 75 76 77 78 |
# File 'lib/types/class.rb', line 70 def self.parse(input) klass = Object.const_get(input) if !klass.is_a?(::Class) raise ArgumentError, "Class #{klass} is not a Class!" end return klass end |
.resolve ⇒ Object
Resolves to the actual Ruby Class class.
82 83 84 |
# File 'lib/types/class.rb', line 82 def self.resolve ::Class end |
Instance Method Details
#parse(input) ⇒ Object
Parses the input as a class, optionally checking the base class constraint.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/types/class.rb', line 45 def parse(input) klass = Object.const_get(input) base = self.resolve if base and !klass.ancestors.include?(base) raise ArgumentError, "Class #{klass} is not a subclass of #{base}!" end return klass end |
#resolve ⇒ Object
Resolve the base class if possible.
35 36 37 38 39 |
# File 'lib/types/class.rb', line 35 def resolve Object.const_get(@base.to_s) rescue NameError nil end |
#to_rbs ⇒ Object
62 63 64 |
# File 'lib/types/class.rb', line 62 def to_rbs "Class" end |