-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When configuring the gem, we'll now throw errors when the hours configuration: * is not hash-like. * does not contain any blocks of hours.
- Loading branch information
1 parent
a9cf942
commit 7d4398e
Showing
7 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Biz | ||
class Error < StandardError | ||
|
||
Configuration = Class.new(self) | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module Biz | ||
class Validation | ||
|
||
def self.perform(raw) | ||
new(raw).perform | ||
end | ||
|
||
def initialize(raw) | ||
@raw = raw | ||
end | ||
|
||
def perform | ||
RULES.each do |rule| rule.check(raw) end | ||
|
||
self | ||
end | ||
|
||
protected | ||
|
||
attr_reader :raw | ||
|
||
class Rule | ||
|
||
def initialize(message, &condition) | ||
@message = message | ||
@condition = condition | ||
end | ||
|
||
def check(raw) | ||
fail Error::Configuration, message unless condition.call(raw) | ||
end | ||
|
||
protected | ||
|
||
attr_reader :message, | ||
:condition | ||
|
||
end | ||
|
||
RULES = Set.new([ | ||
Rule.new('Hours must be hash-like.') { |raw| | ||
raw.hours.respond_to?(:to_h) | ||
}, | ||
Rule.new('Hours must be provided.') { |raw| | ||
raw.hours.to_h.any? | ||
} | ||
]) | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
RSpec.describe Biz::Error do | ||
it 'is a standard error' do | ||
expect(described_class.new.is_a?(StandardError)).to eq true | ||
end | ||
|
||
describe Biz::Error::Configuration do | ||
it "is a 'biz' error" do | ||
expect(described_class.new.is_a?(Biz::Error)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
RSpec.describe Biz::Validation do | ||
let(:raw) { Struct.new(:hours, :holidays, :time_zone).new } | ||
|
||
subject(:validation) { described_class.new(raw) } | ||
|
||
describe '.perform' do | ||
before { raw.hours = {} } | ||
|
||
it 'performs the validation on the provided raw input' do | ||
expect { described_class.perform(raw) }.to raise_error( | ||
Biz::Error::Configuration | ||
) | ||
end | ||
end | ||
|
||
describe '#perform' do | ||
describe 'when the hours are hash-like' do | ||
describe 'and the hours are not empty' do | ||
before { raw.hours = {mon: {'09:00' => '17:00'}} } | ||
|
||
it 'does not raise an error' do | ||
expect { validation.perform }.not_to raise_error | ||
end | ||
end | ||
|
||
describe 'and the hours are empty' do | ||
before { raw.hours = {} } | ||
|
||
it 'raises a configuration error' do | ||
expect { validation.perform }.to raise_error Biz::Error::Configuration | ||
end | ||
end | ||
end | ||
|
||
describe 'when the hours are not hash-like' do | ||
before { raw.hours = 1 } | ||
|
||
it 'raises a configuration error' do | ||
expect { validation.perform }.to raise_error Biz::Error::Configuration | ||
end | ||
end | ||
end | ||
end |