Class: Nagoro::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/nagoro/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Template

Returns a new instance of Template.



19
20
21
22
# File 'lib/nagoro/template.rb', line 19

def initialize(options = {})
  parse_option(options)
  @compiled = false
end

Instance Attribute Details

#bindingObject

Returns the value of attribute binding.



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

def binding
  @binding
end

#compiledObject

Returns the value of attribute compiled.



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

def compiled
  @compiled
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#pipesObject

Returns the value of attribute pipes.



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

def pipes
  @pipes
end

Class Method Details

.[](*pipes) ⇒ Object



13
14
15
# File 'lib/nagoro/template.rb', line 13

def self.[](*pipes)
  new(:pipes => pipes.flatten)
end

Instance Method Details

#compile(io, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nagoro/template.rb', line 32

def compile(io, options = {})
  parse_option(options) unless options.empty?

  case io
  when String
    io = File.read(io) if io.size < 1024 and File.file?(io)
    @compiled = pipeline(io)
  when StringIO, IO
    @compiled = pipeline(io.read)
  else
    raise(ArgumentError, "Cannot compile %p" % io)
  end

  return self
end

#parse_option(options = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/nagoro/template.rb', line 24

def parse_option(options = {})
  @binding   = options.fetch(:binding, BindingProvider.binding)
  @file      = options[:filename] || DEFAULT_FILE
  @line      = options[:line] || DEFAULT_LINE
  @pipes     = options[:pipes] || DEFAULT_PIPES
  @variables = options[:variables]
end

#pipeline(io) ⇒ Object

use inject?



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nagoro/template.rb', line 49

def pipeline(io)
  @pipes.each do |pipe|
    if pipe.respond_to?(:new)
      io = pipe.new(io).result
    else
      io = Pipe.const_get(pipe).new(io).result
    end
  end

  return io
rescue Scanner::Stuck => exception
  exception.message << " In: #{@file}"
  puts exception
  ''
end

#render(io, options = {}) ⇒ Object



85
86
87
# File 'lib/nagoro/template.rb', line 85

def render(io, options = {})
  compile(io, options).result
end

#result(options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nagoro/template.rb', line 65

def result(options = {})
  parse_option(options) unless options.empty?

  if vars = @variables
    obj = eval('self', @binding)
    obj.instance_variable_set('@_nagoro_ivs', vars)
    eval(%q(
      @_nagoro_ivs.each{|key, value| instance_variable_set("@#{key}", value) }
    ), @binding)
  end

  eval(@compiled, @binding, @file, @line).strip
end

#tidy_result(options = {}) ⇒ Object



79
80
81
82
83
# File 'lib/nagoro/template.rb', line 79

def tidy_result(options = {})
  final = result(options)
  flags = options.fetch(:flags, Tidy::FLAGS)
  Tidy.tidy(final, flags).strip
end