Module: Rstructural::ADT

Included in:
Either, Option
Defined in:
lib/rstructural/adt.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



6
7
8
9
# File 'lib/rstructural/adt.rb', line 6

def self.extended(klass)
  klass.class_variable_set(:@@adt_types, [])
  klass.class_variable_set(:@@adt_super_class, klass)
end

Instance Method Details

#const(value = nil, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rstructural/adt.rb', line 11

def const(value = nil, &block)
  if value
    Rstructural::Struct.new(:value, __caller: caller, &block).new(value)
  else
    Rstructural::Struct.new(__caller: caller, &block).new
  end.tap do |k|
    self.class_variable_get(:@@adt_types) << k
    k.class.include(self.class_variable_get(:@@adt_super_class))

    def k.name
      self.class.name
    end
  end
end

#data(*fields, &block) ⇒ Object



26
27
28
29
30
31
# File 'lib/rstructural/adt.rb', line 26

def data(*fields, &block)
  Rstructural::Struct.new(*fields, __caller: caller, &block).tap do |k|
    k.include(self.class_variable_get(:@@adt_super_class))
    self.class_variable_get(:@@adt_types) << k
  end
end

#interface(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/rstructural/adt.rb', line 33

def interface(&block)
  self.class_variable_get(:@@adt_types).each do |t|
    case t
    in Class
      t.class_eval(&block)
    else
      t.class.class_eval(&block)
    end
  end
end