Skip to content

Commit

Permalink
Merge pull request #132 from zendesk/craig/validation-error
Browse files Browse the repository at this point in the history
Enrich the hours validation experience
  • Loading branch information
craiglittle authored Oct 30, 2018
2 parents bf81765 + c7d1130 commit e61ced1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lib/biz/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def initialize

yield raw if block_given?

Validation.perform(raw)

raw.freeze

Validation.perform(self)
end

def intervals
Expand Down
24 changes: 12 additions & 12 deletions lib/biz/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
module Biz
class Validation

def self.perform(raw)
new(raw).perform
def self.perform(configuration)
new(configuration).perform
end

def initialize(raw)
@raw = raw
def initialize(configuration)
@configuration = configuration
end

def perform
RULES.each do |rule| rule.check(raw) end
RULES.each do |rule| rule.check(configuration) end

self
end

private

attr_reader :raw
attr_reader :configuration

class Rule

Expand All @@ -28,8 +28,8 @@ def initialize(message, &condition)
@condition = condition
end

def check(raw)
fail Error::Configuration, message unless condition.call(raw)
def check(configuration)
fail Error::Configuration, message if condition.call(configuration)
end

private
Expand All @@ -40,11 +40,11 @@ def check(raw)
end

RULES = [
Rule.new('hours not hash-like') { |raw|
raw.hours.respond_to?(:to_h)
Rule.new('hours not provided') { |configuration|
configuration.intervals.none?
},
Rule.new('hours not provided') { |raw|
raw.hours.to_h.any?
Rule.new('nonsensical hours provided') { |configuration|
configuration.intervals.any?(&:empty?)
}
].freeze

Expand Down
44 changes: 26 additions & 18 deletions spec/validation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
# frozen_string_literal: true

RSpec.describe Biz::Validation do
let(:raw) { Struct.new(:hours, :holidays, :time_zone).new }
let(:configuration) {
Biz::Configuration.new do |config| config.hours = hours end
}

subject(:validation) { described_class.new(raw) }
subject(:validation) { described_class.new(configuration) }

describe '.perform' do
before do raw.hours = {} end
let(:hours) { {} }

it 'performs the validation on the provided raw input' do
it 'performs the validation on the provided configuration' do
expect {
described_class.perform(raw)
described_class.perform(configuration)
}.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 do raw.hours = {mon: {'09:00' => '17:00'}} end
context 'when hours are provided' do
let(:hours) { {mon: {'09:00' => '17:00'}} }

it 'does not raise an error' do
expect { validation.perform }.not_to raise_error
end
it 'does not raise an error' do
expect { validation.perform }.not_to raise_error
end
end

describe 'and the hours are empty' do
before do raw.hours = {} end
context 'when hours are not provided' do
let(:hours) { {} }

it 'raises a configuration error' do
expect { validation.perform }.to raise_error Biz::Error::Configuration
end
it 'raises a configuration error' do
expect { validation.perform }.to raise_error Biz::Error::Configuration
end
end

context 'when nonsensical hours are provided' do
let(:hours) { {mon: {'09:00' => '09:00'}} }

it 'raises a configuration error' do
expect { validation.perform }.to raise_error Biz::Error::Configuration
end
end

describe 'when the hours are not hash-like' do
before do raw.hours = 1 end
context 'when a day with no hours is provided' do
let(:hours) { {mon: {}} }

it 'raises a configuration error' do
expect { validation.perform }.to raise_error Biz::Error::Configuration
Expand Down

0 comments on commit e61ced1

Please sign in to comment.