From 1d28cfc527dd5311494c2a9b9cfe77766d1195f4 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Fri, 2 Aug 2024 17:37:50 +0200 Subject: [PATCH] Refactor how the local discomfort is stored --- app/services/tables/distribution.rb | 12 ++---------- app/services/tables/table.rb | 11 ++++++++--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/services/tables/distribution.rb b/app/services/tables/distribution.rb index 33cb130..4965346 100644 --- a/app/services/tables/distribution.rb +++ b/app/services/tables/distribution.rb @@ -17,9 +17,7 @@ module Tables end def discomfort - @tables.map do |table| - local_discomfort(table) - end.sum + @tables.map(&:discomfort).sum end def inspect @@ -28,7 +26,7 @@ module Tables def pretty_print @tables.map.with_index do |table, i| - "Table #{i + 1} (#{table.count} ppl): (#{local_discomfort(table)}) #{table.map(&:full_name).join(', ')}" + "Table #{i + 1} (#{table.count} ppl): (#{table.discomfort}) #{table.map(&:full_name).join(', ')}" end.join("\n") end @@ -55,11 +53,5 @@ module Tables arrangement.update!(discomfort:) end end - - private - - def local_discomfort(table) - table.discomfort ||= DiscomfortCalculator.new(table).calculate - end end end diff --git a/app/services/tables/table.rb b/app/services/tables/table.rb index 1dde7f6..253c200 100644 --- a/app/services/tables/table.rb +++ b/app/services/tables/table.rb @@ -1,15 +1,20 @@ module Tables class Table < Array - attr_accessor :discomfort + attr_writer :discomfort + def initialize(*args) super reset end def reset - original_discomfort = discomfort + original_discomfort = @discomfort @discomfort = nil original_discomfort end + + def discomfort + @discomfort ||= DiscomfortCalculator.new(self).calculate + end end -end \ No newline at end of file +end