Class: PagesCore::Templates::TemplateConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/pages_core/templates/template_configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_name) ⇒ TemplateConfiguration

Returns a new instance of TemplateConfiguration.



44
45
46
# File 'lib/pages_core/templates/template_configuration.rb', line 44

def initialize(template_name)
  @template_name = template_name.to_sym
end

Instance Attribute Details

#template_nameObject (readonly)

Returns the value of attribute template_name.



4
5
6
# File 'lib/pages_core/templates/template_configuration.rb', line 4

def template_name
  @template_name
end

Class Method Details

.all_blocksObject



7
8
9
10
11
# File 'lib/pages_core/templates/template_configuration.rb', line 7

def all_blocks
  (config.get(:default, :blocks).keys +
   all_templates.map { |t| configured_block_names(t) } +
   all_templates.map { |t| enabled_block_names(t) }).flatten.compact.uniq
end

.all_templatesObject



13
14
15
# File 'lib/pages_core/templates/template_configuration.rb', line 13

def all_templates
  Array(config.get(:templates).try(&:keys))
end

.configObject



17
18
19
# File 'lib/pages_core/templates/template_configuration.rb', line 17

def config
  PagesCore::Templates.configuration
end

.localized_blocksObject



21
22
23
24
25
26
27
28
29
# File 'lib/pages_core/templates/template_configuration.rb', line 21

def localized_blocks
  template_blocks = all_templates.flat_map do |t|
    config.get(:templates, t, :blocks).to_a
  end

  (config.get(:default, :blocks).to_a + template_blocks)
    .compact
    .select { |_, opts| opts[:localized] }.map(&:first)
end

Instance Method Details

#block(block_name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/pages_core/templates/template_configuration.rb', line 68

def block(block_name)
  default_block_options(block_name)
    .deep_merge(config.get(:default, :blocks, block_name) || {})
    .deep_merge(
      config.get(:templates, @template_name, :blocks, block_name) || {}
    )
end

#configObject



48
49
50
# File 'lib/pages_core/templates/template_configuration.rb', line 48

def config
  self.class.config
end

#enabled_blocksObject



76
77
78
79
80
81
82
83
# File 'lib/pages_core/templates/template_configuration.rb', line 76

def enabled_blocks
  blocks = value(:enabled_blocks)
  blocks = [:name] + blocks unless blocks.include?(:name)
  if block_given?
    blocks.each { |block_name| yield block_name, block(block_name) }
  end
  blocks
end

#metadata_blocksObject



85
86
87
88
89
90
91
# File 'lib/pages_core/templates/template_configuration.rb', line 85

def 
  blocks = PagesCore::Templates.
  if block_given?
    blocks.each { |block_name| yield block_name, block(block_name) }
  end
  blocks
end

#options(*path) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/pages_core/templates/template_configuration.rb', line 60

def options(*path)
  path = [path, :options].flatten
  (config.get(*[:default, path].flatten) || {})
    .deep_merge(
      config.get(*[:templates, @template_name, path].flatten) || {}
    )
end

#value(*path) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/pages_core/templates/template_configuration.rb', line 52

def value(*path)
  path = [path, :value].flatten
  value = config.get(*[:default, path].flatten)
  template_value = config.get(*[:templates, @template_name, path].flatten)
  value = template_value unless template_value.nil?
  value
end