Class: Lono::Template::Template

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/lono/template/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blueprint, name, block = nil, options = {}) ⇒ Template

Returns a new instance of Template.



16
17
18
19
# File 'lib/lono/template/template.rb', line 16

def initialize(blueprint, name, block=nil, options={})
  @blueprint, @name, @block, @options = blueprint, name, block, options
  @source_path = default_source_path(name)
end

Instance Attribute Details

#nameObject (readonly)

Main template Erb methods are: source and variables

template "example-2" do
  source "example"
  variables(test: 1)
end


15
16
17
# File 'lib/lono/template/template.rb', line 15

def name
  @name
end

Instance Method Details

#buildObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/lono/template/template.rb', line 38

def build
  instance_eval(&@block) if @block

  if File.exist?(@source_path)
    RenderMePretty.result(@source_path, context: context)
  else
    puts "ERROR: #{@source_path} does not exist, but it was used as a template source.".color(:red)
    exit 1
  end
end

#contextObject

Context for ERB rendering. This is where we control what references get passed to the ERB rendering.



51
52
53
# File 'lib/lono/template/template.rb', line 51

def context
  @context ||= Lono::Template::Context.new(@options)
end

#default_source_path(name) ⇒ Object

internal methods



34
35
36
# File 'lib/lono/template/template.rb', line 34

def default_source_path(name)
  "#{Lono.config.templates_path}/#{name}.yml" # defaults to name, source method overrides
end

#source(path) ⇒ Object

Returns path, example: ./app/templates/example.yml



22
23
24
25
# File 'lib/lono/template/template.rb', line 22

def source(path)
  @source_path = path[0..0] == '/' ? path : "#{Lono.config.templates_path}/#{path}"
  @source_path += ".yml"
end

#variables(vars = {}) ⇒ Object



27
28
29
30
31
# File 'lib/lono/template/template.rb', line 27

def variables(vars={})
  vars.each do |var,value|
    context.instance_variable_set("@#{var}", value)
  end
end