Permalink
Browse files

[ @ ] Feature: ContentGenerator

  • Loading branch information...
1 parent 2d8bb53 commit 3b8b09c6dc1255975dee23bc2b068ddddb689c60 @pnegri committed Nov 11, 2011
Showing with 77 additions and 77 deletions.
  1. +31 −0 lib/_monkey-patching.coffee
  2. +45 −76 lib/arcabouco.coffee
  3. +1 −1 package.json
@@ -1,10 +1,41 @@
require 'coffee-script'
Common = require __dirname + '/common'
require __dirname + '/_monkey-fs'
+Underscore = require 'underscore'
String.prototype.replaceLast = ( search, replace ) ->
string = this
n = string.lastIndexOf( search )
if n >= 0 && n + replace.length >= string.length
string = string.substring(0, n) + replace
string
+
+Common.Http.ServerResponse.prototype.group = 'default'
+
+Common.Http.ServerResponse.prototype.getGroup = () ->
+ @group
+
+Common.Http.ServerResponse.prototype.setGroup = ( groupName ) ->
+ @group = groupName
+
+Common.Http.ServerResponse.prototype.redirectTo = ( url ) ->
+ @writeHead 302, { 'Location': url }
+ @end()
+
+Common.Http.ServerResponse.prototype.respondWith = ( content, type='text/html', status=200, expiration=0 ) ->
+ expirationTime = new Date()
+ headers =
+ 'Content-Type': type + '; charset=utf-8'
+ if expiration > 0
+ expirationTime.setTime expirationTime.getTime() + expiration
+ Underscore.extend headers,
+ 'Cache-Control' : 'max-age=' + expiration + ', public, most-revalidate'
+ 'Expires' : expirationTime.toGMTString()
+ else
+ Underscore.extend headers,
+ 'Cache-Control' : 'max-age=0, no-cache, no-store, must-revalidate'
+ 'Pragma' : 'no-cache'
+ 'Expires' : 'Thu, 01 Jan 1970 00:00:00 GMT'
+ @writeHead status, headers
+ @write content
+ @end()
View
@@ -2,89 +2,53 @@ Common = require __dirname + '/common'
Underscore = require 'underscore'
require __dirname + '/_monkey-patching'
-Haml = require 'haml'
-#Common.Http.ServerResponse.prototype.testing = 'BlaBleBli'
-
-Common.Http.ServerResponse.prototype.redirectTo = ( url ) ->
- @writeHead 302, { 'Location': url }
- @end()
-
-Common.Http.ServerResponse.prototype.respondWith = ( content, type='text/html', status=200, expiration=0 ) ->
- expirationTime = new Date()
- headers =
- 'Content-Type': type + '; charset=utf-8'
- if expiration > 0
- expirationTime.setTime expirationTime.getTime() + expiration
- Underscore.extend headers,
- 'Cache-Control' : 'max-age=' + expiration + ', public, most-revalidate'
- 'Expires' : expirationTime.toGMTString()
- else
- Underscore.extend headers,
- 'Cache-Control' : 'max-age=0, no-cache, no-store, must-revalidate'
- 'Pragma' : 'no-cache'
- 'Expires' : 'Thu, 01 Jan 1970 00:00:00 GMT'
- @writeHead status, headers
- @write content
- @end()
-
-## START WORKING ON THIS
-Template =
- loadedTemplates : []
-
- ## TODO: Search for Template
- ## TODO: Modulize template for support multiple files
-
- getTemplate: ( templateFile ) ->
- templateFile = Common.Path.basename templateFile
- unless @loadedTemplates[ templateFile ]
- return false
- @loadedTemplates[ templateFile ]
-
- loadTemplate: ( templateFile, asTemplateName="" ) ->
- unless templateFile
- return false
- unless templateFile.match /\.haml$/gi
- return false
- baseTemplateFile = Common.Path.basename templateFile
- if asTemplateName != ""
- baseTemplateFile = asTemplateName
- template = Common.Fs.readFileSync templateFile, 'utf-8'
- compiledTemplate = Haml.compile template
- optimizedTemplate = Haml.optimize compiledTemplate
- @loadedTemplates[ baseTemplateFile ] =
- type: 'haml'
- data: optimizedTemplate
-
- loadTemplateString: ( templateString, templateName ) ->
- @loadedTemplates[ templateName ] =
- type: 'plain'
- data: templateString
-
- doRender: ( templateFile, context = this, params = {}, layout = 'layout.haml' ) ->
- template = @getTemplate templateFile
- unless template
- return 'Template Missing: ' + templateFile
-
- if template.type == 'haml'
- content = Haml.execute template.data, context, params
- else
- content = template.data
+Template = require __dirname + '/template'
- if layout
- compiled_layout = @getTemplate layout
- params.content = content
- return Haml.execute layout, context, params
- return content
+class ContentGenerator
+ contentArray : []
- doRenderPartial: ( templateFile, context = this, params = {}) ->
- @doRender templateFile, context, params, false
+ ensureArray: ( where, group ) ->
+ @contentArray[ group ] = [] unless @contentArray[ group ]
+ @contentArray[ group ][ where ] = [] unless @contentArray[ group ][ where ]
+ true
-## Register Modules???
+ addContentFor: ( where, data, options = { group: 'default' } ) ->
+ priority = 0
+ priority = options.priority if options.priority
+ group = options.group
+
+ @ensureArray( where, group )
+
+ type = 'plain'
+ type = 'function' if typeof data == 'function'
+ @contentArray[ group ][ where ].push
+ 'type': type
+ data: data
+ priority: priority
+
+ getContentFor: ( where, options = { group: 'default' } ) ->
+ group = options.group
+
+ @ensureArray( where, group )
+
+ content = ''
+ priorityArray = Underscore.sortBy @contentArray[ group ][ where ],
+ ( obj ) ->
+ return obj.priority
+ for aContent in priorityArray
+ output = ''
+ if aContent.type == 'plain'
+ output = aContent.data
+ else
+ output = aContent.data()
+ content += output
+ content
class Arcabouco
config : {}
- Template : Template
+ Template : false
+ ContentGenerator : false
newRoutingAvaiable : false
controllerInstances : []
@@ -109,6 +73,11 @@ class Arcabouco
global.objects = []
+ @Template = new Template()
+ @ContentGenerator = new ContentGenerator()
+
+ #@content_for = @ContentGenerator.getContentFor
+
@Template.loadTemplate Common.Path.normalize(__dirname + '/../views/404.haml'), '404'
@Template.loadTemplate Common.Path.normalize(__dirname + '/../views/500.haml'), '500'
View
@@ -1,7 +1,7 @@
{
"name" : "arcabouco-js",
"description" : "scalable microframework in node.js",
- "version" : "0.6.1",
+ "version" : "0.6.2",
"author" : "Patrick Negri <patrick@iugu.com.br>",
"homepage": "http://github.com/pnegri/arcabouco-js",
"contributors" : [],

0 comments on commit 3b8b09c

Please sign in to comment.