Class: Uinit::Type::Impl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Operators

#&, #|

Constructor Details

#initialize(*sym_interface, **interface) ⇒ Impl

TODO: Allow to pass Fn



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/uinit/type/impl.rb', line 7

def initialize(*sym_interface, **interface)
  super()

  sym_interface.each do |meth|
    next if meth.is_a?(Symbol)

    raise ArgumentError, "#{meth.inspect} must be a Symbol"
  end

  interface.each do |(meth, fn)|
    next if fn.nil? || fn.is_a?(Fn)

    raise ArgumentError, "#{meth.inspect} must be Fn or Nil"
  end

  @interface =
    sym_interface.each_with_object(interface.dup) do |meth, inter|
      inter[meth] = nil
    end
end

Instance Attribute Details

#interfaceObject (readonly)

Returns the value of attribute interface.



28
29
30
# File 'lib/uinit/type/impl.rb', line 28

def interface
  @interface
end

Instance Method Details

#check!(value, depth) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/uinit/type/impl.rb', line 40

def check!(value, depth)
  interface.each do |meth, fn|
    is = value.respond_to?(meth)

    type_error!("#{value.inspect} do not implement #{meth.inspect} method", depth) unless is

    next if fn.nil?

    begin
      fn.check!(value.method(meth), depth + 1)
    rescue Error => e
      trace!(e, ".#{meth}()")
    end
  end

  value
end

#inspectObject



58
59
60
# File 'lib/uinit/type/impl.rb', line 58

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

#is?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def is?(value)
  interface.all? do |(meth, fn)|
    is = value.respond_to?(meth)

    next is if !is || fn.nil?

    fn.is?(value.method(meth))
  end
end