class GirFFI::ClassPrettyPrinter

Pretty-prints classes

Public Class Methods

new(klass) click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 6
def initialize(klass)
  @klass = klass
end

Public Instance Methods

pretty_print() click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 10
def pretty_print
  arr = []
  arr << "class #{@klass.name} < #{@klass.superclass.name}"
  arr << pretty_print_singleton_methods.indent
  arr << pretty_print_instance_methods.indent
  arr << "end"
  arr.join "\n"
end

Private Instance Methods

instance_method_list() click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 32
def instance_method_list
  @instance_method_list ||=
    instance_method_name_list.map { |name| @klass.instance_method name }
end
instance_method_name_list() click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 28
def instance_method_name_list
  @klass.instance_methods(false).sort
end
method_source(meth) click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 49
def method_source(meth)
  if meth.arity == -1
    name = meth.name.to_s

    if @klass.gir_info.find_instance_method name
      @klass.setup_instance_method name
    elsif name == "_"
      @klass.setup_instance_method ""
    end

    meth = @klass.instance_method name
  end

  begin
    meth.to_ruby
  rescue LiveAST::ASTNotFoundError => e
    warn "Printing #{@klass.name}##{meth.name} failed: #{e.message}"
  end
end
pretty_print_alias(meth) click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 37
def pretty_print_alias(meth)
  return if meth.name == meth.original_name

  "alias_method '#{meth.name}', '#{meth.original_name}'"
end
pretty_print_instance_methods() click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 21
def pretty_print_instance_methods
  arr =
    instance_method_list.map { |mth| pretty_print_method(mth) } +
    instance_method_list.map { |mth| pretty_print_alias(mth) }
  arr.compact.join "\n"
end
pretty_print_method(meth) click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 43
def pretty_print_method(meth)
  return if meth.name != meth.original_name

  method_source meth
end
pretty_print_singleton_methods() click to toggle source
# File lib/gir_ffi-pretty_printer/class_pretty_printer.rb, line 69
def pretty_print_singleton_methods
  arr = []
  @klass.methods(false).sort.each do |mname|
    meth = @klass.method mname
    ruby = meth.to_ruby

    if meth.arity == -1 && ruby =~ /setup_and_call/
      @klass.setup_method mname.to_s
      meth = @klass.method mname
      ruby = meth.to_ruby
    end

    arr << ruby
  end
  arr.join "\n"
end