Module: Qrb::Syntax::Contract

Defined in:
lib/qrb/syntax/contract.rb

Instance Method Summary collapse

Instance Method Details

#compile(factory, clazz) ⇒ Object



5
6
7
8
# File 'lib/qrb/syntax/contract.rb', line 5

def compile(factory, clazz)
  contract = [ type.compile(factory) ] + compile_pair(factory, clazz)
  { contract_name.to_sym => contract }
end

#compile_pair(factory, clazz) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/qrb/syntax/contract.rb', line 10

def compile_pair(factory, clazz)
  if pair
    pair.compile(factory)
  elsif clazz
    dresser   = clazz.method(contract_name.to_sym)
    undresser = clazz.instance_method(:"to_#{contract_name}")
    [
      dresser,
      ->(d){ undresser.bind(d).call }
    ]
  else
    [ Qrb::IDENTITY, Qrb::IDENTITY ]
  end
end

#to_astObject



25
26
27
28
29
30
31
32
33
# File 'lib/qrb/syntax/contract.rb', line 25

def to_ast
  ast = [
    :contract,
    contract_name.to_s,
    (type && type.to_ast) 
  ]
  ast << pair.to_ast if pair
  ast
end