Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread safety around modifying internal class variables #9

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1
1.2.2
30 changes: 18 additions & 12 deletions lib/support_table_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down