Class: Uinit::Type::Type

Inherits:
Base
  • Object
show all
Defined in:
lib/uinit/type/type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, [], #is!, #to_s, #trace!, #type_error!

Methods included from Operators

#&, #|

Constructor Details

#initialize(class_module) ⇒ Type

TODO: Allow to pass Fn



15
16
17
18
19
# File 'lib/uinit/type/type.rb', line 15

def initialize(class_module)
  super()

  @class_module = class_module
end

Instance Attribute Details

#class_moduleObject (readonly)

Returns the value of attribute class_module.



21
22
23
# File 'lib/uinit/type/type.rb', line 21

def class_module
  @class_module
end

Class Method Details

.from(value) ⇒ Object



10
11
12
# File 'lib/uinit/type/type.rb', line 10

def self.from(value)
  new(value) if from?(value)
end

.from?(value) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/uinit/type/type.rb', line 6

def self.from?(value)
  value.is_a?(Class) || value.is_a?(Module)
end

Instance Method Details

#check!(value, depth) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/uinit/type/type.rb', line 29

def check!(value, depth)
  unless value.is_a?(Class) || value.is_a?(Module)
    type_error!("#{value.inspect} is not a Class or a Module", depth)
  end

  return value if value.ancestors.include?(class_module)

  type_error!("#{value.inspect} does not extend or include or preprend #{class_module}", depth)
end

#inspectObject



39
40
41
# File 'lib/uinit/type/type.rb', line 39

def inspect
  "#{super}[#{class_module.inspect}]"
end

#is?(value) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/uinit/type/type.rb', line 23

def is?(value)
  return false unless value.is_a?(Class) || value.is_a?(Module)

  value.ancestors.include?(class_module)
end