Sha256: 92bdb8d82390b6a0d504932f9533d4f3cf7cd2dfd76720493dfc46b8d575f411

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

module GirFFI
  # Base class for builders of #method_missing definitions.
  class MethodMissingDefinitionBuilder
    def initialize lib, namespace
      @lib = lib
      @namespace = namespace
    end

    def generate
      args = arguments.map {|arg| "\"#{arg}\""}

      return <<-CODE
	def #{slf}method_missing method, *arguments, &block
	  result = GirFFI::Builder.#{fn} #{args.join ', '}, #{libs.join ', '}, self, method.to_s
	  return super unless result
	  if block.nil?
	    self.send method, *arguments
	  else
	    self.send method, *arguments, &block
	  end
	end
      CODE
    end
  end

  # Builds a #method_missing for a module. This method_missing will be
  # called for missing module methods; these modules are not meant for
  # #include'ing.
  class ModuleMethodMissingDefinitionBuilder < MethodMissingDefinitionBuilder
    private

    def slf; "self."; end
    def fn; "setup_function"; end
    def arguments; [@namespace]; end
    def libs; [@lib]; end
  end

  # Builds a #method_missing for missing instance methods.
  class InstanceMethodMissingDefinitionBuilder < MethodMissingDefinitionBuilder
    def initialize lib, modul, namespace, classname
      super lib, namespace
      @classname = classname
      @module = modul
    end

    private

    def slf; ""; end
    def fn; "setup_method"; end
    def arguments; [@namespace, @classname]; end
    def libs; [@lib, @module]; end
  end

  # Builds a #method_missing for missing class methods.
  class ClassMethodMissingDefinitionBuilder < InstanceMethodMissingDefinitionBuilder
    private

    def slf; "self."; end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gir_ffi-0.0.1 lib/gir_ffi/method_missing_definition_builder.rb