Extract a DiscomfortCalculator class

This commit is contained in:
Manuel Bustillo 2024-07-31 22:53:19 +02:00
parent 69b013a717
commit df391a4f35
3 changed files with 31 additions and 13 deletions

View 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

View File

@ -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

View File

@ -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