A registry configuration.
Access this API by calling the configuration method of a registry. Conceptually, the configuration and the registry are just two windows (APIs) into the same object.
Once objects are added to the registry, the configuration is locked and cannot be modified. Informational methods may still be called.
Add a category type.
You must provide a category type name, a pattern that recognizes tuples that should trigger this category, and an array of indexes into the pattern that indicates which tuple element(s) will identify individual categories within this category type.
# File lib/idregistry/configuration.rb, line 347 def add_category(category_, pattern_, indexes_=[]) @mutex.synchronize do raise ConfigurationLockedError if @locked if @categories.has_key?(category_) raise IllegalConfigurationError, "Category already exists" end @categories[category_] = [pattern_, indexes_] end self end
Add a convenience method, providing a short cut for doing lookups in the registry. You must provide a pattern that serves as a tuple template, and an array of indexes. The method will take a number of arguments corresponding to that array, and the indexes will then be used as indexes into the pattern, replacing pattern elements to generate the actual tuple to be looked up.
# File lib/idregistry/configuration.rb, line 377 def add_convenience_method(name_, pattern_, indexes_) @mutex.synchronize do raise ConfigurationLockedError if @locked name_ = name_.to_sym if @methods.has_key?(name_) raise IllegalConfigurationError, "Factory method already exists" end @methods[name_] = [pattern_, indexes_] end self end
Add a pattern to the configuration.
You may use one of the following call sequences:
add_pattern( pattern ) { ... }
Add a simple pattern, using the given block to generate objects matching that pattern.
add_pattern( pattern, to_generate_object )
Add a simple pattern, using the given proc to generate objects matching that pattern.
add_pattern( pattern, type, to_generate_object, to_generate_tuple )
Add a pattern for the given type. You should provide both a proc to generate objects, and a proc to generate a tuple from an object.
add_pattern() { ... }
Utilize a PatternAdder DSL to define the pattern.
# File lib/idregistry/configuration.rb, line 270 def add_pattern(*args_, &block_) raise ConfigurationLockedError if @locked if block_ case args_.size when 0 adder_ = PatternAdder._new(nil, nil, nil, nil) ::Blockenspiel.invoke(block_, adder_) when 1 adder_ = PatternAdder._new(args_[0], nil, block_, nil) else raise IllegalConfigurationError, "Did not recognize call sequence for add_pattern" end else case args_.size when 2, 3 adder_ = PatternAdder._new(args_[0], nil, args_[1], args_[2]) when 4 adder_ = PatternAdder._new(args_[0], args_[1], args_[2], args_[3]) else raise IllegalConfigurationError, "Did not recognize call sequence for add_pattern" end end pattern_ = adder_.pattern type_ = adder_.type || AnonymousType.new gen_obj_ = adder_.to_generate_object gen_tuple_ = adder_.to_generate_tuple @mutex.synchronize do raise ConfigurationLockedError if @locked if @patterns.has_key?(pattern_) raise IllegalConfigurationError, "Pattern already exists" end @patterns[pattern_] = [type_, gen_obj_, gen_tuple_] (@types[type_] ||= []) << pattern_ end self end
Returns an array of all category types known by this configuration.
# File lib/idregistry/configuration.rb, line 176 def all_categories @mutex.synchronize do @categories.keys end end
Returns an array of all convenience method names known by this configuration.
# File lib/idregistry/configuration.rb, line 186 def all_convenience_methods @mutex.synchronize do @methods.keys end end
Returns an array of all patterns known by this configuration.
The pattern arrays will be duplicates of the actual arrays stored internally, so you cannot modify patterns in place.
# File lib/idregistry/configuration.rb, line 155 def all_patterns @mutex.synchronize do @patterns.keys.map{ |a_| a_.dup } end end
Returns an array of all object types known by this configuration.
Does not include any “anonymous” types that are automatically generated if you add a pattern without a type.
# File lib/idregistry/configuration.rb, line 167 def all_types @mutex.synchronize do @types.keys.find_all{ |t_| !t_.is_a?(AnonymousType) } end end
Clear all configuration information, including all object types, patterns, categories, and convenience methods.
# File lib/idregistry/configuration.rb, line 404 def clear @mutex.synchronize do raise ConfigurationLockedError if @locked @patterns.clear @types.clear @categories.clear @methods.clear end self end
Remove a category type by name.
# File lib/idregistry/configuration.rb, line 361 def delete_category(category_) @mutex.synchronize do raise ConfigurationLockedError if @locked @categories.delete(category_) end self end
Delete a convenience method by name.
# File lib/idregistry/configuration.rb, line 392 def delete_convenience_method(name_) @mutex.synchronize do raise ConfigurationLockedError if @locked @methods.delete(name_.to_sym) end self end
Remove the given pattern from this configuration. Automatically removes the object type if this is the object type’s only remaining pattern.
# File lib/idregistry/configuration.rb, line 312 def delete_pattern(pattern_) @mutex.synchronize do raise ConfigurationLockedError if @locked if (patdata_ = @patterns.delete(pattern_)) type_ = patdata_[0] typedata_ = @types[type_] typedata_.delete(pattern_) @types.delete(type_) if typedata_.empty? end end self end
Remove the given object type from this configuration. Automatically removes all patterns associated with this object type.
# File lib/idregistry/configuration.rb, line 329 def delete_type(type_) @mutex.synchronize do raise ConfigurationLockedError if @locked if (typedata_ = @types.delete(type_)) typedata_.each{ |pat_| @patterns.delete(pat_) } end end self end
Returns true if this configuration includes the given category type.
# File lib/idregistry/configuration.rb, line 213 def has_category?(category_) @mutex.synchronize do @categories.has_key?(category_) end end
Returns true if this configuration includes the given convenience method.
# File lib/idregistry/configuration.rb, line 222 def has_convenience_method?(method_) @mutex.synchronize do @methods.has_key?(method_) end end
Returns true if this configuration includes the given pattern.
# File lib/idregistry/configuration.rb, line 195 def has_pattern?(pattern_) @mutex.synchronize do @patterns.has_key?(pattern_) end end
Returns true if this configuration includes the given object type.
# File lib/idregistry/configuration.rb, line 204 def has_type?(type_) @mutex.synchronize do @types.has_key?(type_) end end
Lock the configuration, preventing further changes.
This is called by registries when you start using them.
In addition, it is cheap to spawn another registry from a configuration that is locked, because the configuration internals can be reused. Therefore, you should lock a configuration if you want to use it as a template to create empty registries quickly (using the #spawn_registry call).
# File lib/idregistry/configuration.rb, line 142 def lock @mutex.synchronize do @locked = true end self end
Returns true if this configuration has been locked. A locked configuration can no longer be modified. Registries lock their configurations once you start using them.
# File lib/idregistry/configuration.rb, line 85 def locked? @locked end
Returns an array of patterns corresponding to the given object type. Returns the empty array if the given object type is not recognized.
# File lib/idregistry/configuration.rb, line 243 def patterns_for_type(type_) @mutex.synchronize do typedata_ = @types[type_] typedata_ ? typedata_.dup : [] end end
Returns the registry that owns this configuration.
# File lib/idregistry/configuration.rb, line 92 def registry @registry end
Create a new empty registry, duplicating this configuration.
If the :unlocked
option is set to true, the new registry will
have an unlocked configuration that can be modified further. Otherwise, the
new registry’s configuration will be locked.
Spawning a locked registry from a locked configuration is very fast because it reuses the configuration objects.
# File lib/idregistry/configuration.rb, line 106 def spawn_registry(opts_={}) request_unlocked_ = opts_[:unlocked] if @locked && !request_unlocked_ reg_ = Registry._new(@patterns, @types, @categories, @methods) reg_.config.lock else patterns_ = {} types_ = {} categories_ = {} methods_ = {} @mutex.synchronize do @patterns.each{ |k_, v_| patterns_[k_] = v_.dup } @types.each{ |k_, v_| types_[k_] = v_.dup } @categories.each{ |k_, v_| categories_[k_] = v_.dup } @methods.each{ |k_, v_| methods_[k_] = v_.dup } end reg_ = Registry._new(patterns_, types_, categories_, methods_) reg_.config.lock unless request_unlocked_ end reg_ end
Returns the object type corresponding to the given pattern. Returns nil if the given pattern is not recognized.
# File lib/idregistry/configuration.rb, line 232 def type_for_pattern(pattern_) @mutex.synchronize do patdata_ = @patterns[pattern_] patdata_ ? patdata_[0] : nil end end