Module: Vips::Yard

Defined in:
lib/vips/image.rb

Overview

This module generates yard comments for all the dynamically bound vips operations.

Regenerate with something like:

$ ruby > methods.rb
require 'vips'; Vips::Yard.generate
^D

Constant Summary collapse

MAP_GO_TO_RUBY =

map gobject's type names to Ruby

{
  "gboolean" => "Boolean",
  "gint" => "Integer",
  "gdouble" => "Float",
  "gfloat" => "Float",
  "gchararray" => "String",
  "VipsImage" => "Vips::Image",
  "VipsInterpolate" => "Vips::Interpolate",
  "VipsConnection" => "Vips::Connection",
  "VipsSource" => "Vips::Source",
  "VipsTarget" => "Vips::Target",
  "VipsSourceCustom" => "Vips::SourceCustom",
  "VipsTargetCustom" => "Vips::TargetCustom",
  "VipsArrayDouble" => "Array<Double>",
  "VipsArrayInt" => "Array<Integer>",
  "VipsArrayImage" => "Array<Image>",
  "VipsArrayString" => "Array<String>",
}
NO_GENERATE =

these have hand-written methods, see above

["scale", "bandjoin", "composite", "ifthenelse"]
ALIAS =

these are aliased (appear under several names)

["crop"]

Class Method Summary collapse

Class Method Details

.generateObject



1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
# File 'lib/vips/image.rb', line 1529

def self.generate
  alias_gtypes = {}
  ALIAS.each do |name| 
    gtype = Vips::type_find "VipsOperation", name
    alias_gtypes[gtype] = name 
  end

  generate_class = lambda do |gtype, _|
    if alias_gtypes.key? gtype
      name = alias_gtypes[gtype]
    else
      name = Vips::nickname_find gtype
    end

    if name
      begin
        # can fail for abstract types
        introspect = Vips::Introspect.get_yard name
      rescue Vips::Error
        nil
      end

      generate_operation(introspect) if introspect
    end

    Vips::vips_type_map gtype, generate_class, nil
  end

  puts "module Vips"
  puts "  class Image"
  puts ""

  generate_class.(GObject::g_type_from_name("VipsOperation"), nil)

  puts "  end"
  puts "end"
end

.generate_operation(introspect) ⇒ Object



1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
# File 'lib/vips/image.rb', line 1461

def self.generate_operation introspect
  return if (introspect.flags & OPERATION_DEPRECATED) != 0
  return if NO_GENERATE.include? introspect.name

  method_args = introspect.method_args
  required_output = introspect.required_output
  optional_input = introspect.optional_input
  optional_output = introspect.optional_output

  print "# @!method "
  print "self." unless introspect.member_x
  print "#{introspect.name}("
  print method_args.map{ |x| x[:yard_name] }.join(", ")
  print ", " if method_args.length > 0
  puts "**opts)"

  puts "#   #{introspect.description.capitalize}."

  method_args.each do |details|
    yard_name = details[:yard_name]
    gtype = details[:gtype]
    blurb = details[:blurb]

    puts "#   @param #{yard_name} [#{gtype_to_ruby(gtype)}] #{blurb}"
  end

  puts "#   @param opts [Hash] Set of options"
  optional_input.each do |arg_name, details|
    yard_name = details[:yard_name]
    gtype = details[:gtype]
    blurb = details[:blurb]

    puts "#   @option opts [#{gtype_to_ruby(gtype)}] :#{yard_name} " +
         "#{blurb}"
  end
  optional_output.each do |arg_name, details|
    yard_name = details[:yard_name]
    gtype = details[:gtype]
    blurb = details[:blurb]

    print "#   @option opts [#{gtype_to_ruby(gtype)}] :#{yard_name}"
    puts " Output #{blurb}"
  end

  print "#   @return ["
  if required_output.length == 0
    print "nil"
  elsif required_output.length == 1
    print gtype_to_ruby(required_output.first[:gtype])
  else
    print "Array<"
    print required_output.map{ |x| gtype_to_ruby(x[:gtype]) }.join(", ")
    print ">"
  end
  if optional_output.length > 0
    print ", Hash<Symbol => Object>"
  end
  print "] "
  print required_output.map{ |x| x[:blurb] }.join(", ")
  if optional_output.length > 0
    print ", " if required_output.length > 0
    print "Hash of optional output items"
  end
  puts ""

  puts ""
end

.gtype_to_ruby(gtype) ⇒ Object

turn a gtype into a ruby type name



1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
# File 'lib/vips/image.rb', line 1445

def self.gtype_to_ruby gtype
  fundamental = GObject::g_type_fundamental gtype
  type_name = GObject::g_type_name gtype

  if MAP_GO_TO_RUBY.include? type_name
    type_name = MAP_GO_TO_RUBY[type_name]
  end

  if fundamental == GObject::GFLAGS_TYPE ||
     fundamental == GObject::GENUM_TYPE
    type_name = "Vips::" + type_name[/Vips(.*)/, 1]
  end

  type_name
end