From df391a4f35e0435a7ef859b52209202da76309f4 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Wed, 31 Jul 2024 22:53:19 +0200 Subject: [PATCH] Extract a DiscomfortCalculator class --- app/services/tables/discomfort_calculator.rb | 22 +++++++++++++++++++ app/services/tables/distribution.rb | 8 +++---- ..._spec.rb => discomfort_calculator_spec.rb} | 14 +++++------- 3 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 app/services/tables/discomfort_calculator.rb rename spec/services/tables/{distribution_spec.rb => discomfort_calculator_spec.rb} (70%) diff --git a/app/services/tables/discomfort_calculator.rb b/app/services/tables/discomfort_calculator.rb new file mode 100644 index 0000000..3ba39e7 --- /dev/null +++ b/app/services/tables/discomfort_calculator.rb @@ -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 diff --git a/app/services/tables/distribution.rb b/app/services/tables/distribution.rb index d94d690..d10d73f 100644 --- a/app/services/tables/distribution.rb +++ b/app/services/tables/distribution.rb @@ -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 diff --git a/spec/services/tables/distribution_spec.rb b/spec/services/tables/discomfort_calculator_spec.rb similarity index 70% rename from spec/services/tables/distribution_spec.rb rename to spec/services/tables/discomfort_calculator_spec.rb index e9aca83..c997ac2 100644 --- a/spec/services/tables/distribution_spec.rb +++ b/spec/services/tables/discomfort_calculator_spec.rb @@ -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