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.



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

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

Instance Attribute Details

#siteBridgetown::Site (readonly)

Returns:



9
10
11
# File 'bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb', line 9

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)



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

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



21
22
23
24
25
26
27
28
29
30
# File 'bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb', line 21

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



16
17
18
19
# File 'bridgetown-core/lib/bridgetown-core/front_matter/defaults.rb', line 16

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