Module: Processing::Proxy

Includes:
Math
Defined in:
lib/ruby-processing/app.rb

Overview

This module will get automatically mixed in to any inner class of a Processing::App, in order to mimic Java’s inner classes, which have unfettered access to the methods defined in the surrounding class.

Class Method Summary collapse

Class Method Details

.desired_method_namesObject

Generate the list of method names that we’d like to proxy for inner classes. Nothing camelCased, nothing __internal__, just the Processing API.



258
259
260
261
262
263
264
# File 'lib/ruby-processing/app.rb', line 258

def self.desired_method_names
  bad_method = /__/    # Internal JRuby methods.
  unwanted = PApplet.superclass.instance_methods + Object.instance_methods
  unwanted -= ['width', 'height', 'cursor', 'create_image', 'background', 'size', 'resize']
  methods = Processing::App.public_instance_methods
  methods.reject {|m| unwanted.include?(m) || bad_method.match(m) }
end

.included(inner_class) ⇒ Object

Don’t do all of the work unless we have an inner class that needs it.



293
294
295
296
297
298
# File 'lib/ruby-processing/app.rb', line 293

def self.included(inner_class)
  return if @already_defined
  proxy_methods
  proxy_constants
  @already_defined = true
end

.proxy_constantsObject

Proxy the sketch’s constants on to the inner classes.



285
286
287
288
289
# File 'lib/ruby-processing/app.rb', line 285

def self.proxy_constants
  Processing::App.constants.each do |name|
    Processing::Proxy.const_set(name, Processing::App.const_get(name))
  end
end

.proxy_methodsObject

Proxy methods through to the sketch.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/ruby-processing/app.rb', line 268

def self.proxy_methods
  code = desired_method_names.inject('') do |code, method|
    code << <<-EOS
      def #{method}(*args, &block)                # def rect(*args, &block)
        if block_given?                           #   if block_given?
          $app.send :'#{method}', *args, &block   #     $app.send(:rect, *args, &block)
        else                                      #   else
          $app.#{method} *args                    #     $app.rect *args
        end                                       #   end
      end                                         # end
    EOS
  end
  module_eval(code, "Processing::Proxy", 1)
end