Class: Pakyow::Presenter::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/presenter/templates.rb

Constant Summary collapse

DEFAULT_LAYOUTS_PATH =
"layouts"
DEFAULT_PARTIALS_PATH =
"includes"
DEFAULT_PAGES_PATH =
"pages"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, processor: nil, config: {}) ⇒ Templates

Returns a new instance of Templates.



18
19
20
21
22
23
# File 'lib/pakyow/presenter/templates.rb', line 18

def initialize(name, path, processor: nil, config: {})
  @name, @path, @processor = name, Pathname(path), processor
  @layouts, @includes, @info = {}, {}, {}
  build_config(config)
  load_templates
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/pakyow/presenter/templates.rb', line 12

def config
  @config
end

#includesObject (readonly)

Returns the value of attribute includes.



12
13
14
# File 'lib/pakyow/presenter/templates.rb', line 12

def includes
  @includes
end

#layoutsObject (readonly)

Returns the value of attribute layouts.



12
13
14
# File 'lib/pakyow/presenter/templates.rb', line 12

def layouts
  @layouts
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/pakyow/presenter/templates.rb', line 12

def name
  @name
end

#pagesObject (readonly)

Returns the value of attribute pages.



12
13
14
# File 'lib/pakyow/presenter/templates.rb', line 12

def pages
  @pages
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/pakyow/presenter/templates.rb', line 12

def path
  @path
end

#processorObject (readonly)

Returns the value of attribute processor.



12
13
14
# File 'lib/pakyow/presenter/templates.rb', line 12

def processor
  @processor
end

Instance Method Details

#eachObject

Yields each template.



80
81
82
83
84
85
86
87
88
# File 'lib/pakyow/presenter/templates.rb', line 80

def each
  @info.each do |_path, info|
    yield info[:layout]
    yield info[:page]
    info[:partials].each do |_name, partial|
      yield partial
    end
  end
end

#info(path) ⇒ Object



33
34
35
36
37
# File 'lib/pakyow/presenter/templates.rb', line 33

def info(path)
  if view?(path)
    @info[path]
  end
end

#layout(name_or_path) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/pakyow/presenter/templates.rb', line 39

def layout(name_or_path)
  if name_or_path.is_a?(Symbol)
    layout_with_name(name_or_path)
  else
    info(name_or_path) & [:layout]
  end
end

#layouts_pathObject



59
60
61
# File 'lib/pakyow/presenter/templates.rb', line 59

def layouts_path
  path.join(@config[:paths][:layouts])
end

#page(path) ⇒ Object



47
48
49
# File 'lib/pakyow/presenter/templates.rb', line 47

def page(path)
  info(path) & [:page]
end

#pages_pathObject



67
68
69
# File 'lib/pakyow/presenter/templates.rb', line 67

def pages_path
  path.join(@config[:paths][:pages])
end

#partial(path, name) ⇒ Object



55
56
57
# File 'lib/pakyow/presenter/templates.rb', line 55

def partial(path, name)
  partials(path)[name.to_sym]
end

#partials(path) ⇒ Object



51
52
53
# File 'lib/pakyow/presenter/templates.rb', line 51

def partials(path)
  info(path) & [:partials] || {}
end

#partials_pathObject



63
64
65
# File 'lib/pakyow/presenter/templates.rb', line 63

def partials_path
  path.join(@config[:paths][:partials])
end

#pathsObject



29
30
31
# File 'lib/pakyow/presenter/templates.rb', line 29

def paths
  @info.keys
end

#template?(path) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/pakyow/presenter/templates.rb', line 71

def template?(path)
  return false if path.basename.to_s.start_with?(".")
  return false unless path.extname == ".html" || @processor&.process?(path.extname)

  true
end

#view?(path) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/pakyow/presenter/templates.rb', line 25

def view?(path)
  @info.key?(path)
end