Sha256: 179230a4e23d09896daf38d66160ad4f214ed94581a5203e8bcffe513b917295

Contents?: true

Size: 1.02 KB

Versions: 19

Compression:

Stored size: 1.02 KB

Contents

module Finitio
  module Syntax
    module Contract
      include Node

      capture :type, :pair
      capture_str :contract_name

      def compile(factory, clazz)
        name = contract_name.to_sym
        infotype = type.compile(factory)
        dresser, undresser  = compile_pair(factory, clazz)
        factory.contract(infotype, dresser, undresser, name, metadata)
      end

      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
          [ Finitio::IDENTITY, Finitio::IDENTITY ]
        end
      end

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

    end # module Contract
  end # module Syntax
end # module Finitio

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
finitio-0.12.0 lib/finitio/syntax/type/contract.rb
finitio-0.11.4 lib/finitio/syntax/type/contract.rb
finitio-0.11.3 lib/finitio/syntax/type/contract.rb
finitio-0.11.2 lib/finitio/syntax/type/contract.rb
finitio-0.11.1 lib/finitio/syntax/type/contract.rb
finitio-0.10.0 lib/finitio/syntax/type/contract.rb
finitio-0.9.1 lib/finitio/syntax/type/contract.rb
finitio-0.9.0 lib/finitio/syntax/type/contract.rb
finitio-0.8.0 lib/finitio/syntax/type/contract.rb
finitio-0.7.0 lib/finitio/syntax/type/contract.rb
finitio-0.7.0.pre.rc4 lib/finitio/syntax/type/contract.rb
finitio-0.7.0.pre.rc3 lib/finitio/syntax/type/contract.rb
finitio-0.7.0.pre.rc2 lib/finitio/syntax/type/contract.rb
finitio-0.7.0.pre.rc1 lib/finitio/syntax/type/contract.rb
finitio-0.6.1 lib/finitio/syntax/type/contract.rb
finitio-0.6.0 lib/finitio/syntax/type/contract.rb
finitio-0.5.2 lib/finitio/syntax/type/contract.rb
finitio-0.5.1 lib/finitio/syntax/type/contract.rb
finitio-0.5.0 lib/finitio/syntax/type/contract.rb