Class: Doozer::MailerPartial
- Includes:
- Util::Logger, ViewHelpers, ERB::Util
- Defined in:
- lib/doozer/mailer_partial.rb
Overview
The MailerPartial is really similar to Doozer::Partial.
Constant Summary collapse
- @@partials =
{}
Instance Attribute Summary collapse
-
#erb ⇒ Object
Returns the value of attribute erb.
Class Method Summary collapse
-
.clear_loaded_partials ⇒ Object
Class methods for clearing all cached partials.
-
.include_view_helper(helper) ⇒ Object
Class method for including a view helper.
-
.load_partial(name) ⇒ Object
Load and cache partial ERB template with the given file_name.
-
.partial(file = nil, locals = {}) ⇒ Object
This class method lazy loads and caches the erb templates of the requested partials.
Instance Method Summary collapse
- #bind ⇒ Object
-
#initialize(erb, locals) ⇒ MailerPartial
constructor
A new instance of MailerPartial.
- #partial(file = nil, locals = {}) ⇒ Object
Methods included from ViewHelpers
#alink, #app_name, #app_path, #aurl, #authtoken, #base_url, #feed, #h, #hash_to_props, #hash_to_qs, #img, #ip, #javascript, #link, #metatags, #path, #rack_env, #server_name, #session?, #static_url, #stylesheet, #title, #url
Methods included from Util::Logger
Constructor Details
#initialize(erb, locals) ⇒ MailerPartial
Returns a new instance of MailerPartial.
12 13 14 15 16 17 18 19 20 |
# File 'lib/doozer/mailer_partial.rb', line 12 def initialize(erb, locals) @erb = erb if locals.kind_of? Hash locals.each_pair {|key, value| #p "#{key}:#{value}" self.instance_variable_set("@#{key}".to_sym, value) # :@a, value } end end |
Instance Attribute Details
#erb ⇒ Object
Returns the value of attribute erb.
4 5 6 |
# File 'lib/doozer/mailer_partial.rb', line 4 def erb @erb end |
Class Method Details
.clear_loaded_partials ⇒ Object
Class methods for clearing all cached partials. Mainly a dispatcher for the file watcher to pick up new changes without having to restart the appserver in development mode.
64 65 66 |
# File 'lib/doozer/mailer_partial.rb', line 64 def self.clear_loaded_partials @@partials = {} end |
.include_view_helper(helper) ⇒ Object
Class method for including a view helper.
69 70 71 72 |
# File 'lib/doozer/mailer_partial.rb', line 69 def self.include_view_helper(helper) m = Doozer::Lib.classify(helper) include Object.const_get(m) end |
.load_partial(name) ⇒ Object
Load and cache partial ERB template with the given file_name.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/doozer/mailer_partial.rb', line 51 def self.load_partial(name) file = File.join(Doozer::Configs.app_path,"app/views/#{name}.html.erb") results = [] begin File.new(file, "r").each { |line| results << line } @@partials[name] = ERB.new(results.join("")) rescue puts "ERROR => sorry couldn't load partial #{name} (#{file})" end end |
.partial(file = nil, locals = {}) ⇒ Object
This class method lazy loads and caches the erb templates of the requested partials
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/doozer/mailer_partial.rb', line 27 def self.partial(file=nil, locals={}) #p "Class method: Doozer::Partial#partial" dir = locals[:view_dir] if file.index("/").nil? name = "#{dir}/_#{file}" else name = "#{file.gsub(/\//,'/_')}" end load_partial(name) if @@partials[name].nil? erb = @@partials[name] if erb partial = Doozer::MailerPartial.new(erb, locals) partial.bind() else puts "ERROR => no partial exists for #{file}\n" end end |
Instance Method Details
#bind ⇒ Object
22 23 24 |
# File 'lib/doozer/mailer_partial.rb', line 22 def bind @erb.result(binding) end |
#partial(file = nil, locals = {}) ⇒ Object
45 46 47 48 |
# File 'lib/doozer/mailer_partial.rb', line 45 def partial(file=nil, locals={}) locals[:view_dir] = @view_dir if not @view_dir.nil? Doozer::MailerPartial.partial(file, locals) end |