Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Add configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Mar 23, 2024
1 parent ffa4f0b commit 523cb08
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mosaik.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "forwardable"
require "yaml"

require "active_support/all"
require "zeitwerk"
Expand All @@ -16,6 +17,10 @@ def options
@options ||= Options.new
end

def configuration
@configuration ||= Configuration.from(File.join(options.directory, "mosaik.yml"))
end

def logger
@logger ||= Logger.new
end
Expand Down
23 changes: 23 additions & 0 deletions lib/mosaik/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module MOSAIK
class Configuration
attr_reader :include, :exclude

def initialize(include = [], exclude = [])
@include = include
@exclude = exclude
end

def self.from(file)
raise ConfigurationError, "Configuration file not found: #{file}" unless File.exist?(file)

configuration = YAML.load_file(file)

new(
configuration["include"],
configuration["exclude"],
)
end
end
end
3 changes: 3 additions & 0 deletions lib/mosaik/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ class UsageError < Error; end

# Raised when options are invalid
class OptionError < Error; end

# Raise when configuration is invalid
class ConfigurationError < Error; end
end
10 changes: 10 additions & 0 deletions spec/fixtures/mosaik.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# List of patterns for folder paths to include
include:
- foo
- bar

# List of patterns for folder paths to exclude
exclude:
- baz
- qux
31 changes: 31 additions & 0 deletions spec/mosaik/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

RSpec.describe MOSAIK::Configuration do
subject(:configuration) { described_class.new }

describe ".from" do
context "when the configuration file exists" do
let(:file) { "spec/fixtures/mosaik.yml" }

it "creates a new configuration instance" do
expect(described_class.from(file)).to be_a(described_class)
end

it "sets the include attribute" do
expect(described_class.from(file).include).to eq(["foo", "bar"])
end

it "sets the exclude attribute" do
expect(described_class.from(file).exclude).to eq(["baz", "qux"])
end
end

context "when the configuration file does not exist" do
let(:file) { "spec/fixtures/missing.yml" }

it "raises an error" do
expect { described_class.from(file) }.to raise_error(MOSAIK::ConfigurationError, "Configuration file not found: #{file}")
end
end
end
end

0 comments on commit 523cb08

Please sign in to comment.