Class: Deterministic::ProtocolBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/deterministic/protocol.rb

Defined Under Namespace

Classes: Signature

Instance Method Summary collapse

Constructor Details

#initialize(typevar, block) ⇒ ProtocolBuilder

Returns a new instance of ProtocolBuilder.



4
5
6
7
# File 'lib/deterministic/protocol.rb', line 4

def initialize(typevar, block)
  @typevar, @block = typevar, block
  @protocol = Class.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



14
15
16
# File 'lib/deterministic/protocol.rb', line 14

def method_missing(m, *args)
  [m, args]
end

Instance Method Details

#buildObject



9
10
11
12
# File 'lib/deterministic/protocol.rb', line 9

def build
  instance_exec(&@block)
  @protocol
end

#fn(signature, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/deterministic/protocol.rb', line 20

def fn(signature, &block)
  m = signature.to_a.flatten
  name        = m[0]
  return_type = m[-1]
  params      = Hash[(m[1..-2][0] || {}).map { |k, v| [k[0], v] }]

  @protocol.instance_eval {
      define_singleton_method(name) {
        Signature.new(name, params, return_type, block)
      }
  }

  @protocol.instance_eval {
    if block
      define_method(name) { |*args|
        block.call(args)
      }
    end
  }
end