Extract a DiscomfortCalculator class
This commit is contained in:
parent
69b013a717
commit
df391a4f35
22
app/services/tables/discomfort_calculator.rb
Normal file
22
app/services/tables/discomfort_calculator.rb
Normal file
@ -0,0 +1,22 @@
|
||||
module Tables
|
||||
class DiscomfortCalculator
|
||||
private attr_reader :table
|
||||
def initialize(table)
|
||||
@table = table
|
||||
end
|
||||
|
||||
def calculate
|
||||
group_merging
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def group_merging
|
||||
10 * (number_of_groups - 1)
|
||||
end
|
||||
|
||||
def number_of_groups
|
||||
table.map(&:affinity_groups).flatten.uniq.count
|
||||
end
|
||||
end
|
||||
end
|
@ -1,3 +1,5 @@
|
||||
require_relative '../../extensions/tree_node_extension'
|
||||
|
||||
module Tables
|
||||
class Distribution
|
||||
attr_accessor :tables
|
||||
@ -56,11 +58,7 @@ module Tables
|
||||
private
|
||||
|
||||
def local_discomfort(table)
|
||||
10 * (number_of_groups(table) - 1)
|
||||
end
|
||||
|
||||
def number_of_groups(table)
|
||||
table.map(&:affinity_groups).flatten.uniq.count
|
||||
DiscomfortCalculator.new(table).calculate
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,10 +1,8 @@
|
||||
require 'rails_helper'
|
||||
|
||||
module Tables
|
||||
RSpec.describe Distribution do
|
||||
describe '#local_discomfort' do
|
||||
let(:service) { described_class.new(min_per_table: 5, max_per_table: 5) }
|
||||
before { service.tables = [table] }
|
||||
RSpec.describe DiscomfortCalculator do
|
||||
describe '#group_merging' do
|
||||
let(:calculator) { described_class.new(table) }
|
||||
|
||||
context 'when there is just one group in the table' do
|
||||
let(:table) do
|
||||
@ -14,7 +12,7 @@ module Tables
|
||||
end
|
||||
end
|
||||
|
||||
it { expect(service.send(:local_discomfort, table)).to eq(0) }
|
||||
it { expect(calculator.send(:group_merging)).to eq(0) }
|
||||
end
|
||||
|
||||
context 'when the table contains two groups' do
|
||||
@ -26,7 +24,7 @@ module Tables
|
||||
guests.each(&:save!)
|
||||
end
|
||||
|
||||
it { expect(service.send(:local_discomfort, table)).to eq(10) }
|
||||
it { expect(calculator.send(:group_merging)).to eq(10) }
|
||||
end
|
||||
|
||||
context 'when the table contains three groups' do
|
||||
@ -38,7 +36,7 @@ module Tables
|
||||
guests.each(&:save!)
|
||||
end
|
||||
|
||||
it { expect(service.send(:local_discomfort, table)).to eq(20) }
|
||||
it { expect(calculator.send(:group_merging)).to eq(20) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user