Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic hours validations #34

Merged
merged 1 commit into from
Oct 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)).to eq true
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