From 67a40725104ce91e0e65eea1826c812d507a6ae9 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Wed, 21 Aug 2024 10:47:35 -0700 Subject: [PATCH] add thread safety around modifying internal class variables --- CHANGELOG.md | 6 ++++++ VERSION | 2 +- lib/support_table_data.rb | 30 ++++++++++++++++++------------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bc9941..acf6d02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 1.2.2 + +### Fixed + +- Added thread safety to modification of internal class variables. + ## 1.2.1 ### Changed diff --git a/VERSION b/VERSION index 6085e94..23aa839 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.1 +1.2.2 diff --git a/lib/support_table_data.rb b/lib/support_table_data.rb index 7c52f05..a706e5c 100644 --- a/lib/support_table_data.rb +++ b/lib/support_table_data.rb @@ -10,6 +10,7 @@ module SupportTableData included do # Internal variables used for memoization. + @mutex = Mutex.new @support_table_data_files = [] @support_table_attribute_helpers = {} @support_table_instance_names = {} @@ -76,7 +77,10 @@ def sync_table_data! # @return [void] def add_support_table_data(data_file_path) root_dir = (support_table_data_directory || SupportTableData.data_directory || Dir.pwd) - @support_table_data_files << File.expand_path(data_file_path, root_dir) + @mutex.synchronize do + @support_table_data_files += [File.expand_path(data_file_path, root_dir)] + @support_table_instance_keys = nil + end define_support_table_named_instances end @@ -88,8 +92,10 @@ def add_support_table_data(data_file_path) # @param attributes [String, Symbol] The names of the attributes to add helper methods for. # @return [void] def named_instance_attribute_helpers(*attributes) - attributes.flatten.collect(&:to_s).each do |attribute| - @support_table_attribute_helpers[attribute] = [] + @mutex.synchronize do + attributes.flatten.collect(&:to_s).each do |attribute| + @support_table_attribute_helpers = @support_table_attribute_helpers.merge(attribute => []) + end end define_support_table_named_instances end @@ -207,7 +213,9 @@ def define_support_table_named_instances next unless data.is_a?(Hash) data.each do |name, attributes| - define_support_table_named_instance_methods(name, attributes) + @mutex.synchronize do + define_support_table_named_instance_methods(name, attributes) + end end end end @@ -230,17 +238,15 @@ def define_support_table_named_instance_methods(name, attributes) unless @support_table_instance_names.include?(method_name) define_support_table_instance_helper(method_name, key_attribute, key_value) define_support_table_predicates_helper("#{method_name}?", key_attribute, key_value) - @support_table_instance_names[method_name] = key_value + @support_table_instance_names = @support_table_instance_names.merge(method_name => key_value) end - if defined?(@support_table_attribute_helpers) - @support_table_attribute_helpers.each do |attribute_name, defined_methods| - attribute_method_name = "#{method_name}_#{attribute_name}" - next if defined_methods.include?(attribute_method_name) + @support_table_attribute_helpers.each do |attribute_name, defined_methods| + attribute_method_name = "#{method_name}_#{attribute_name}" + next if defined_methods.include?(attribute_method_name) - define_support_table_instance_attribute_helper(attribute_method_name, attributes[attribute_name]) - defined_methods << attribute_method_name - end + define_support_table_instance_attribute_helper(attribute_method_name, attributes[attribute_name]) + defined_methods << attribute_method_name end end