Class: Bridgetown::FrontMatter::Defaults

Inherits:
Object
  • Object
show all
Defined in:
bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb

Overview

This class handles custom defaults for front matter settings. It is exposed via the frontmatter_defaults method on the site class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Defaults

Returns a new instance of Defaults.



13
14
15
16
# File 'bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb', line 13

def initialize(site)
  @site = site
  @defaults_cache = {}
end

Instance Attribute Details

#siteBridgetown::Site (readonly)

Returns:



11
12
13
# File 'bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb', line 11

def site
  @site
end

Instance Method Details

#all(path, collection_name) ⇒ Hash

Collects a hash with all default values for a resource

Parameters:

  • path (String)

    the relative path of the resource

  • collection_name (Symbol)

    :posts, :pages, etc.

Returns:

  • (Hash)

    all default values (an empty hash if there are none)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb', line 40

def all(path, collection_name)
  if @defaults_cache.key?([path, collection_name])
    return @defaults_cache[[path, collection_name]]
  end

  defaults = {}
  merge_data_cascade_for_path(path, defaults)

  old_scope = nil
  matching_sets(path, collection_name).each do |set|
    if has_precedence?(old_scope, set["scope"])
      defaults = Utils.deep_merge_hashes(defaults, set["values"])
      old_scope = set["scope"]
    else
      defaults = Utils.deep_merge_hashes(set["values"], defaults)
    end
  end

  @defaults_cache[[path, collection_name]] = defaults
end

#ensure_time!(set) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb', line 24

def ensure_time!(set)
  return set unless set.key?("values") && set["values"].key?("date")
  return set if set["values"]["date"].is_a?(Time)

  set["values"]["date"] = Utils.parse_date(
    set["values"]["date"],
    "An invalid date format was found in a front-matter default set: #{set}"
  )
  set
end

#resetObject



18
19
20
21
22
# File 'bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb', line 18

def reset
  @glob_cache = {}
  @defaults_cache = {}
  @sets = nil
end