Provides a data structure for collecting homogeneous objects and indexing them for quick lookup.
Ruby 3.0+
Add this line to your application's Gemfile:
gem 'registry'And then execute:
bundleOr install it yourself as:
gem install registryPerson = Struct.new(:id, :name, :email)
registry = Registry.new([
Person.new(1, 'Dale', 'jason@twilightcoders.net'),
Person.new(2, 'Dale', 'dale@twilightcoders.net')
])
registry.index(:name)
# Find items using where method
results = registry.where(name: 'Dale')
# Check if items exist
registry.exists?(name: 'Dale') #=> true
# Automatic reindexing when attributes change
d = registry.where(name: 'Dale').first
d.name = "Jason"
registry.where(name: 'Jason') # Contains the updated item# Create a thread-safe registry
registry = Registry.new(items, thread_safe: true)# Clean up method watching for long-lived registries
registry.cleanup!begin
registry.where(nonexistent_index: 'value')
rescue Registry::IndexNotFound => e
puts "Index not found: #{e.message}"
end
begin
registry.add("invalid item")
rescue Registry::MissingAttributeError => e
puts "Missing required attributes: #{e.message}"
endAfter checking out the repo, run bundle to install dependencies. Then, run bundle exec rspec to run the tests.
Bug reports and pull requests are welcome on GitHub at https://github.com/TwilightCoders/active_registry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.