Skip to content

Instantly share code, notes, and snippets.

@dajester2013
Created December 17, 2013 05:43
Show Gist options
  • Save dajester2013/8000580 to your computer and use it in GitHub Desktop.
Save dajester2013/8000580 to your computer and use it in GitHub Desktop.
object to create dynamic proxy from a struct of closures in addition to CFCs.
/* The MIT License (MIT)
*
* Copyright (c) 2013 Jesse Shaffer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* InterfaceInstance
* =================
* Get an instance of a java interface (so long as it is loaded into the classpath accessible by createdynamicproxy)
* Can be used to wrap a CFC by instance or path, or a struct of closures
*
* Example (quick runnable instance):
*
* runnable = new InterfaceInstance("java.lang.Runnable",{
* run : function() {
* writeoutput("ran with it!");
* }
* });
*
* @author jesse.shaffer
* @license MIT (http://opensource.org/licenses/MIT)
* @date 11/16/13
**/
component output=false persistent=false {
/**
* Constructor - returns a dynamic proxy based on the passed interface(s).
* @interface [String|Array] The interface as you would pass to createDynamicProxy()
* @wrapped [Object|String|Struct] A CFC instance, a CFC path, or a struct of closure functions.
**/
public function init(required any interface, required wrapped) {
if (!isObject(wrapped) && isStruct(wrapped)) {
structAppend(this,wrapped);
wrapped = this;
} else if (isValid("string",wrapped)) {
wrapped = createObject("component",wrapped);
}
if (!isArray(interface)) interface = [interface];
if (!isObject(wrapped)) wrapped = this;
return createDynamicProxy(wrapped,interface);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment