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
|
module Tables
|
||||||
class Distribution
|
class Distribution
|
||||||
attr_accessor :tables
|
attr_accessor :tables
|
||||||
@ -56,11 +58,7 @@ module Tables
|
|||||||
private
|
private
|
||||||
|
|
||||||
def local_discomfort(table)
|
def local_discomfort(table)
|
||||||
10 * (number_of_groups(table) - 1)
|
DiscomfortCalculator.new(table).calculate
|
||||||
end
|
|
||||||
|
|
||||||
def number_of_groups(table)
|
|
||||||
table.map(&:affinity_groups).flatten.uniq.count
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
module Tables
|
module Tables
|
||||||
RSpec.describe Distribution do
|
RSpec.describe DiscomfortCalculator do
|
||||||
describe '#local_discomfort' do
|
describe '#group_merging' do
|
||||||
let(:service) { described_class.new(min_per_table: 5, max_per_table: 5) }
|
let(:calculator) { described_class.new(table) }
|
||||||
before { service.tables = [table] }
|
|
||||||
|
|
||||||
context 'when there is just one group in the table' do
|
context 'when there is just one group in the table' do
|
||||||
let(:table) do
|
let(:table) do
|
||||||
@ -14,7 +12,7 @@ module Tables
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it { expect(service.send(:local_discomfort, table)).to eq(0) }
|
it { expect(calculator.send(:group_merging)).to eq(0) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the table contains two groups' do
|
context 'when the table contains two groups' do
|
||||||
@ -26,7 +24,7 @@ module Tables
|
|||||||
guests.each(&:save!)
|
guests.each(&:save!)
|
||||||
end
|
end
|
||||||
|
|
||||||
it { expect(service.send(:local_discomfort, table)).to eq(10) }
|
it { expect(calculator.send(:group_merging)).to eq(10) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the table contains three groups' do
|
context 'when the table contains three groups' do
|
||||||
@ -38,7 +36,7 @@ module Tables
|
|||||||
guests.each(&:save!)
|
guests.each(&:save!)
|
||||||
end
|
end
|
||||||
|
|
||||||
it { expect(service.send(:local_discomfort, table)).to eq(20) }
|
it { expect(calculator.send(:group_merging)).to eq(20) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
x
Reference in New Issue
Block a user