Class: Liquor::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/liquor/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/liquor/manager.rb', line 5

def initialize(options={})
  import   = options.delete(:import).to_a || []
  @dump_ir = options.delete(:dump_intermediates)

  if options.any?
    raise "Unknown function options: #{options.keys.join ", "}"
  end

  @compiler  = Liquor::Compiler.new(manager: self)
  import.each do |library|
    library.export @compiler
  end

  @parser    = Liquor::Parser.new(@compiler.tags)

  @sources   = {}
  @templates = {}
  @partials  = {}
  @compiled_templates = nil

  @diagnostics = []
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics



3
4
5
# File 'lib/liquor/manager.rb', line 3

def diagnostics
  @diagnostics
end

Instance Method Details

#compileObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/liquor/manager.rb', line 69

def compile
  @compiled_templates = {}

  @templates.each do |name, (template, externals)|
    @compiler.compile(template, externals, name)
    dump_intermediates(name, template)

    if @compiler.success?
      @compiled_templates[name] = @compiler.code
    else
      @diagnostics.concat @compiler.diagnostics
    end
  end

  success?
end

#decorate(error) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/liquor/manager.rb', line 121

def decorate(error)
  decorated = []

  if error.is_a? Diagnostic
    if error.location
      if file = error.location[:file]
        decorated = error.decorate @sources[file]
      end
    end
  end

  decorated
end

#errorsObject



86
87
88
# File 'lib/liquor/manager.rb', line 86

def errors
  diagnostics.select &:error?
end

#fetch_partial(name) ⇒ Object



65
66
67
# File 'lib/liquor/manager.rb', line 65

def fetch_partial(name)
  @partials[name.to_s]
end

#has_template?(name) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/liquor/manager.rb', line 94

def has_template?(name)
  !@compiled_templates[name].nil?
end

#partial?(name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/liquor/manager.rb', line 28

def partial?(name)
  name.start_with? '_'
end

#register_layout(name, source, externals = []) ⇒ Object



46
47
48
# File 'lib/liquor/manager.rb', line 46

def register_layout(name, source, externals=[])
  register_template name, source, externals + [ :_inner_template ]
end

#register_partial(name, source) ⇒ Object



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

def register_partial(name, source)
  unless partial? name
    raise ::ArgumentError, "Template `#{name}' is not a partial"
  end

  @sources[name] = source.dup

  if @parser.parse(source, name)
    @partials[name.to_s] = @parser.ast
  else
    @partials[name.to_s] = :syntax_error
    @diagnostics.concat @parser.errors
  end
end

#register_template(name, source, externals = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/liquor/manager.rb', line 32

def register_template(name, source, externals=[])
  if partial? name
    raise ::ArgumentError, "Template `#{name}' is a partial"
  end

  @sources[name] = source.dup

  if @parser.parse(source, name)
    @templates[name.to_s] = [ @parser.ast, externals ]
  else
    @diagnostics.concat @parser.errors
  end
end

#render(name, externals = {}, storage = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/liquor/manager.rb', line 98

def render(name, externals={}, storage={})
  if @compiled_templates.nil?
    raise ::RuntimeError, "Call #compile before #render"
  end

  template = @compiled_templates[name]
  if template.nil?
    raise Error.new("Template `#{name}' does not exist")
  end

  template.call(externals, storage)
end

#render_with_layout(layout_name, layout_externals, template_name, template_externals, storage = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/liquor/manager.rb', line 111

def render_with_layout(layout_name, layout_externals,
                       template_name, template_externals,
                       storage={})
  template_result = render(template_name, template_externals, storage)
  layout_result   = render(layout_name, layout_externals.
                        merge(_inner_template: template_result), storage)

  layout_result
end

#success?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/liquor/manager.rb', line 90

def success?
  errors.none?
end