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.



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

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

Instance Attribute Details

#template_nameObject (readonly)

Returns the value of attribute template_name.



6
7
8
# File 'lib/pages_core/templates/template_configuration.rb', line 6

def template_name
  @template_name
end

Class Method Details

.all_blocksObject



9
10
11
12
13
14
15
# File 'lib/pages_core/templates/template_configuration.rb', line 9

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

.all_templatesObject



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

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

.configObject



21
22
23
# File 'lib/pages_core/templates/template_configuration.rb', line 21

def config
  PagesCore::Templates.configuration
end

.localized_blocksObject



25
26
27
28
29
30
31
32
33
# File 'lib/pages_core/templates/template_configuration.rb', line 25

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



72
73
74
75
76
77
78
# File 'lib/pages_core/templates/template_configuration.rb', line 72

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



52
53
54
# File 'lib/pages_core/templates/template_configuration.rb', line 52

def config
  self.class.config
end

#enabled_blocksObject



80
81
82
83
84
85
86
87
# File 'lib/pages_core/templates/template_configuration.rb', line 80

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



89
90
91
92
93
94
95
# File 'lib/pages_core/templates/template_configuration.rb', line 89

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

#options(*path) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/pages_core/templates/template_configuration.rb', line 64

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



56
57
58
59
60
61
62
# File 'lib/pages_core/templates/template_configuration.rb', line 56

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