lib/mustache_render/mustache.rb in mustache_render-0.0.10 vs lib/mustache_render/mustache.rb in mustache_render-0.0.12

- old
+ new

@@ -17,25 +17,18 @@ def media @media ||= config.default_render_media end def render(data = template, ctx = {}) - self.template = data - - return self.template.render(context) if ctx == {} - - begin - context.push(ctx) - self.template.render(context) - ensure - context.pop + impl_logger :level => :debug, :operation => 'MustacheRender::Mustache.render' do + impl_render data, ctx end end # 片段树 def partials_tree - + end # Context accessors. # # view = Mustache.new @@ -64,21 +57,25 @@ end # Given a file name and an optional context, attempts to load and # render the file as a template. def file_render(name, context = {}) - @media = :file - render(partial(name), context) + impl_logger :level => :debug, :operation => "#{self.class}.file_render" do + @media = :file + render(partial(name), context) + end end def self.db_render(full_path, context={}) self.new.db_render full_path, context end def db_render(full_path, context={}) - @media = :db - render(partial(full_path), context) + impl_logger :level => :debug, :operation => "#{self.class}.db_render" do + @media = :db + render(partial(full_path), context) + end end def impl_read_db_template name db_template = ::MustacheRenderTemplate.find_with_full_path(name) db_template.try :content @@ -113,28 +110,35 @@ end end end def read_template_from_media name, media - ::MustacheRender.logger.debug "MustacheRender render -> read template from #{media}: #{name}" - # puts "read_template_from_media: #{name} #{media}" - case media - when :db - if ::MustacheRender.config.db_template_cache? - self.class.fetch_partial_cache name, media, :expires_in => ::MustacheRender.config.db_template_cache_expires_in do - impl_read_db_template name - end - else - impl_read_db_template name - end - when :file - if ::MustacheRender.config.file_template_cache? - self.class.fetch_partial_cache name, media, :expires_in => ::MustacheRender.config.file_template_cache_expires_in do - impl_read_file_template name - end - else + # ::MustacheRender.logger.debug "MustacheRender render -> read template from #{media}: #{name}" + + impl_logger :level => :debug, + :operation => "MustacheRender render -> read template from #{media}: #{name}" do + + case media + when :db + # if ::MustacheRender.config.db_template_cache? + # self.class.fetch_partial_cache name, media, :expires_in => ::MustacheRender.config.db_template_cache_expires_in do + # impl_read_db_template name + # end + # else + # impl_read_db_template name + # end impl_read_file_template name + when :file + # if ::MustacheRender.config.file_template_cache? + # self.class.fetch_partial_cache name, media, :expires_in => ::MustacheRender.config.file_template_cache_expires_in do + # impl_read_file_template name + # end + # else + # impl_read_file_template name + # end + + impl_read_file_template name end end end # Override this in your subclass if you want to do fun things like @@ -146,39 +150,39 @@ # return self.read_template_from_media name, media @_cached_partials ||= {} (@_cached_partials[media] ||= {})[name] ||= self.read_template_from_media name, media end - def self.partial_cache_key(name, media) - raise 'options key: :media must in(:file, :db)' unless [:file, :db].include?(media) - "MustacheRender::Mustache#Template.cache##{media}##{name}" - end + ## def self.partial_cache_key(name, media) + ## raise 'options key: :media must in(:file, :db)' unless [:file, :db].include?(media) + ## "MustacheRender::Mustache#Template.cache##{media}##{name}" + ## end + ## + ## def self.exist_partial_cache?(name, media) + ## ::MustacheRender.config.cache.exist?(self.partial_cache_key(name, media)) + ## end + ## + ## def self.delete_partial_cache(name, media) + ## ::MustacheRender.config.cache.delete(self.partial_cache_key(name, media)) + ## end + ## + ## def self.fetch_partial_cache(name, media, options={}, &block) + ## ::MustacheRender.config.cache.fetch(self.partial_cache_key(name, media), options) do + ## if block_given? + ## block.call + ## end + ## end + ## end + ## + ## def self.read_partial_cache(name, media) + ## ::MustacheRender.config.cache.read(self.partial_cache_key(name, media)) + ## end + ## + ## def self.write_partial_cache(name, media, value, options={}) + ## ::MustacheRender.config.cache.write(self.partial_cache_key(name, media), value, options) + ## end - def self.exist_partial_cache?(name, media) - ::MustacheRender.config.cache.exist?(self.partial_cache_key(name, media)) - end - - def self.delete_partial_cache(name, media) - ::MustacheRender.config.cache.delete(self.partial_cache_key(name, media)) - end - - def self.fetch_partial_cache(name, media, options={}, &block) - ::MustacheRender.config.cache.fetch(self.partial_cache_key(name, media), options) do - if block_given? - block.call - end - end - end - - def self.read_partial_cache(name, media) - ::MustacheRender.config.cache.read(self.partial_cache_key(name, media)) - end - - def self.write_partial_cache(name, media, value, options={}) - ::MustacheRender.config.cache.write(self.partial_cache_key(name, media), value, options) - end - # Override this to provide custom escaping. # # class PersonView < Mustache # def escapeHTML(str) # my_html_escape_method(str) @@ -308,7 +312,44 @@ # # Returns the inherited or default configuration setting. def self.inheritable_config_for(attr_name, default) superclass.respond_to?(attr_name) ? superclass.send(attr_name) : default end + + private + + def impl_logger options={} + level = options[:level] || :debug + + result = nil + + if block_given? + start_at = Time.now + result = yield + ms = ((Time.now - start_at) * 1000).to_i + MustacheRender.logger.send level, impl_format_log_entry( + "#{options[:operation]} (#{ms}ms)", options[:message] + ) + end + + result + end + + def impl_format_log_entry(operation, message = nil) + " \033[4;34;1m#{operation}\033[0m \033[0;1m#{message}\033[0m" + end + + def impl_render(data=template, ctx={}) + self.template = data + + return self.template.render(context) if ctx == {} + + begin + context.push(ctx) + self.template.render(context) + ensure + context.pop + end + end + end end