Class: Uinit::Type::Instance

Inherits:
Base
  • Object
show all
Defined in:
lib/uinit/type/instance.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) ⇒ Instance

Returns a new instance of Instance.

Raises:

  • (ArgumentError)


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

def initialize(class_module)
  super()

  raise ArgumentError, 'class_module must be a Class or a Module' unless self.class.from?(class_module)

  @class_module = class_module
end

Instance Attribute Details

#class_moduleObject (readonly)

Returns the value of attribute class_module.



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

def class_module
  @class_module
end

Class Method Details

.from(class_module) ⇒ Object



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

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

.from?(class_module) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#check!(value, depth) ⇒ Object



28
29
30
31
32
# File 'lib/uinit/type/instance.rb', line 28

def check!(value, depth)
  return value if is?(value)

  type_error!("#{value.inspect} must be an instance of #{class_module}", depth)
end

#inspectObject



34
35
36
# File 'lib/uinit/type/instance.rb', line 34

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

#is?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def is?(value)
  value.is_a?(class_module)
end