Class: IDRegistry::Configuration
- Inherits:
-
Object
- Object
- IDRegistry::Configuration
- Includes:
- Blockenspiel::DSL
- Defined in:
- lib/idregistry/configuration.rb
Overview
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.
Defined Under Namespace
Classes: AnonymousType
Instance Method Summary collapse
-
#add_category(category_, pattern_, indexes_ = []) ⇒ Object
Add a category type.
-
#add_convenience_method(name_, pattern_, indexes_) ⇒ Object
Add a convenience method, providing a short cut for doing lookups in the registry.
-
#add_pattern(*args_, &block_) ⇒ Object
Add a pattern to the configuration.
-
#all_categories ⇒ Object
Returns an array of all category types known by this configuration.
-
#all_convenience_methods ⇒ Object
Returns an array of all convenience method names known by this configuration.
-
#all_patterns ⇒ Object
Returns an array of all patterns known by this configuration.
-
#all_types ⇒ Object
Returns an array of all object types known by this configuration.
-
#clear ⇒ Object
Clear all configuration information, including all object types, patterns, categories, and convenience methods.
-
#delete_category(category_) ⇒ Object
Remove a category type by name.
-
#delete_convenience_method(name_) ⇒ Object
Delete a convenience method by name.
-
#delete_pattern(pattern_) ⇒ Object
Remove the given pattern from this configuration.
-
#delete_type(type_) ⇒ Object
Remove the given object type from this configuration.
-
#has_category?(category_) ⇒ Boolean
Returns true if this configuration includes the given category type.
-
#has_convenience_method?(method_) ⇒ Boolean
Returns true if this configuration includes the given convenience method.
-
#has_pattern?(pattern_) ⇒ Boolean
Returns true if this configuration includes the given pattern.
-
#has_type?(type_) ⇒ Boolean
Returns true if this configuration includes the given object type.
-
#initialize(registry_, patterns_, types_, categories_, methods_) ⇒ Configuration
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#lock ⇒ Object
Lock the configuration, preventing further changes.
-
#locked? ⇒ Boolean
Returns true if this configuration has been locked.
-
#patterns_for_type(type_) ⇒ Object
Returns an array of patterns corresponding to the given object type.
-
#registry ⇒ Object
Returns the registry that owns this configuration.
-
#spawn_registry(opts_ = {}) ⇒ Object
Create a new empty registry, duplicating this configuration.
-
#type_for_pattern(pattern_) ⇒ Object
Returns the object type corresponding to the given pattern.
Constructor Details
#initialize(registry_, patterns_, types_, categories_, methods_) ⇒ Configuration
:nodoc:
65 66 67 68 69 70 71 72 73 |
# File 'lib/idregistry/configuration.rb', line 65 def initialize(registry_, patterns_, types_, categories_, methods_) # :nodoc: @registry = registry_ @patterns = patterns_ @types = types_ @categories = categories_ @methods = methods_ @locked = false @mutex = ::Mutex.new end |
Instance Method Details
#add_category(category_, pattern_, indexes_ = []) ⇒ Object
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.
347 348 349 350 351 352 353 354 355 356 |
# 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_convenience_method(name_, pattern_, indexes_) ⇒ Object
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.
377 378 379 380 381 382 383 384 385 386 387 |
# 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_pattern(*args_, &block_) ⇒ Object
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.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# 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 |
#all_categories ⇒ Object
Returns an array of all category types known by this configuration.
176 177 178 179 180 |
# File 'lib/idregistry/configuration.rb', line 176 def all_categories @mutex.synchronize do @categories.keys end end |
#all_convenience_methods ⇒ Object
Returns an array of all convenience method names known by this configuration.
186 187 188 189 190 |
# File 'lib/idregistry/configuration.rb', line 186 def all_convenience_methods @mutex.synchronize do @methods.keys end end |
#all_patterns ⇒ Object
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.
155 156 157 158 159 |
# File 'lib/idregistry/configuration.rb', line 155 def all_patterns @mutex.synchronize do @patterns.keys.map{ |a_| a_.dup } end end |
#all_types ⇒ Object
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.
167 168 169 170 171 |
# 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 ⇒ Object
Clear all configuration information, including all object types, patterns, categories, and convenience methods.
404 405 406 407 408 409 410 411 412 413 |
# 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 |
#delete_category(category_) ⇒ Object
Remove a category type by name.
361 362 363 364 365 366 367 |
# 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_convenience_method(name_) ⇒ Object
Delete a convenience method by name.
392 393 394 395 396 397 398 |
# 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 |
#delete_pattern(pattern_) ⇒ Object
Remove the given pattern from this configuration. Automatically removes the object type if this is the object type’s only remaining pattern.
312 313 314 315 316 317 318 319 320 321 322 323 |
# 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 |
#delete_type(type_) ⇒ Object
Remove the given object type from this configuration. Automatically removes all patterns associated with this object type.
329 330 331 332 333 334 335 336 337 |
# 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 |
#has_category?(category_) ⇒ Boolean
Returns true if this configuration includes the given category type.
213 214 215 216 217 |
# File 'lib/idregistry/configuration.rb', line 213 def has_category?(category_) @mutex.synchronize do @categories.has_key?(category_) end end |
#has_convenience_method?(method_) ⇒ Boolean
Returns true if this configuration includes the given convenience method.
222 223 224 225 226 |
# File 'lib/idregistry/configuration.rb', line 222 def has_convenience_method?(method_) @mutex.synchronize do @methods.has_key?(method_) end end |
#has_pattern?(pattern_) ⇒ Boolean
Returns true if this configuration includes the given pattern.
195 196 197 198 199 |
# File 'lib/idregistry/configuration.rb', line 195 def has_pattern?(pattern_) @mutex.synchronize do @patterns.has_key?(pattern_) end end |
#has_type?(type_) ⇒ Boolean
Returns true if this configuration includes the given object type.
204 205 206 207 208 |
# File 'lib/idregistry/configuration.rb', line 204 def has_type?(type_) @mutex.synchronize do @types.has_key?(type_) end end |
#inspect ⇒ Object
:nodoc:
76 77 78 |
# File 'lib/idregistry/configuration.rb', line 76 def inspect # :nodoc: "#<#{self.class}:0x#{object_id.to_s(16)}>" end |
#lock ⇒ Object
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).
142 143 144 145 146 147 |
# File 'lib/idregistry/configuration.rb', line 142 def lock @mutex.synchronize do @locked = true end self end |
#locked? ⇒ Boolean
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.
85 86 87 |
# File 'lib/idregistry/configuration.rb', line 85 def locked? @locked end |
#patterns_for_type(type_) ⇒ Object
Returns an array of patterns corresponding to the given object type. Returns the empty array if the given object type is not recognized.
243 244 245 246 247 248 |
# File 'lib/idregistry/configuration.rb', line 243 def patterns_for_type(type_) @mutex.synchronize do typedata_ = @types[type_] typedata_ ? typedata_.dup : [] end end |
#registry ⇒ Object
Returns the registry that owns this configuration.
92 93 94 |
# File 'lib/idregistry/configuration.rb', line 92 def registry @registry end |
#spawn_registry(opts_ = {}) ⇒ Object
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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# 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 |
#type_for_pattern(pattern_) ⇒ Object
Returns the object type corresponding to the given pattern. Returns nil if the given pattern is not recognized.
232 233 234 235 236 237 |
# File 'lib/idregistry/configuration.rb', line 232 def type_for_pattern(pattern_) @mutex.synchronize do patdata_ = @patterns[pattern_] patdata_ ? patdata_[0] : nil end end |