Class: Frender::Renderer

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging
Defined in:
lib/frender/renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(spec_file, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



5
6
7
8
9
10
11
# File 'lib/frender/renderer.rb', line 5

def initialize(spec_file, options={})
  @spec_file = spec_file
  @options = options

  load_spec
  load_scope
end

Instance Method Details

#load_scopeObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/frender/renderer.rb', line 58

def load_scope
  if @scope_file = @options[:scope]
    debug "Using scope file %s from options hash" % @options[:scope]
  else
    debug "Using scope file %s from specification" % @spec[:yaml_scope]
    @scope_file = @spec[:yaml_scope]
  end

  info "Reading scope file %s" % @scope_file

  @scope = YAML.load(File.read(@scope_file))
end

#load_specObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/frender/renderer.rb', line 39

def load_spec
  raise("Cannot find spec file %s" % @spec_file) unless File.exist?(@spec_file)
  raise("Cannot read spec file %s" % @spec_file) unless File.readable?(@spec_file)

  info "Reading specification file %s" % @spec_file

  @spec = YAML.load(File.read(@spec_file))

  if !@spec[:frender].is_a?(Hash) || @spec[:frender].empty?
    raise "No :frender: found in the spec loaded from %s" % @spec_file
  end

  @spec = @spec[:frender]

  if !@spec[:files].is_a?(Hash)
    raise "No :files: found in the spec loaded from %s" % @spec_file
  end
end

#render!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/frender/renderer.rb', line 17

def render!
  @spec[:files].each do |file, options|
    mode = options.fetch(:mode, 0644)

    info "Rendering %s with mode %o using template %s" % [file, mode, options[:template]]

    File.open(file, "w") do |f|
      f.puts TemplateWrapper::render(options, scope)
    end

    FileUtils.chmod mode, file
  end

  if @options[:stat]
    puts
    c = "git diff --color --stat %s" % @spec[:files].map{|f,_| f}.join(" ")
    system(c)
  end

  nil
end

#scopeObject



13
14
15
# File 'lib/frender/renderer.rb', line 13

def scope
  Marshal.load(Marshal.dump(@scope))
end