Class: Vips::Introspect

Inherits:
Object
  • Object
show all
Defined in:
lib/vips/operation.rb

Overview

Introspect a vips operation and return a large structure containing everything we know about it. This is used for doc generation as well as call.

Constant Summary collapse

@@introspect_cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Introspect

Returns a new instance of Introspect.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/vips/operation.rb', line 55

def initialize name
  # if there's a trailing "!", this is a destructive version of an
  # operation
  if name[-1] == "!"
    @destructive = true
    # strip the trailing "!"
    @vips_name = name[0...-1]
  else
    @destructive = false
    @vips_name = name
  end

  @op = Operation.new @vips_name
  @args = []
  @required_input = []
  @optional_input = {}
  @required_output = []
  @optional_output = {}

  # find all the arguments the operator can take
  @op.argument_map do |pspec, argument_class, _argument_instance|
    flags = argument_class[:flags]
    if (flags & ARGUMENT_CONSTRUCT) != 0
      # names can include - as punctuation, but we always use _ in
      # Ruby
      arg_name = pspec[:name].tr("-", "_")
      @args << {
        arg_name: arg_name,
        flags: flags,
        gtype: pspec[:value_type]
      }
    end

    nil
  end

  @args.each do |details|
    arg_name = details[:arg_name]
    flags = details[:flags]

    if (flags & ARGUMENT_INPUT) != 0
      if (flags & ARGUMENT_REQUIRED) != 0 &&
          (flags & ARGUMENT_DEPRECATED) == 0
        @required_input << details
      else
        # we allow deprecated optional args
        @optional_input[arg_name] = details
      end

      # MODIFY INPUT args count as OUTPUT as well in non-destructive mode
      if (flags & ARGUMENT_MODIFY) != 0 &&
          !@destructive
        if (flags & ARGUMENT_REQUIRED) != 0 &&
            (flags & ARGUMENT_DEPRECATED) == 0
          @required_output << details
        else
          @optional_output[arg_name] = details
        end
      end
    elsif (flags & ARGUMENT_OUTPUT) != 0
      if (flags & ARGUMENT_REQUIRED) != 0 &&
          (flags & ARGUMENT_DEPRECATED) == 0
        @required_output << details
      else
        # again, allow deprecated optional args
        @optional_output[arg_name] = details
      end
    end
  end

  # in destructive mode, the first required input arg must be MODIFY and
  # must be an image
  if @destructive
    if @required_input.length < 1 ||
        @required_input[0][:flags] & ARGUMENT_MODIFY == 0 ||
        @required_input[0][:gtype] != IMAGE_TYPE
      raise Vips::Error, "operation #{@vips_name} is not destructive"
    end
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



49
50
51
# File 'lib/vips/operation.rb', line 49

def args
  @args
end

#descriptionObject (readonly)

Returns the value of attribute description.



49
50
51
# File 'lib/vips/operation.rb', line 49

def description
  @description
end

#destructiveObject (readonly)

Returns the value of attribute destructive.



49
50
51
# File 'lib/vips/operation.rb', line 49

def destructive
  @destructive
end

#flagsObject (readonly)

Returns the value of attribute flags.



49
50
51
# File 'lib/vips/operation.rb', line 49

def flags
  @flags
end

#member_xObject (readonly)

Returns the value of attribute member_x.



49
50
51
# File 'lib/vips/operation.rb', line 49

def member_x
  @member_x
end

#method_argsObject (readonly)

Returns the value of attribute method_args.



49
50
51
# File 'lib/vips/operation.rb', line 49

def method_args
  @method_args
end

#nameObject (readonly)

Returns the value of attribute name.



49
50
51
# File 'lib/vips/operation.rb', line 49

def name
  @name
end

#optional_inputObject (readonly)

Returns the value of attribute optional_input.



49
50
51
# File 'lib/vips/operation.rb', line 49

def optional_input
  @optional_input
end

#optional_outputObject (readonly)

Returns the value of attribute optional_output.



49
50
51
# File 'lib/vips/operation.rb', line 49

def optional_output
  @optional_output
end

#required_inputObject (readonly)

Returns the value of attribute required_input.



49
50
51
# File 'lib/vips/operation.rb', line 49

def required_input
  @required_input
end

#required_outputObject (readonly)

Returns the value of attribute required_output.



49
50
51
# File 'lib/vips/operation.rb', line 49

def required_output
  @required_output
end

#vips_nameObject (readonly)

Returns the value of attribute vips_name.



49
50
51
# File 'lib/vips/operation.rb', line 49

def vips_name
  @vips_name
end

Class Method Details

.get(name) ⇒ Object



169
170
171
# File 'lib/vips/operation.rb', line 169

def self.get name
  @@introspect_cache[name] ||= Introspect.new name
end

.get_yard(name) ⇒ Object



173
174
175
176
177
# File 'lib/vips/operation.rb', line 173

def self.get_yard name
  introspect = Introspect.get name
  introspect.add_yard_introspection name
  introspect
end

Instance Method Details

#add_yard_introspection(name) ⇒ Object

Yard comment generation needs a little more introspection. We add this extra metadata in a separate method to keep the main path as fast as we can.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/vips/operation.rb', line 139

def add_yard_introspection name
  @name = name
  @description = Vips.vips_object_get_description @op
  @flags = Vips.vips_operation_get_flags @op
  @member_x = nil
  @method_args = []

  @args.each do |details|
    arg_name = details[:arg_name]
    flags = details[:flags]
    gtype = details[:gtype]

    details[:yard_name] = arg_name == "in" ? "im" : arg_name
    pspec = @op.get_pspec arg_name
    details[:blurb] = GObject.g_param_spec_get_blurb pspec

    if (flags & ARGUMENT_INPUT) != 0 &&
        (flags & ARGUMENT_REQUIRED) != 0 &&
        (flags & ARGUMENT_DEPRECATED) == 0
      # the first required input image is the thing we will be a method
      # of
      if @member_x.nil? && gtype == IMAGE_TYPE
        @member_x = details
      else
        @method_args << details
      end
    end
  end
end