Capcode 0.8.6 → 0.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +72 -1
- data/doc/rdoc/classes/Capcode.html +549 -444
- data/doc/rdoc/classes/Capcode/Helpers.html +293 -133
- data/doc/rdoc/classes/Capcode/Helpers/Authorization.html +60 -29
- data/doc/rdoc/created.rid +1 -1
- data/doc/rdoc/files/README_rdoc.html +94 -8
- data/doc/rdoc/files/lib/capcode/configuration_rb.html +101 -0
- data/doc/rdoc/files/lib/capcode/helpers/auth_rb.html +1 -32
- data/doc/rdoc/files/lib/capcode/render/erb_rb.html +1 -1
- data/doc/rdoc/files/lib/capcode/render/haml_rb.html +1 -1
- data/doc/rdoc/files/lib/capcode/render/markaby_rb.html +1 -1
- data/doc/rdoc/files/lib/capcode/render/sass_rb.html +1 -1
- data/doc/rdoc/files/lib/capcode/render/text_rb.html +1 -1
- data/doc/rdoc/files/lib/capcode_rb.html +8 -1
- data/doc/rdoc/fr_file_index.html +1 -0
- data/doc/rdoc/fr_method_index.html +19 -11
- data/examples/blog-couchdb.rb +4 -3
- data/examples/erb/cf.rhtml +1 -0
- data/examples/haml/cf.haml +4 -1
- data/examples/render-erb.rb +4 -1
- data/examples/render-haml_sass.rb +6 -2
- data/examples/render-image.rb +70 -0
- data/examples/render-static.rb +4 -1
- data/examples/render-static.ru +0 -2
- data/examples/render-use.rb +31 -0
- data/examples/static/coderay.css +131 -0
- data/examples/static/index.html +19 -0
- data/lib/capcode.rb +125 -76
- data/lib/capcode/configuration.rb +39 -0
- data/lib/capcode/helpers/auth.rb +22 -23
- data/lib/capcode/render/erb.rb +25 -18
- data/lib/capcode/render/haml.rb +28 -19
- data/lib/capcode/render/markaby.rb +2 -1
- data/lib/capcode/render/sass.rb +19 -13
- data/lib/capcode/render/text.rb +1 -1
- data/lib/capcode/version.rb +1 -1
- metadata +8 -2
data/lib/capcode.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# Please read the README.rdoc file !
|
2
|
+
|
1
3
|
require 'rubygems'
|
2
4
|
require 'rack'
|
3
5
|
require 'json' ## DELETE THIS IN 1.0.0
|
@@ -9,11 +11,12 @@ require 'capcode/version'
|
|
9
11
|
require 'capcode/core_ext'
|
10
12
|
require 'capcode/helpers/auth'
|
11
13
|
require 'capcode/render/text'
|
14
|
+
require 'capcode/configuration'
|
12
15
|
|
13
16
|
module Capcode
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
#@@__ROUTES = {}
|
18
|
+
#@@__STATIC_DIR = nil
|
19
|
+
#@@__APP = nil
|
17
20
|
|
18
21
|
# @@__FILTERS = []
|
19
22
|
# def self.before_filter( opts, &b )
|
@@ -36,7 +39,13 @@ module Capcode
|
|
36
39
|
|
37
40
|
# Helpers contains methods available in your controllers
|
38
41
|
module Helpers
|
39
|
-
|
42
|
+
#@@__ARGS__ = nil
|
43
|
+
def self.args
|
44
|
+
@args ||= nil
|
45
|
+
end
|
46
|
+
def self.args=(x)
|
47
|
+
@args = x
|
48
|
+
end
|
40
49
|
|
41
50
|
# Render a view
|
42
51
|
#
|
@@ -58,6 +67,10 @@ module Capcode
|
|
58
67
|
# If you want to use a specific layout, you can specify it with option
|
59
68
|
# :layout
|
60
69
|
#
|
70
|
+
# If you want to change the Content-Type, you can specify it with option
|
71
|
+
# :content_type
|
72
|
+
# Note that this will not work with the JSON renderer
|
73
|
+
#
|
61
74
|
# If you use the WebDav renderer, you can use the option
|
62
75
|
# :resource_class (see http://github.com/georgi/rack_dav for more informations)
|
63
76
|
def render( hash )
|
@@ -82,6 +95,10 @@ module Capcode
|
|
82
95
|
end
|
83
96
|
|
84
97
|
render_name = hash.delete(render_type)
|
98
|
+
content_type = hash.delete(:content_type)
|
99
|
+
unless content_type.nil?
|
100
|
+
@response['Content-Type'] = content_type
|
101
|
+
end
|
85
102
|
|
86
103
|
begin
|
87
104
|
self.send( "render_#{render_type.to_s}", render_name, hash )
|
@@ -105,7 +122,7 @@ module Capcode
|
|
105
122
|
#
|
106
123
|
# <b>DEPRECATED</b>, please use <tt>render( :json => o )</tt>
|
107
124
|
def json( d ) ## DELETE THIS IN 1.0.0
|
108
|
-
warn( "json is deprecated, please use `render( :json => ... )'" )
|
125
|
+
warn( "json is deprecated and will be removed in version 1.0, please use `render( :json => ... )'" )
|
109
126
|
@response['Content-Type'] = 'application/json'
|
110
127
|
d.to_json
|
111
128
|
end
|
@@ -209,7 +226,8 @@ module Capcode
|
|
209
226
|
# end
|
210
227
|
# end
|
211
228
|
def content_for( x )
|
212
|
-
if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
|
229
|
+
#if @@__ARGS__.map{|_| _.to_s }.include?(x.to_s)
|
230
|
+
if Capcode::Helpers.args.map{|_| _.to_s }.include?(x.to_s)
|
213
231
|
yield
|
214
232
|
end
|
215
233
|
end
|
@@ -262,7 +280,7 @@ module Capcode
|
|
262
280
|
end
|
263
281
|
|
264
282
|
class << self
|
265
|
-
attr :__auth__, true
|
283
|
+
attr :__auth__, true #:nodoc:
|
266
284
|
|
267
285
|
# Add routes to a controller class
|
268
286
|
#
|
@@ -382,47 +400,47 @@ module Capcode
|
|
382
400
|
}
|
383
401
|
end
|
384
402
|
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
finalArgs = nil
|
389
|
-
finalNArgs = nil
|
390
|
-
|
391
|
-
aPath = @request.path.gsub( /^\//, "" ).split( "/" )
|
392
|
-
self.class.__urls__[0].each do |p, r|
|
393
|
-
xPath = p.gsub( /^\//, "" ).split( "/" )
|
394
|
-
if (xPath - aPath).size == 0
|
395
|
-
diffArgs = aPath - xPath
|
396
|
-
diffNArgs = diffArgs.size
|
397
|
-
if finalNArgs.nil? or finalNArgs > diffNArgs
|
398
|
-
finalPath = p
|
399
|
-
finalNArgs = diffNArgs
|
400
|
-
finalArgs = diffArgs
|
401
|
-
end
|
402
|
-
end
|
403
|
-
|
404
|
-
end
|
403
|
+
finalPath = nil
|
404
|
+
finalArgs = nil
|
405
|
+
finalNArgs = nil
|
405
406
|
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
407
|
+
aPath = @request.path.gsub( /^\//, "" ).split( "/" )
|
408
|
+
self.class.__urls__[0].each do |p, r|
|
409
|
+
xPath = p.gsub( /^\//, "" ).split( "/" )
|
410
|
+
if (xPath - aPath).size == 0
|
411
|
+
diffArgs = aPath - xPath
|
412
|
+
diffNArgs = diffArgs.size
|
413
|
+
if finalNArgs.nil? or finalNArgs > diffNArgs
|
414
|
+
finalPath = p
|
415
|
+
finalNArgs = diffNArgs
|
416
|
+
finalArgs = diffArgs
|
413
417
|
end
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
418
|
+
end
|
419
|
+
|
420
|
+
end
|
421
|
+
|
422
|
+
nargs = self.class.__urls__[1]
|
423
|
+
regexp = Regexp.new( self.class.__urls__[0][finalPath] )
|
424
|
+
args = regexp.match( Rack::Utils.unescape(@request.path).gsub( Regexp.new( "^#{finalPath}" ), "" ).gsub( /^\//, "" ) )
|
425
|
+
if args.nil?
|
426
|
+
raise Capcode::ParameterError, "Path info `#{@request.path_info}' does not match route regexp `#{regexp.source}'"
|
427
|
+
else
|
428
|
+
args = args.captures.map { |x| (x.size == 0)?nil:x }
|
429
|
+
end
|
430
|
+
|
431
|
+
while args.size < nargs
|
432
|
+
args << nil
|
433
|
+
end
|
434
|
+
|
435
|
+
case @env["REQUEST_METHOD"]
|
436
|
+
when "GET"
|
419
437
|
get( *args )
|
420
438
|
when "POST"
|
421
439
|
_method = params.delete( "_method" ) { |_| "post" }
|
422
|
-
send( _method.downcase.to_sym )
|
440
|
+
send( _method.downcase.to_sym, *args )
|
423
441
|
else
|
424
442
|
_method = @env["REQUEST_METHOD"]
|
425
|
-
send( _method.downcase.to_sym )
|
443
|
+
send( _method.downcase.to_sym, *args )
|
426
444
|
end
|
427
445
|
}
|
428
446
|
if r.respond_to?(:to_ary)
|
@@ -450,7 +468,25 @@ module Capcode
|
|
450
468
|
# Rack::File.new( "." )
|
451
469
|
# end
|
452
470
|
def map( route, &b )
|
453
|
-
|
471
|
+
#@@__ROUTES[route] = yield
|
472
|
+
Capcode.routes[route] = yield
|
473
|
+
end
|
474
|
+
|
475
|
+
# This method allow you to use a Rack middleware
|
476
|
+
#
|
477
|
+
# Example :
|
478
|
+
#
|
479
|
+
# module Capcode
|
480
|
+
# ...
|
481
|
+
# use Rack::Codehighlighter, :coderay, :element => "pre",
|
482
|
+
# :pattern => /\A:::(\w+)\s*\n/, :logging => false
|
483
|
+
# ...
|
484
|
+
# end
|
485
|
+
def use(middleware, *args, &block)
|
486
|
+
middlewares << [middleware, args, block]
|
487
|
+
end
|
488
|
+
def middlewares #:nodoc:
|
489
|
+
@middlewares ||= []
|
454
490
|
end
|
455
491
|
|
456
492
|
# Allow you to add and HTTP Authentication (Basic or Digest) to controllers for or specific route
|
@@ -490,25 +526,26 @@ module Capcode
|
|
490
526
|
|
491
527
|
def configuration( args = {} ) #:nodoc:
|
492
528
|
{
|
493
|
-
:port => args[:port]||3000,
|
494
|
-
:host => args[:host]||"0.0.0.0",
|
495
|
-
:server => args[:server]||nil,
|
496
|
-
:log => args[:log]||$stdout,
|
497
|
-
:session => args[:session]||{},
|
498
|
-
:pid => args[:pid]||"#{$0}.pid",
|
499
|
-
:daemonize => args[:daemonize]||false,
|
500
|
-
:db_config => File.expand_path(args[:db_config]||"database.yml"),
|
501
|
-
:
|
502
|
-
:
|
503
|
-
:
|
504
|
-
:verbose => args[:verbose]||false,
|
529
|
+
:port => args[:port]||Capcode.get(:port)||3000,
|
530
|
+
:host => args[:host]||Capcode.get(:host)||"0.0.0.0",
|
531
|
+
:server => args[:server]||Capcode.get(:server)||nil,
|
532
|
+
:log => args[:log]||Capcode.get(:log)||$stdout,
|
533
|
+
:session => args[:session]||Capcode.get(:session)||{},
|
534
|
+
:pid => args[:pid]||Capcode.get(:pid)||"#{$0}.pid",
|
535
|
+
:daemonize => args[:daemonize]||Capcode.get(:daemonize)||false,
|
536
|
+
:db_config => File.expand_path(args[:db_config]||Capcode.get(:db_config)||"database.yml"),
|
537
|
+
:root => args[:root]||Capcode.get(:root)||File.expand_path(File.dirname($0)),
|
538
|
+
:static => args[:static]||Capcode.get(:static)||args[:root]||File.expand_path(File.dirname($0)),
|
539
|
+
:verbose => args[:verbose]||Capcode.get(:verbose)||false,
|
505
540
|
:console => false
|
506
541
|
}
|
507
542
|
end
|
508
543
|
|
509
544
|
# Return the Rack App.
|
510
545
|
#
|
511
|
-
# Options :
|
546
|
+
# Options : see Capcode.set
|
547
|
+
#
|
548
|
+
# Options set here replace the ones set globally
|
512
549
|
def application( args = {} )
|
513
550
|
conf = configuration(args)
|
514
551
|
|
@@ -517,8 +554,10 @@ module Capcode
|
|
517
554
|
if eval "Capcode::#{k}.public_methods(true).include?( '__urls__' )"
|
518
555
|
hash_of_routes, max_captures_for_routes, klass = eval "Capcode::#{k}.__urls__"
|
519
556
|
hash_of_routes.keys.each do |current_route_path|
|
520
|
-
raise Capcode::RouteError, "Route `#{current_route_path}' already define !", caller if @@__ROUTES.keys.include?(current_route_path)
|
521
|
-
|
557
|
+
#raise Capcode::RouteError, "Route `#{current_route_path}' already define !", caller if @@__ROUTES.keys.include?(current_route_path)
|
558
|
+
raise Capcode::RouteError, "Route `#{current_route_path}' already define !", caller if Capcode.routes.keys.include?(current_route_path)
|
559
|
+
#@@__ROUTES[current_route_path] = klass.new
|
560
|
+
Capcode.routes[current_route_path] = klass.new
|
522
561
|
end
|
523
562
|
end
|
524
563
|
rescue => e
|
@@ -527,15 +566,18 @@ module Capcode
|
|
527
566
|
end
|
528
567
|
|
529
568
|
# Set Static directory
|
530
|
-
|
569
|
+
#@@__STATIC_DIR = (conf[:static][0].chr == "/")?conf[:static]:"/"+conf[:static] unless conf[:static].nil?
|
570
|
+
Capcode.static = (conf[:static][0].chr == "/")?conf[:static]:"/"+conf[:static] unless conf[:static].nil?
|
531
571
|
|
532
572
|
# Initialize Rack App
|
533
573
|
puts "** Map routes." if conf[:verbose]
|
534
|
-
app = Rack::URLMap.new(@@__ROUTES)
|
574
|
+
#app = Rack::URLMap.new(@@__ROUTES)
|
575
|
+
app = Rack::URLMap.new(Capcode.routes)
|
535
576
|
puts "** Initialize static directory (#{conf[:static]})" if conf[:verbose]
|
536
577
|
app = Rack::Static.new(
|
537
578
|
app,
|
538
|
-
|
579
|
+
#:urls => [@@__STATIC_DIR],
|
580
|
+
:urls => [Capcode.static],
|
539
581
|
:root => File.expand_path(conf[:root])
|
540
582
|
) unless conf[:static].nil?
|
541
583
|
puts "** Initialize session" if conf[:verbose]
|
@@ -547,6 +589,16 @@ module Capcode
|
|
547
589
|
#app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!!
|
548
590
|
# app = Rack::CommonLogger.new( app, Logger.new(conf[:log]) )
|
549
591
|
|
592
|
+
middlewares.each do |mw|
|
593
|
+
middleware, args, block = mw
|
594
|
+
puts "** Load middleware #{middleware}" if conf[:verbose]
|
595
|
+
if block
|
596
|
+
app = middleware.new( app, *args, &block )
|
597
|
+
else
|
598
|
+
app = middleware.new( app, *args )
|
599
|
+
end
|
600
|
+
end
|
601
|
+
|
550
602
|
# Start database
|
551
603
|
if self.methods.include? "db_connect"
|
552
604
|
db_connect( conf[:db_config], conf[:log] )
|
@@ -561,19 +613,9 @@ module Capcode
|
|
561
613
|
|
562
614
|
# Start your application.
|
563
615
|
#
|
564
|
-
# Options :
|
565
|
-
#
|
566
|
-
#
|
567
|
-
# * <tt>:server</tt> = Server type (webrick or mongrel)
|
568
|
-
# * <tt>:log</tt> = Output logfile (default: STDOUT)
|
569
|
-
# * <tt>:session</tt> = Session parameters. See Rack::Session for more informations
|
570
|
-
# * <tt>:pid</tt> = PID file (default: $0.pid)
|
571
|
-
# * <tt>:daemonize</tt> = Daemonize application (default: false)
|
572
|
-
# * <tt>:db_config</tt> = database configuration file (default: database.yml)
|
573
|
-
# * <tt>:static</tt> = Static directory (default: none -- relative to the working directory)
|
574
|
-
# * <tt>:root</tt> = Root directory (default: directory of the main.rb) -- This is also the working directory !
|
575
|
-
# * <tt>:verbose</tt> = run in verbose mode
|
576
|
-
# * <tt>:auth</tt> = HTTP Basic Authentication options
|
616
|
+
# Options : see Capcode.set
|
617
|
+
#
|
618
|
+
# Options set here replace the ones set globally
|
577
619
|
def run( args = {} )
|
578
620
|
conf = configuration(args)
|
579
621
|
|
@@ -695,11 +737,18 @@ module Capcode
|
|
695
737
|
end
|
696
738
|
|
697
739
|
def routes #:nodoc:
|
698
|
-
|
740
|
+
#@@__ROUTES
|
741
|
+
@routes ||= {}
|
699
742
|
end
|
700
743
|
|
701
744
|
def static #:nodoc:
|
702
|
-
|
703
|
-
|
745
|
+
#@@__STATIC_DIR
|
746
|
+
@static_dir ||= nil
|
747
|
+
end
|
748
|
+
def static=(x) #:nodoc:
|
749
|
+
#@@__STATIC_DIR
|
750
|
+
@static_dir = x
|
751
|
+
end
|
752
|
+
|
704
753
|
end
|
705
754
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Capcode
|
2
|
+
class << self
|
3
|
+
# Set global configuration options
|
4
|
+
#
|
5
|
+
# Options :
|
6
|
+
# * <tt>:port</tt> = Listen port (default: 3000)
|
7
|
+
# * <tt>:host</tt> = Listen host (default: 0.0.0.0)
|
8
|
+
# * <tt>:server</tt> = Server type (webrick or mongrel)
|
9
|
+
# * <tt>:log</tt> = Output logfile (default: STDOUT)
|
10
|
+
# * <tt>:session</tt> = Session parameters. See Rack::Session for more informations
|
11
|
+
# * <tt>:pid</tt> = PID file (default: $0.pid)
|
12
|
+
# * <tt>:daemonize</tt> = Daemonize application (default: false)
|
13
|
+
# * <tt>:db_config</tt> = database configuration file (default: database.yml)
|
14
|
+
# * <tt>:static</tt> = Static directory (default: the working directory)
|
15
|
+
# * <tt>:root</tt> = Root directory (default: directory of the main.rb) -- This is also the working directory !
|
16
|
+
# * <tt>:verbose</tt> = run in verbose mode
|
17
|
+
# * <tt>:auth</tt> = HTTP Basic Authentication options
|
18
|
+
#
|
19
|
+
# It can exist specifics options depending on a renderer, a helper, ...
|
20
|
+
#
|
21
|
+
# Example :
|
22
|
+
#
|
23
|
+
# module Capcode
|
24
|
+
# set :erb, "/path/to/erb/files"
|
25
|
+
# ...
|
26
|
+
# end
|
27
|
+
def set( key, value )
|
28
|
+
config[key] = value
|
29
|
+
end
|
30
|
+
|
31
|
+
def get( key ) #:nodoc:
|
32
|
+
config[key] || nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def config
|
36
|
+
@configuration ||= {}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/capcode/helpers/auth.rb
CHANGED
@@ -1,28 +1,27 @@
|
|
1
|
-
# Because this helper was trully inspired by this post :
|
2
|
-
# http://www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/
|
3
|
-
# and because the code in this post was extracted out of Wink, this file follow the
|
4
|
-
# Wink license :
|
5
|
-
#
|
6
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
-
# of this software and associated documentation files (the "Software"), to deal
|
8
|
-
# in the Software without restriction, including without limitation the rights
|
9
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
-
# copies of the Software, and to permit persons to whom the Software is
|
11
|
-
# furnished to do so, subject to the following conditions:
|
12
|
-
#
|
13
|
-
# The above copyright notice and this permission notice shall be included in
|
14
|
-
# all copies or substantial portions of the Software.
|
15
|
-
#
|
16
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
-
# THE SOFTWARE.
|
23
|
-
|
24
1
|
module Capcode
|
25
2
|
module Helpers
|
3
|
+
# Because this helper was trully inspired by this post :
|
4
|
+
# http://www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/
|
5
|
+
# and because the code in this post was extracted out of Wink, this file follow the
|
6
|
+
# Wink license :
|
7
|
+
#
|
8
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
# of this software and associated documentation files (the "Software"), to deal
|
10
|
+
# in the Software without restriction, including without limitation the rights
|
11
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
# copies of the Software, and to permit persons to whom the Software is
|
13
|
+
# furnished to do so, subject to the following conditions:
|
14
|
+
#
|
15
|
+
# The above copyright notice and this permission notice shall be included in
|
16
|
+
# all copies or substantial portions of the Software.
|
17
|
+
#
|
18
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24
|
+
# THE SOFTWARE.
|
26
25
|
module Authorization
|
27
26
|
def auth #:nodoc:
|
28
27
|
if @auth_type == :basic
|
data/lib/capcode/render/erb.rb
CHANGED
@@ -2,11 +2,11 @@ require 'erb'
|
|
2
2
|
|
3
3
|
module Capcode
|
4
4
|
module Helpers
|
5
|
-
@@__ERB_PATH__ = "."
|
6
|
-
|
7
5
|
# Set the path to ERB files. If this path is not set, Capcode will search in the static path.
|
8
|
-
|
9
|
-
|
6
|
+
# This method is deprecated and will be removed in version 1.0
|
7
|
+
def self.erb_path=( p )
|
8
|
+
warn "Capcode::Helpers.erb_path is deprecated and will be removed in version 1.0, please use `set :erb'"
|
9
|
+
Capcode.set :erb, p
|
10
10
|
end
|
11
11
|
|
12
12
|
def get_binding #:nodoc:
|
@@ -14,8 +14,8 @@ module Capcode
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def render_erb( f, opts ) #:nodoc:
|
17
|
-
if
|
18
|
-
|
17
|
+
if @erb_path.nil?
|
18
|
+
@erb_path = Capcode.get( :erb ) || Capcode.static()
|
19
19
|
end
|
20
20
|
|
21
21
|
f = f.to_s
|
@@ -24,28 +24,35 @@ module Capcode
|
|
24
24
|
end
|
25
25
|
|
26
26
|
if /Windows/.match( ENV['OS'] )
|
27
|
-
unless( /.:\\/.match(
|
28
|
-
|
27
|
+
unless( /.:\\/.match( @erb_path[0] ) )
|
28
|
+
@erb_path = File.expand_path( File.join(".", @erb_path) )
|
29
29
|
end
|
30
30
|
else
|
31
|
-
unless(
|
32
|
-
|
31
|
+
unless( @erb_path[0].chr == "/" )
|
32
|
+
@erb_path = File.expand_path( File.join(".", @erb_path) )
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
# Get Layout
|
36
37
|
layout = opts.delete(:layout)||:layout
|
37
|
-
layout_file = File.join(
|
38
|
+
layout_file = File.join( @erb_path, layout.to_s+".rhtml" )
|
38
39
|
|
40
|
+
# Get file
|
39
41
|
f = f + ".rhtml" if File.extname( f ) != ".rhtml"
|
40
|
-
file = File.join(
|
42
|
+
file = File.join( @erb_path, f )
|
41
43
|
|
42
|
-
if( File.exist?(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
if( File.exist?( file ) )
|
45
|
+
if( File.exist?( layout_file ) )
|
46
|
+
ERB.new(open(layout_file).read).result( get_binding { |*args|
|
47
|
+
#@@__ARGS__ = args
|
48
|
+
Capcode::Helpers.args = args
|
49
|
+
ERB.new(open(file).read).result(binding)
|
50
|
+
} )
|
51
|
+
else
|
52
|
+
ERB.new(open(file).read).result(binding)
|
53
|
+
end
|
47
54
|
else
|
48
|
-
|
55
|
+
raise Capcode::RenderError, "Error rendering `erb', #{file} does not exist !"
|
49
56
|
end
|
50
57
|
end
|
51
58
|
end
|