forked from framework-one/cfmljure
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned up caching. Added namespace tagging and caching. Added implic…
…it calling (via onMissingMethod). Added installation to a scope from a configuration. Updated main example to show three usage patterns. Added advanced / automated example using Application.cfc to automatically install Clojure and namespaces based on configuration.
- Loading branch information
1 parent
2701e7c
commit 5603015
Showing
7 changed files
with
248 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
component { | ||
this.name = hash( getBaseTemplatePath() ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
component { | ||
this.name = hash( getBaseTemplatePath() ); | ||
// cfmljure configuration: | ||
config = { | ||
project = 'cfml', | ||
files = 'cfml/examples', | ||
ns = 'cfml.examples, clojure.core' | ||
}; | ||
|
||
// magic that auto-installs Clojure stuff into variables scope: | ||
function onRequestStart() { | ||
if ( !structKeyExists( application, 'clj') || | ||
( structKeyExists( URL, 'reload' ) && isBoolean( URL.reload ) && URL.reload ) ) { | ||
application.clj = new cfmljure(); | ||
} | ||
application.clj.install( config, variables ); | ||
} | ||
|
||
function onRequest( string targetPath ) { | ||
include targetPath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<cfset start = getTickCount() /> | ||
<cfscript> | ||
writeOutput( '<h1>Calls via implicit method lookup after implicit installation</h1>' ); | ||
// call functions: | ||
// simple function call: | ||
writeOutput( '(greet "World") = ' & cfml.examples.greet( 'World' ) & '<br />' ); | ||
// pass CFML array to Clojure and loop over Clojure sequence that comes back: | ||
list = cfml.examples.twice( [ 1, 2, 3 ] ); | ||
writeOutput( '(twice [ 1 2 3 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
// simple function call (times2 is def'd to an anonymous function literal: | ||
writeOutput( '(times2 42) = ' & cfml.examples.times2( 42 ) & '<br />' ); | ||
// call built-in Clojure function, passing raw definition of times2 function: | ||
list = clojure.core.map( cfml.examples.get( 'times2' ).defn, [ 4, 5, 6 ] ); | ||
writeOutput( '(map times2 [ 4 5 6 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
</cfscript> | ||
<cfset end = getTickCount() /> | ||
<cfoutput> | ||
Time taken: #end - start#ms.<br /> | ||
<a href="?reload=true">Reload the runtime</a>. | ||
</cfoutput> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
component { | ||
this.name = hash( getBaseTemplatePath() ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<cfset start = getTickCount() /> | ||
<cfscript> | ||
// load Clojure runtime (for cfml project - search path root is clj/cfml/src/): | ||
clj = new cfmljure( 'cfml' ); | ||
// load scripts (from project source folder - that's clj/cfml/src/cfml/examples.clj): | ||
clj.load( 'cfml/examples' ); | ||
// we can either get handles on specific functions like this: | ||
writeOutput( '<h1>Calls via explicit handles on methods</h1>' ); | ||
// get handle on individual functions (from namespace cfml.examples): | ||
greet = clj.get( 'cfml.examples.greet' ); | ||
twice = clj.get( 'cfml.examples.twice' ); | ||
times2 = clj.get( 'cfml.examples.times2' ); | ||
// get handle on built-in map function (from namespace clojure.core): | ||
map = clj.get( 'clojure.core.map' ); | ||
// call functions: | ||
// simple function call: | ||
writeOutput( '(greet "World") = ' & greet.call( 'World' ) & '<br />' ); | ||
// pass CFML array to Clojure and loop over Clojure sequence that comes back: | ||
list = twice.call( [ 1, 2, 3 ] ); | ||
writeOutput( '(twice [ 1 2 3 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
// simple function call (times2 is def'd to an anonymous function literal: | ||
writeOutput( '(times2 42) = ' & times2.call( 42 ) & '<br />' ); | ||
// call built-in Clojure function, passing raw definition of times2 function: | ||
list = map.call( times2.defn, [ 4, 5, 6 ] ); | ||
writeOutput( '(map times2 [ 4 5 6 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
</cfscript> | ||
<cfset end = getTickCount() /> | ||
<cfoutput>Time taken: #end - start#ms.<br /></cfoutput> | ||
<cfset start = getTickCount() /> | ||
<cfscript> | ||
// or we can get the namespaces and then call methods directly: | ||
// setup my namespaces: | ||
cfml.examples = clj.ns( 'cfml.examples' ); | ||
clojure.core = clj.ns( 'clojure.core' ); | ||
writeOutput( '<h1>Calls via implicit method lookup (lowercase only, no -)</h1>' ); | ||
// call functions: | ||
// simple function call: | ||
writeOutput( '(greet "World") = ' & cfml.examples.greet( 'World' ) & '<br />' ); | ||
// pass CFML array to Clojure and loop over Clojure sequence that comes back: | ||
list = cfml.examples.twice( [ 1, 2, 3 ] ); | ||
writeOutput( '(twice [ 1 2 3 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
// simple function call (times2 is def'd to an anonymous function literal: | ||
writeOutput( '(times2 42) = ' & cfml.examples.times2( 42 ) & '<br />' ); | ||
// call built-in Clojure function, passing raw definition of times2 function: | ||
list = clojure.core.map( cfml.examples.get( 'times2' ).defn, [ 4, 5, 6 ] ); | ||
writeOutput( '(map times2 [ 4 5 6 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
</cfscript> | ||
<cfset end = getTickCount() /> | ||
<cfoutput>Time taken: #end - start#ms.<br /></cfoutput> | ||
<cfset start = getTickCount() /> | ||
<cfscript> | ||
// by configuration and installation: | ||
config = { | ||
project = 'cfml', | ||
files = 'cfml/examples', | ||
ns = 'cfml.examples, clojure.core' | ||
}; | ||
target = { }; // normally you'd target a scope - this is just an example | ||
// install the configuration to the target 'scope': | ||
new cfmljure().install( config, target ); | ||
writeOutput( '<h1>Calls via implicit method lookup after installation to a target scope on every request</h1>' ); | ||
// call functions: | ||
// simple function call: | ||
writeOutput( '(greet "World") = ' & target.cfml.examples.greet( 'World' ) & '<br />' ); | ||
// pass CFML array to Clojure and loop over Clojure sequence that comes back: | ||
list = target.cfml.examples.twice( [ 1, 2, 3 ] ); | ||
writeOutput( '(twice [ 1 2 3 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
// simple function call (times2 is def'd to an anonymous function literal: | ||
writeOutput( '(times2 42) = ' & target.cfml.examples.times2( 42 ) & '<br />' ); | ||
// call built-in Clojure function, passing raw definition of times2 function: | ||
list = target.clojure.core.map( target.cfml.examples.get( 'times2' ).defn, [ 4, 5, 6 ] ); | ||
writeOutput( '(map times2 [ 4 5 6 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
</cfscript> | ||
<cfset end = getTickCount() /> | ||
<cfoutput>Time taken: #end - start#ms.</cfoutput> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,5 @@ | ||
<cfscript> | ||
// load Clojure runtime (for cfml project - search path root is clj/cfml/src/): | ||
clj = new cfmljure( 'cfml' ); | ||
// load scripts (from project source folder - that's clj/cfml/src/cfml/examples.clj): | ||
clj.load( 'cfml/examples' ); | ||
// get handle on individual functions (from namespace cfml.examples): | ||
greet = clj.get( 'cfml.examples.greet' ); | ||
twice = clj.get( 'cfml.examples.twice' ); | ||
times2 = clj.get( 'cfml.examples.times2' ); | ||
// get handle on built-in map function (from namespace clojure.core): | ||
map = clj.get( 'clojure.core.map' ); | ||
// call functions: | ||
// simple function call: | ||
writeOutput( '(greet "World") = ' & greet.call( 'World' ) & '<br />' ); | ||
// pass CFML array to Clojure and loop over Clojure sequence that comes back: | ||
list = twice.call( [ 1, 2, 3 ] ); | ||
writeOutput( '(twice [ 1 2 3 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
// simple function call (times2 is def'd to an anonymous function literal: | ||
writeOutput( '(times2 42) = ' & times2.call( 42 ) & '<br />' ); | ||
// call built-in Clojure function, passing raw definition of times2 function: | ||
list = map.call( times2.defn, [ 4, 5, 6 ] ); | ||
writeOutput( '(map times2 [ 4 5 6 ]) = ' ); | ||
for ( n in list ) writeOutput( n & ' ' ); | ||
writeOutput( '<br />' ); | ||
</cfscript> | ||
<h1>cfmljure examples</h1> | ||
<h2>Basic example</h2> | ||
<p><a href="basic/index.cfm" target="_blank">Three low-level examples of using cfmljure</a></p> | ||
<h2>Advanced example</h2> | ||
<p><a href="advanced/index.cfm" target="_blank">Integrated / automated cfmljure example</a></p> |