Class: Sanford::TemplateSource

Inherits:
Object
  • Object
show all
Defined in:
lib/sanford/template_source.rb

Direct Known Subclasses

NullTemplateSource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, logger = nil) ⇒ TemplateSource

Returns a new instance of TemplateSource.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sanford/template_source.rb', line 10

def initialize(path, logger = nil)
  @path = path.to_s
  @default_opts = {
    'source_path' => @path,
    'logger'      => logger || Sanford::NullLogger.new
  }
  @engines = Hash.new do |hash, ext|
    # cache null template exts so we don't repeatedly call this block for
    # known null template exts
    hash[ext.to_s] = Sanford::NullTemplateEngine.new(@default_opts)
  end
  @engine_exts = []
end

Instance Attribute Details

#enginesObject (readonly)

Returns the value of attribute engines.



8
9
10
# File 'lib/sanford/template_source.rb', line 8

def engines
  @engines
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/sanford/template_source.rb', line 8

def path
  @path
end

Instance Method Details

#==(other_template_source) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/sanford/template_source.rb', line 49

def ==(other_template_source)
  if other_template_source.kind_of?(TemplateSource)
    self.path    == other_template_source.path &&
    self.engines == other_template_source.engines
  else
    super
  end
end

#engine(input_ext, engine_class, registered_opts = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/sanford/template_source.rb', line 24

def engine(input_ext, engine_class, registered_opts = nil)
  @engine_exts << input_ext.to_s
  engine_opts = @default_opts.merge(registered_opts || {})
  engine_opts['ext'] = input_ext.to_s
  @engines[input_ext.to_s] = engine_class.new(engine_opts)
end

#engine_for?(ext) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/sanford/template_source.rb', line 31

def engine_for?(ext)
  @engine_exts.include?(ext.to_s)
end

#engine_for_template?(template_name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/sanford/template_source.rb', line 35

def engine_for_template?(template_name)
  self.engine_for?(get_template_ext(template_name))
end

#partial(template_name, locals) ⇒ Object



44
45
46
47
# File 'lib/sanford/template_source.rb', line 44

def partial(template_name, locals)
  engine = @engines[get_template_ext(template_name)]
  engine.partial(template_name, locals)
end

#render(template_name, service_handler, locals) ⇒ Object



39
40
41
42
# File 'lib/sanford/template_source.rb', line 39

def render(template_name, service_handler, locals)
  engine = @engines[get_template_ext(template_name)]
  engine.render(template_name, service_handler, locals)
end