Class: Dry::View::Layout

Inherits:
Object
  • Object
show all
Extended by:
Configurable
Defined in:
lib/dry/view/layout.rb

Constant Summary collapse

DEFAULT_DIR =
'layouts'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLayout

Returns a new instance of Layout.



48
49
50
51
52
53
54
55
# File 'lib/dry/view/layout.rb', line 48

def initialize
  @config = self.class.config
  @default_format = self.class.default_format
  @layout_dir = DEFAULT_DIR
  @layout_path = "#{layout_dir}/#{config.name}"
  @template_path = config.template
  @scope = config.scope
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



25
26
27
# File 'lib/dry/view/layout.rb', line 25

def config
  @config
end

#default_formatObject (readonly)

Returns the value of attribute default_format.



25
26
27
# File 'lib/dry/view/layout.rb', line 25

def default_format
  @default_format
end

#layout_dirObject (readonly)

Returns the value of attribute layout_dir.



25
26
27
# File 'lib/dry/view/layout.rb', line 25

def layout_dir
  @layout_dir
end

#layout_pathObject (readonly)

Returns the value of attribute layout_path.



25
26
27
# File 'lib/dry/view/layout.rb', line 25

def layout_path
  @layout_path
end

#scopeObject (readonly)

Returns the value of attribute scope.



25
26
27
# File 'lib/dry/view/layout.rb', line 25

def scope
  @scope
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



25
26
27
# File 'lib/dry/view/layout.rb', line 25

def template_path
  @template_path
end

Class Method Details

.default_formatObject



44
45
46
# File 'lib/dry/view/layout.rb', line 44

def self.default_format
  config.formats.keys.first
end

.renderer(format = default_format) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/dry/view/layout.rb', line 28

def self.renderer(format = default_format)
  unless config.formats.key?(format.to_sym)
    raise ArgumentError, "format +#{format}+ is not configured"
  end

  renderers[format]
end

.renderersObject



36
37
38
39
40
41
42
# File 'lib/dry/view/layout.rb', line 36

def self.renderers
  @renderers ||= Hash.new do |h, key|
    h[key.to_sym] = Renderer.new(
      config.root, format: key, engine: config.formats[key.to_sym]
    )
  end
end

Instance Method Details

#call(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/dry/view/layout.rb', line 57

def call(options = {})
  renderer = self.class.renderer(options.fetch(:format, default_format))

  template_content = renderer.(template_path, template_scope(options, renderer))

  renderer.(layout_path, layout_scope(options, renderer)) do
    template_content
  end
end

#locals(options) ⇒ Object



67
68
69
# File 'lib/dry/view/layout.rb', line 67

def locals(options)
  options.fetch(:locals, {})
end

#parts(locals, renderer) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dry/view/layout.rb', line 71

def parts(locals, renderer)
  return empty_part(template_path, renderer) unless locals.any?

  part_hash = locals.each_with_object({}) do |(key, value), result|
    part =
      case value
      when Array
        el_key = Inflecto.singularize(key).to_sym

        template_part(
          key, renderer,
          value.map { |element| template_part(el_key, renderer, element) }
        )
      else
        template_part(key, renderer, value)
      end

    result[key] = part
  end

  part(template_path, renderer, part_hash)
end