Class: Deas::TemplateSource
- Inherits:
-
Object
- Object
- Deas::TemplateSource
- Defined in:
- lib/deas/template_source.rb
Direct Known Subclasses
Constant Summary collapse
- DISALLOWED_ENGINE_EXTS =
[ 'rb' ]
- DisallowedEngineExtError =
Class.new(ArgumentError)
Instance Attribute Summary collapse
-
#engines ⇒ Object
readonly
Returns the value of attribute engines.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #engine(input_ext, engine_class, registered_opts = nil) ⇒ Object
- #engine_for?(ext) ⇒ Boolean
- #engine_for_template?(template_name) ⇒ Boolean
-
#initialize(path, logger = nil) ⇒ TemplateSource
constructor
A new instance of TemplateSource.
- #partial(template_name, locals, &content) ⇒ Object
- #render(template_name, view_handler, locals, &content) ⇒ Object
Constructor Details
#initialize(path, logger = nil) ⇒ TemplateSource
Returns a new instance of TemplateSource.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/deas/template_source.rb', line 14 def initialize(path, logger = nil) @path = path.to_s @default_engine_opts = { 'source_path' => @path, 'logger' => logger || Deas::NullLogger.new, 'default_template_source' => self } @engines = Hash.new{ |h, k| Deas::NullTemplateEngine.new(@default_engine_opts) } @ext_cache = Hash.new do |hash, template_name| paths = Dir.glob("#{File.join(@path, template_name.to_s)}.*") paths = paths.reject{ |p| !@engines.keys.include?(parse_ext(p)) } if !(ext = parse_ext(paths.first.to_s)).nil? hash[template_name] = ext end end end |
Instance Attribute Details
#engines ⇒ Object (readonly)
Returns the value of attribute engines.
12 13 14 |
# File 'lib/deas/template_source.rb', line 12 def engines @engines end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/deas/template_source.rb', line 12 def path @path end |
Instance Method Details
#engine(input_ext, engine_class, registered_opts = nil) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/deas/template_source.rb', line 31 def engine(input_ext, engine_class, registered_opts = nil) if DISALLOWED_ENGINE_EXTS.include?(input_ext) raise DisallowedEngineExtError, "`#{input_ext}` is disallowed as an"\ " engine extension." end engine_opts = @default_engine_opts.merge(registered_opts || {}) @engines[input_ext.to_s] = engine_class.new(engine_opts) end |
#engine_for?(ext) ⇒ Boolean
40 41 42 |
# File 'lib/deas/template_source.rb', line 40 def engine_for?(ext) @engines.keys.include?(ext) end |
#engine_for_template?(template_name) ⇒ Boolean
44 45 46 |
# File 'lib/deas/template_source.rb', line 44 def engine_for_template?(template_name) self.engine_for?(get_template_ext(template_name)) end |
#partial(template_name, locals, &content) ⇒ Object
56 57 58 |
# File 'lib/deas/template_source.rb', line 56 def partial(template_name, locals, &content) get_engine(template_name).partial(template_name, locals, &content) end |
#render(template_name, view_handler, locals, &content) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/deas/template_source.rb', line 48 def render(template_name, view_handler, locals, &content) [ view_handler.layouts, template_name ].flatten.reverse.inject(content) do |render_proc, name| proc{ get_engine(name).render(name, view_handler, locals, &render_proc) } end.call end |