Skip to content

Commit

Permalink
Start making builders more configurable
Browse files Browse the repository at this point in the history
A user may or may not use all builders. This gets us closer to making
this configurable from a settings file. (But not quite.)
  • Loading branch information
benjaminwil committed Nov 13, 2023
1 parent 630d187 commit 4af2b10
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/lifer/brain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ def init(root: Dir.pwd)
def build!
brainwash!

[Lifer::Builder::HTML].each do |builder|
builder.execute root: root
# FIXME:
# Make the list of builders configurable via settings.
#
builders = [:html, :rss]

builders.each do |builder|
Lifer::Builder.find(builder).execute root: root
end
end

Expand Down
20 changes: 19 additions & 1 deletion lib/lifer/builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
module Lifer::Builder
class Lifer::Builder
class << self
def find(name)
result = descendants.detect { |descendant| descendant.name == name }

raise StandardError, "no builder with name \"%s\"" % name if result.nil?
result
end

def name
super.split("::").last.downcase.to_sym
end

private

def descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end
end

require_relative "builder/html"
Expand Down
2 changes: 1 addition & 1 deletion lib/lifer/builder/html.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "fileutils"

class Lifer::Builder::HTML
class Lifer::Builder::HTML < Lifer::Builder
class << self
def execute(root:)
Dir.chdir Lifer.output_directory do
Expand Down
2 changes: 1 addition & 1 deletion lib/lifer/builder/rss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# https://www.rssboard.org/rss-specification
#
class Lifer::Builder::RSS
class Lifer::Builder::RSS < Lifer::Builder
DEFAULT_FEED_FILENAME = "feed.xml"

class << self
Expand Down
5 changes: 5 additions & 0 deletions spec/lifer/brain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
allow(Lifer::Builder::HTML)
.to receive(:execute)
.and_return(instance_double Lifer::Builder::HTML)
allow(Lifer::Builder::RSS)
.to receive(:execute)
.and_return(instance_double Lifer::Builder::RSS)
end

it "cleans up any existing output directory" do
Expand All @@ -33,10 +36,12 @@

it "executes a build" do
allow(Lifer::Builder::HTML).to receive(:execute).with(root: root)
allow(Lifer::Builder::RSS).to receive(:execute).with(root: root)

subject

expect(Lifer::Builder::HTML).to have_received(:execute).with(root: root)
expect(Lifer::Builder::RSS).to have_received(:execute).with(root: root)
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/lifer/builder/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@
end
end
end

describe ".name" do
subject { described_class.name }

it { is_expected.to eq :html }
end
end
7 changes: 6 additions & 1 deletion spec/lifer/builder/rss_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
end
end
end
end

describe ".name" do
subject { described_class.name }

it { is_expected.to eq :rss }
end
end
28 changes: 28 additions & 0 deletions spec/lifer/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "spec_helper"

RSpec.describe Lifer::Builder do
describe ".find" do
subject { described_class.find name }

context "when a builder with the given name exists" do
let(:name) { :html }

it { is_expected.to eq Lifer::Builder::HTML }
end

context "when a builder with the given name does not exist" do
let(:name) { :doesntexist }

it "raises an error" do
expect { subject }
.to raise_error StandardError, "no builder with name \"doesntexist\""
end
end
end

describe ".name" do
subject { described_class.name }

it { is_expected.to eq :builder }
end
end

0 comments on commit 4af2b10

Please sign in to comment.