Skip to content

Commit

Permalink
Improve setting API and default settings
Browse files Browse the repository at this point in the history
We love nested settings.
  • Loading branch information
benjaminwil committed Nov 13, 2023
1 parent d1581d2 commit 630d187
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 41 deletions.
4 changes: 2 additions & 2 deletions lib/lifer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def root
brain.root
end

def setting(name, collection: nil)
brain.setting name, collection: collection
def setting(*name, collection: nil)
brain.setting *name, collection: collection
end

def settings
Expand Down
6 changes: 3 additions & 3 deletions lib/lifer/brain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def manifest
def output_directory
@output_directory ||=
begin
dir = "%s/%s" % [root, Lifer.setting(:output_directory)]
dir = "%s/%s" % [root, setting(:global, :output_directory)]

return Pathname(dir) if Dir.exist? dir

Expand All @@ -49,8 +49,8 @@ def output_directory
end
end

def setting(name, collection: nil)
config.setting name, collection_name: collection&.name
def setting(*name, collection: nil)
config.setting *name, collection_name: collection&.name
end

private
Expand Down
11 changes: 5 additions & 6 deletions lib/lifer/builder/rss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def execute
end

output_filename =
File.join Dir.pwd, Lifer.setting(:feed_uri, collection: collection)
File.join Dir.pwd, Lifer.setting(:feed, :uri, collection: collection)

File.open output_filename, "w" do |file|
file.puts new_feed.to_feed
Expand All @@ -37,7 +37,7 @@ def execute

def initialize(root:)
@collections_with_feeds =
Lifer.collections.select { |collection| collection.setting(:feed_uri) }
Lifer.collections.select { |collection| collection.setting(:feed, :uri) }
@root = root
end

Expand Down Expand Up @@ -67,15 +67,14 @@ def rss_feed_for(collection, &block)
feed.channel.lastBuildDate = Time.now.to_s

feed.channel.link = "%s/%s" % [
Lifer.setting(:host),
Lifer.setting(:feed_uri, collection: collection)
Lifer.setting(:global, :host),
Lifer.setting(:feed, :uri, collection: collection)
]

feed.channel.managingEditor =
Lifer.setting(:site_default_author, collection: collection)

feed.channel.title = Lifer.setting(:site_title, collection: collection)

feed.channel.title = Lifer.setting(:title, collection: collection)

feed.channel.webMaster =
Lifer.setting(:site_default_author, collection: collection)
Expand Down
4 changes: 2 additions & 2 deletions lib/lifer/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def entries
}.map { |entry| Lifer::Entry.new file: entry, collection: self }
end

def setting(name)
Lifer.setting(name, collection: self)
def setting(*name)
Lifer.setting(*name, collection: self)
end

private
Expand Down
52 changes: 31 additions & 21 deletions lib/lifer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ class Lifer::Config
"%s/templates/config" % File.expand_path(File.dirname(__FILE__))
DEFAULT_LAYOUT_FILE =
"%s/templates/layout.html.erb" % File.expand_path(File.dirname(__FILE__))
DEFAULT_SETTINGS = {
default_entry_title: "Untitled Entry",
feed_builder: "atom",
feed_uri: "feed.xml",
host: "https://example.com",
layout_file: DEFAULT_LAYOUT_FILE,
language: "en",
output_directory: "_build",
site_default_author: "Admin",
site_title: "My Lifer Weblog",
site_subtitle: "A great weblog, lol...",
uri_strategy: "simple"
DEFAULT_IMPLICIT_SETTINGS = {
layout_file: DEFAULT_LAYOUT_FILE
}
REGISTERED_SETTINGS = DEFAULT_SETTINGS.keys
DEFAULT_REGISTERED_SETTINGS = [
:author,
:description,
{entries: [:default_title]},
{feed: [:builder, :uri]},
:language,
:layout_file,
:title,
:uri_strategy
]

class << self
def build(file:)
Expand All @@ -32,6 +31,7 @@ def build(file:)
end
end

attr_accessor :registered_settings
attr_reader :file

def collectionables
Expand All @@ -45,20 +45,25 @@ def collectionables
# @param name [Symbol] The configuration setting.
# @param collection_name [Symbol] A collection name.
# @return [String] The value of the best in-scope setting.
def setting(name, collection_name: nil)
collection_setting =
if collection_name && settings[collection_name]
settings[collection_name][name]
end
def setting(*name, collection_name: nil)
name_in_collection = name.dup.unshift(collection_name) if collection_name

candidates = [
settings.dig(*name),
default_settings.dig(*name),
DEFAULT_IMPLICIT_SETTINGS.dig(*name)
]
candidates.unshift settings.dig(*name_in_collection) if name_in_collection

[collection_setting, settings[name], DEFAULT_SETTINGS[name]].detect(&:itself)
candidates.detect &:itself
end

def settings(settings_hash = raw)
settings_hash.select { |setting, value|
value = settings(value) if value.is_a?(Hash)

next unless REGISTERED_SETTINGS.include?(setting) || has_collection_settings?(setting)
next unless DEFAULT_REGISTERED_SETTINGS.include?(setting) ||
has_collection_settings?(setting)

[setting, value]
}.compact.to_h
Expand All @@ -68,6 +73,7 @@ def settings(settings_hash = raw)

def initialize(file:)
@file = Pathname(file)
@registered_settings = DEFAULT_REGISTERED_SETTINGS.to_set
end

def collection_candidates
Expand All @@ -79,6 +85,10 @@ def collection_candidates
.select { |dir| !Lifer.ignoreable? dir }.map(&:to_sym).sort.reverse
end

def default_settings
@default_settings ||=
Lifer::Utilities.symbolize_keys(YAML.load_file DEFAULT_CONFIG_FILE).to_h
end

def has_collection_settings?(settings_key)
confirmed_collections = collection_candidates & unregistered_settings.keys
Expand All @@ -97,6 +107,6 @@ def root_directory
end

def unregistered_settings
raw.reject { |setting, _| REGISTERED_SETTINGS.include? setting }
raw.reject { |setting, _| DEFAULT_REGISTERED_SETTINGS.include? setting }
end
end
4 changes: 2 additions & 2 deletions lib/lifer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def full_text
end

def permalink
File.join Lifer.setting(:host),
File.join Lifer.setting(:global, :host),
Lifer::URIStrategy
.find_by_name(collection.setting :uri_strategy)
.new(root: Lifer.root)
Expand All @@ -84,7 +84,7 @@ def summary
end

def title
frontmatter[:title] || Lifer.setting(:default_entry_title)
frontmatter[:title] || Lifer.setting(:entries, :default_title)
end

def to_html
Expand Down
51 changes: 48 additions & 3 deletions lib/lifer/templates/config
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
output_directory: _build
# This file provides the default configuration settings for Lifer.

title: My Lifer Weblog
description: Just another Lifer weblog, lol...
language: en
author: Admin

entries:
default_title: Untitled Entry

feed:
builder: rss
uri: feed.xml

uri_strategy: simple
root:
uri_strategy: simple

# You can optionally set a path to the layout file. If this setting is left
# unset, Lifer will use its built-in, simple-but-usable layout file instead.
#
### layout_file: path/to/my_layout.html.erb

# Collections
#
# In addition to the root collection configured above, your configuration file
# can include any number of collections. Collections use the root collections
# configuration by default, or they can have their own values.
#
### my_collection:
### title: My Collection
### description: A collection separate from the root collection.
### language: fr
### author: Benjamin
###
### feed:
### builder: rss
### uri: feed.xml
###
### uri_strategy: pretty
###
### layout_file: path/to/my_other_layout.html.erb

# Global settings
#
# These settings are special, and they're used for all of your collections and
# cannot be set per collection.
#
global:
host: https://example.com
output_directory: _build
19 changes: 19 additions & 0 deletions spec/lifer/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@
expect(subject).to end_with "lib/lifer/templates/layout.html.erb"
end
end

context "when given a nested setting name" do
subject {
config.setting :some,
:double,
:nested,
collection_name: collection.name
}

it "finds the nested setting if available" do
raw_settings_hash.merge! some: {double: {nested: "setting-value"}}

expect(subject).to eq "setting-value"
end

it "returns nil if unavailable" do
expect(subject).to be_nil
end
end
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
feed_uri: default.xml
feed:
uri: default.xml
subdirectory_one:
feed_uri: subdirectory_one.xml
feed:
uri: subdirectory_one.xml

0 comments on commit 630d187

Please sign in to comment.