Changeset 245
- Timestamp:
- 05/30/07 20:58:53 (2 years ago)
- Files:
-
- trunk/Rakefile (modified) (1 diff)
- trunk/examples/skeleton.tar (modified) (previous)
- trunk/lib/merb.rb (modified) (1 diff)
- trunk/lib/merb/merb_controller.rb (modified) (2 diffs)
- trunk/lib/merb/merb_mailer.rb (modified) (2 diffs)
- trunk/lib/merb/merb_server.rb (modified) (1 diff)
- trunk/lib/merb/merb_view_context.rb (modified) (3 diffs)
- trunk/lib/merb/mixins/render_mixin.rb (modified) (7 diffs)
- trunk/lib/merb/template/haml.rb (modified) (3 diffs)
- trunk/lib/merb/template/markaby.rb (modified) (1 diff)
- trunk/specs/fixtures/views (added)
- trunk/specs/fixtures/views/erubis.herb (added)
- trunk/specs/fixtures/views/haml.haml (added)
- trunk/specs/fixtures/views/markaby.mab (added)
- trunk/specs/fixtures/views/partials (added)
- trunk/specs/fixtures/views/partials/_erubis.herb (added)
- trunk/specs/fixtures/views/partials/_haml.haml (added)
- trunk/specs/fixtures/views/partials/_markaby.mab (added)
- trunk/specs/merb/merb_render_spec.rb (added)
- trunk/specs/merb/merb_rendering_spec.rb (deleted)
- trunk/tools/code_statistics.rb (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Rakefile
r241 r245 154 154 end 155 155 156 STATS_DIRECTORIES = [ 157 %w(Code liib/), 158 %w(Unit\ tests specs), 159 ].collect { |name, dir| [ name, "./#{dir}" ] }.select { |name, dir| File.directory?(dir) } 160 161 desc "Report code statistics (KLOCs, etc) from the application" 162 task :stats do 163 require __DIR__ + '/tools/code_statistics' 164 #require 'extra/stats' 165 verbose = true 166 CodeStatistics.new(*STATS_DIRECTORIES).to_s 167 end 168 156 169 ############################################################################## 157 170 # SVN trunk/lib/merb.rb
r242 r245 75 75 76 76 module Mongrel::Const 77 HTTP_COOKIE = 'HTTP_COOKIE'.freeze 78 QUERY_STRING = 'QUERY_STRING'.freeze 79 APPLICATION_JSON = 'application/json'.freeze 80 TEXT_JSON = 'text/x-json'.freeze 77 HTTP_COOKIE = 'HTTP_COOKIE'.freeze 78 QUERY_STRING = 'QUERY_STRING'.freeze 79 APPLICATION_JSON = 'application/json'.freeze 80 TEXT_JSON = 'text/x-json'.freeze 81 APPLICATION_XML = 'application/xml'.freeze 82 TEXT_XML = 'text/xml'.freeze 81 83 UPCASE_CONTENT_TYPE = 'CONTENT_TYPE'.freeze 82 84 end trunk/lib/merb/merb_controller.rb
r242 r245 52 52 json = MerbHash.new(json) if json.is_a? Hash 53 53 querystring.update(json) 54 elsif [ 'application/xml'].include?(@env[Mongrel::Const::UPCASE_CONTENT_TYPE])55 querystring.update(Hash.from_xml(request.read) )54 elsif [Mongrel::Const::APPLICATION_XML, Mongrel::Const::TEXT_XML].include?(@env[Mongrel::Const::UPCASE_CONTENT_TYPE]) 55 querystring.update(Hash.from_xml(request.read).with_indifferent_access) 56 56 else 57 57 querystring.update(query_parse(request.read)) … … 181 181 ok = true 182 182 end 183 case filter 184 when Symbol, String 185 send(filter) if ok 186 when Proc 187 filter.call(self) if ok 188 end 183 if ok 184 case filter 185 when Symbol, String 186 send(filter) 187 when Proc 188 filter.call(self) 189 end 190 end 189 191 end 190 192 return :filter_chain_completed trunk/lib/merb/merb_mailer.rb
r224 r245 46 46 end 47 47 =begin 48 m = Merb::Mailer.new :to => 'foo@bar.com', 49 :from => 'bar@foo.com', 50 :subject => 'Welcome to whatever!', 51 :body => partial(:sometemplate) 52 m.deliver! 48 53 49 54 50 Merb::Mailer.config = { … … 59 55 :auth=>:plain # :plain, :login, or :cram_md5, default :plain 60 56 } 57 m = Merb::Mailer.new :to => 'foo@bar.com', 58 :from => 'bar@foo.com', 59 :subject => 'Welcome to whatever!', 60 :body => partial(:sometemplate) 61 m.deliver! 62 61 63 =end trunk/lib/merb/merb_server.rb
r241 r245 196 196 if @@merb_opts[:console] 197 197 initialize_merb 198 Object.class_eval do 199 def show_routes 200 Merb::Router.generator.paths.each {|p| puts p.inspect} 201 nil 202 end 203 def url(path, o={}) 204 Merb::Router.generator.generate(path,o) 205 end 206 end 198 207 ARGV.clear # Avoid passing args to IRB 199 208 require 'irb' trunk/lib/merb/merb_view_context.rb
r236 r245 33 33 34 34 def initialize(controller) 35 @_merb_partial_locals = {} 35 36 @controller = controller 36 37 (@controller.instance_variables - PROTECTED_IVARS).each do |ivar| … … 57 58 58 59 def respond_to?(sym, include_private=false) 59 old_respond_to?(sym, include_private) || @controller.respond_to?(sym, include_private) 60 old_respond_to?(sym, include_private) || @controller.respond_to?(sym, include_private) || @_merb_partial_locals.key?(sym) 60 61 end 61 62 … … 65 66 if @controller.respond_to? sym 66 67 @controller.send(sym, *args, &blk) 68 elsif @_merb_partial_locals.key? sym 69 @_merb_partial_locals[sym] 67 70 else 68 71 super trunk/lib/merb/mixins/render_mixin.rb
r242 r245 61 61 template = find_partial(partial, opts) 62 62 opts[:layout] = :none 63 64 # Add an instance variable that can be used to create the locals in the 65 # partial 66 if opts[:locals] 67 @_merb_partial_locals = opts[:locals] 68 end 69 opts[:clean_context] = true 63 70 when js = opts[:js] 64 71 headers['Content-Type'] = "text/javascript" … … 77 84 when template = opts[:template] 78 85 template = find_template(:template => template) 79 else 86 else 80 87 template = find_template(:action => action) 81 88 end … … 84 91 options = { 85 92 :file => template, 86 :view_context => (opts[:clean_context] ? clean_view_context : _view_context),93 :view_context => (opts[:clean_context] ? clean_view_context : cached_view_context), 87 94 :opts => opts 88 95 } … … 98 105 # the instance variables in your controller. This is used 99 106 # as the view context object for the Erubis templates. 100 def _view_context107 def cached_view_context 101 108 @_view_context_cache ||= ViewContext.new(self) 102 109 end … … 135 142 # if you create a views/shared directory then you can call 136 143 # partials that live there like partial('shared/foo') 137 def partial(template )138 render :partial => template 144 def partial(template, locals={}) 145 render :partial => template, :locals => locals 139 146 end 140 147 … … 152 159 end 153 160 154 _view_context.instance_variable_set('@_layout_content', content)161 cached_view_context.instance_variable_set('@_layout_content', content) 155 162 engine = engine_for(layout_choice) 156 163 options = { 157 164 :file => layout_choice, 158 :view_context => _view_context,165 :view_context => cached_view_context, 159 166 :opts => opts 160 167 } … … 165 172 def find_template(opts={}) 166 173 if template = opts[:template] 167 path = MERB_VIEW_ROOT/ template174 path = _template_root / template 168 175 elsif action = opts[:action] 169 path = 170 File.expand_path(MERB_VIEW_ROOT / self.class.name.snake_case / action) 176 path = _template_root / self.class.name.snake_case / action 171 177 elsif _layout = opts[:layout] 172 178 path = _layout_root / _layout 173 179 else 174 180 raise "called find_template without an :action or :layout" 175 end 181 end 176 182 extensions = [_template_extensions.keys].flatten.uniq 177 183 glob = "#{path}.{#{opts[:ext] || extensions.join(',')}}" trunk/lib/merb/template/haml.rb
r225 r245 28 28 class << self 29 29 30 class_inheritable_accessor :haml_options31 self.haml_options = { :locals => {} }32 33 30 @@hamls ||= {} 34 31 @@mtimes ||= {} … … 40 37 def transform(options = {}) 41 38 opts, file, view_context = options.values_at(:opts, :file, :view_context) 42 opts = haml_options.merge(opts)39 43 40 begin 44 if precompiled = get_precompiled(file) 45 opts[:precompiled] ||= precompiled 46 haml = ::Haml::Engine.new("", opts) 47 else 48 haml = ::Haml::Engine.new(IO.read(file), opts) 49 set_precompiled(file, haml.precompiled) 50 end 51 41 locals_code = build_locals(opts[:locals]) 42 opts[:locals] = {} 43 44 template = load_template(file) 45 46 haml = ::Haml::Engine.new("#{locals_code}#{template}", opts) 52 47 haml.to_html(view_context) 53 48 rescue … … 60 55 61 56 private 57 def load_template(file) 58 template = "" 59 if @@hamls[file] && !cache_template?(file) 60 template = @@hamls[file] 61 else 62 template = IO.read(file) 63 if cache_template?(file) 64 @@hamls[file], @@mtimes[file] = template, Time.now 65 end 66 return template 67 end 68 end 69 70 def build_locals(locals) 71 locals_code = "" 72 if locals 73 locals_code = locals.keys.inject("") do |code, key| 74 code << "- #{key} = @_merb_partial_locals[:#{key}]\n" 75 end 76 end 77 end 62 78 63 def get_precompiled(path, opts={}) 64 if @@hamls[path] && !cache_template?(path) 65 return @@hamls[path] 66 end 67 end 68 69 def set_precompiled(path, precompiled) 70 @@hamls[path], @@mtimes[path] = precompiled, Time.now 71 end 72 73 def cache_template?(path) 74 return false unless ::Merb::Server.config[:cache_templates] 75 return true unless @@hamls[path] 76 @@mtimes[path] < File.mtime(path) || 77 (File.symlink?(path) && (@@mtimes[path] < File.lstat(path).mtime)) 78 end 79 def cache_template?(path) 80 return false unless ::Merb::Server.config[:cache_templates] 81 return true unless @@hamls[path] 82 @@mtimes[path] < File.mtime(path) || 83 (File.symlink?(path) && (@@mtimes[path] < File.lstat(path).mtime)) 84 end 79 85 80 86 end trunk/lib/merb/template/markaby.rb
r195 r245 34 34 def transform(options = {}) 35 35 opts, file, view_context = options.values_at(:opts, :file, :view_context) 36 37 if opts[:locals] 38 locals = opts[:locals].keys.inject("") do |code, key| 39 code << "#{key} = @_merb_partial_locals[:#{key}]\n" 40 end 41 end 42 36 43 mab = ::Markaby::Builder.new({}, view_context) { 37 instance_eval( File.read(file))44 instance_eval("#{locals}#{File.read(file)}") 38 45 } 39 46 mab.to_s
