Top Level Namespace

Defined Under Namespace

Modules: ADT

Instance Method Summary collapse

Instance Method Details

#ADT(&block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/adt.rb', line 7

def ADT(&block)
  Class.new.tap do |klass|
    o = Object.new

    o.define_singleton_method(:method_missing) do |m, *args, &block|
      ADT::Constructor.new(klass, m, args.first || {}, &block)
    end

    first = o.instance_eval(&block)
    [first, *first.others].each do |tc|
      klass.const_set(tc.name, tc.klass)
      klass.singleton_class.send(:define_method, tc.name) do |*args|
        const_get(tc.name).new(*args)
      end
    end
  end
end