Skip to content

Commit

Permalink
Add basic hours validations
Browse files Browse the repository at this point in the history
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
craiglittle committed Oct 1, 2015
1 parent a9cf942 commit 7d4398e
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/biz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def schedule
require 'biz/day_of_week'
require 'biz/day_time'
require 'biz/duration'
require 'biz/error'
require 'biz/holiday'
require 'biz/interval'
require 'biz/periods'
Expand All @@ -59,4 +60,5 @@ def schedule
require 'biz/time_segment'
require 'biz/week'
require 'biz/week_time'
require 'biz/validation'
require 'biz/version'
2 changes: 2 additions & 0 deletions lib/biz/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Configuration

def initialize
@raw = Raw.new.tap do |raw| yield raw if block_given? end

Validation.perform(raw)
end

def intervals
Expand Down
7 changes: 7 additions & 0 deletions lib/biz/error.rb
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
50 changes: 50 additions & 0 deletions lib/biz/validation.rb
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
12 changes: 12 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
end
end

context 'when initialized with an invalid configuration' do
it 'raises a configuration error' do
expect {
Biz::Configuration.new do |config|
config.hours = {}
config.holidays = holidays
config.time_zone = time_zone
end
}.to raise_error Biz::Error::Configuration
end
end

describe '#intervals' do
context 'when left unconfigured' do
subject(:configuration) {
Expand Down
11 changes: 11 additions & 0 deletions spec/error_spec.rb
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
43 changes: 43 additions & 0 deletions spec/validation_spec.rb
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

0 comments on commit 7d4398e

Please sign in to comment.