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



1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
# File 'lib/vips/image.rb', line 1746

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, _|
    name = if alias_gtypes.key? gtype
      alias_gtypes[gtype]
    else
      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.call(GObject.g_type_from_name("VipsOperation"), nil)

  puts "  end"
  puts "end"
end

.generate_operation(introspect) ⇒ Object



1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
# File 'lib/vips/image.rb', line 1678

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.doc_optional_input
  optional_output = introspect.doc_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]
    rtype = gtype_to_ruby gtype
    blurb = details[:blurb]

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

    puts "#   @option opts [#{rtype}] :#{yard_name} 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



1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
# File 'lib/vips/image.rb', line 1662

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