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
-
.desired_method_names(inner_class) ⇒ Object
Generate the list of method names that we’d like to proxy for inner classes.
-
.included(inner_class) ⇒ Object
Don’t do all of the work unless we have an inner class that needs it.
-
.proxy_constants(inner_class) ⇒ Object
Proxy the sketch’s constants on to the inner classes.
-
.proxy_methods(inner_class) ⇒ Object
Proxy methods through to the sketch.
Class Method Details
.desired_method_names(inner_class) ⇒ Object
Generate the list of method names that we’d like to proxy for inner classes. Nothing camelCased, nothing __internal__, just the Processing API.
209 210 211 212 213 214 215 |
# File 'lib/ruby-processing/app.rb', line 209 def self.desired_method_names(inner_class) 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) || inner_class.method_defined?(m) } end |
.included(inner_class) ⇒ Object
Don’t do all of the work unless we have an inner class that needs it.
242 243 244 245 |
# File 'lib/ruby-processing/app.rb', line 242 def self.included(inner_class) proxy_methods(inner_class) proxy_constants(inner_class) end |
.proxy_constants(inner_class) ⇒ Object
Proxy the sketch’s constants on to the inner classes.
234 235 236 237 238 239 |
# File 'lib/ruby-processing/app.rb', line 234 def self.proxy_constants(inner_class) Processing::App.constants.each do |name| next if inner_class.const_defined?(name) inner_class.const_set(name, Processing::App.const_get(name)) end end |
.proxy_methods(inner_class) ⇒ Object
Proxy methods through to the sketch.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/ruby-processing/app.rb', line 218 def self.proxy_methods(inner_class) code = desired_method_names(inner_class).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 inner_class.class_eval(code) end |