Class: Trekky::HamlSource

Inherits:
Source
  • Object
show all
Defined in:
lib/trekky/haml_source.rb

Instance Attribute Summary collapse

Attributes inherited from Source

#context, #errors, #output, #path

Instance Method Summary collapse

Methods inherited from Source

#add_error, #clear_errors, #extension, #input, #render_error, #render_errors, #valid?

Constructor Details

#initialize(context, path) ⇒ HamlSource

Returns a new instance of HamlSource.



9
10
11
12
13
# File 'lib/trekky/haml_source.rb', line 9

def initialize(context, path)
  super
  @regions = {}
  @env = {}
end

Instance Attribute Details

#regionsObject (readonly)

Returns the value of attribute regions.



7
8
9
# File 'lib/trekky/haml_source.rb', line 7

def regions
  @regions
end

Instance Method Details

#content_for(name, &block) ⇒ Object



46
47
48
49
# File 'lib/trekky/haml_source.rb', line 46

def content_for(name, &block)
  return unless block_given?
  regions[name] = capture_haml(&block)
end

#partial(name) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/trekky/haml_source.rb', line 37

def partial(name)
  source = context.find_partial(name)
  if source
    source.render(layout: false)
  else
    STDERR.puts "[ERROR] Can't find partial: #{name}"
  end
end

#render(options = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/trekky/haml_source.rb', line 15

def render(options = {}, &block)
  clear_errors
  @output = if block_given? || options[:layout] == false
    render_input(&block)
  else
    buffer = render_input
    if layout
      layout.render do |name|
        if region.nil?
          buffer
        elsif regions.has_key?(name)
          regions[name]
        end
      end
    else
      buffer
    end
  end
rescue Exception => error
  add_error error
end

#typeObject



51
52
53
# File 'lib/trekky/haml_source.rb', line 51

def type
  :haml
end