Class: Rodakase::View::Layout

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

Defined Under Namespace

Classes: Scope

Constant Summary collapse

DEFAULT_SCOPE =
Object.new.freeze
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.



39
40
41
42
43
44
45
46
# File 'lib/rodakase/view/layout.rb', line 39

def initialize
  @config = self.class.config
  @renderer = @config.renderer
  @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.



36
37
38
# File 'lib/rodakase/view/layout.rb', line 36

def config
  @config
end

#layout_dirObject (readonly)

Returns the value of attribute layout_dir.



36
37
38
# File 'lib/rodakase/view/layout.rb', line 36

def layout_dir
  @layout_dir
end

#layout_pathObject (readonly)

Returns the value of attribute layout_path.



36
37
38
# File 'lib/rodakase/view/layout.rb', line 36

def layout_path
  @layout_path
end

#rendererObject (readonly)

Returns the value of attribute renderer.



36
37
38
# File 'lib/rodakase/view/layout.rb', line 36

def renderer
  @renderer
end

#scopeObject (readonly)

Returns the value of attribute scope.



36
37
38
# File 'lib/rodakase/view/layout.rb', line 36

def scope
  @scope
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



36
37
38
# File 'lib/rodakase/view/layout.rb', line 36

def template_path
  @template_path
end

Class Method Details

.configure(&block) ⇒ Object



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

def self.configure(&block)
  super do |config|
    yield(config)

    unless config.renderer
      config.renderer = Renderer.new(config.root, engine: config.engine)
    end
  end
end

Instance Method Details

#call(options = {}) ⇒ Object



48
49
50
51
52
# File 'lib/rodakase/view/layout.rb', line 48

def call(options = {})
  renderer.(layout_path, layout_scope(options)) do
    renderer.(template_path, template_scope(options))
  end
end

#layout_part(name, value) ⇒ Object



85
86
87
# File 'lib/rodakase/view/layout.rb', line 85

def layout_part(name, value)
  part(layout_dir, name => value)
end

#layout_scope(options) ⇒ Object



54
55
56
# File 'lib/rodakase/view/layout.rb', line 54

def layout_scope(options)
  Scope.new(layout_part(:page, options.fetch(:scope, scope)))
end

#locals(options) ⇒ Object



62
63
64
# File 'lib/rodakase/view/layout.rb', line 62

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

#part(dir, value) ⇒ Object



93
94
95
# File 'lib/rodakase/view/layout.rb', line 93

def part(dir, value)
  Part.new(renderer.chdir(dir), value)
end

#parts(locals) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rodakase/view/layout.rb', line 66

def parts(locals)
  return DEFAULT_SCOPE 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, value.map { |element| template_part(el_key, element) })
      else
        template_part(key, value)
      end

    result[key] = part
  end

  part(template_path, part_hash)
end

#template_part(name, value) ⇒ Object



89
90
91
# File 'lib/rodakase/view/layout.rb', line 89

def template_part(name, value)
  part(template_path, name => value)
end

#template_scope(options) ⇒ Object



58
59
60
# File 'lib/rodakase/view/layout.rb', line 58

def template_scope(options)
  parts(locals(options))
end