Sha256: 6969fc355056503c2a4521601aa41f86687709cf617c1ddf01ead9ab8d8f4fc5

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'gir_ffi/builders/argument_builder_collection'
require 'gir_ffi/builders/method_template'
require 'gir_ffi/builders/null_return_value_builder'

module GirFFI
  module Builders
    # Implements the creation of a Ruby constructor definition out of a
    # GIR IFunctionInfo.
    class ConstructorBuilder
      def initialize(info)
        @info = info
        return_value_builder = NullReturnValueBuilder.new
        arg_builders = ArgumentBuilderCollection.new(return_value_builder, [])
        @template = MethodTemplate.new(self, arg_builders)
      end

      def method_definition
        @template.method_definition
      end

      def singleton_method?
        true
      end

      def method_name
        @info.safe_name
      end

      def method_arguments
        ['*args', '&block']
      end

      def preparation
        if @info.safe_name == 'new'
          ['obj = allocate']
        else
          [
            "raise NoMethodError unless self == #{@info.container.full_type_name}",
            'obj = allocate'
          ]
        end
      end

      def invocation
        "obj.__send__ #{initializer_name.to_sym.inspect}, #{method_arguments.join(', ')}"
      end

      def result
        ['obj']
      end

      private

      def initializer_name
        @info.safe_name.sub(/^new/, 'initialize')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gir_ffi-0.9.2 lib/gir_ffi/builders/constructor_builder.rb
gir_ffi-0.9.1 lib/gir_ffi/builders/constructor_builder.rb
gir_ffi-0.9.0 lib/gir_ffi/builders/constructor_builder.rb