Class: ActiveValidation::Decorators::ConsistentRegistry
- Inherits:
-
ActiveValidation::Decorator
- Object
- ActiveValidation::Decorator
- ActiveValidation::Decorators::ConsistentRegistry
- Defined in:
- lib/active_validation/decorators/consistent_registry.rb
Instance Attribute Summary collapse
-
#klass ⇒ Class
readonly
All items in the registry must be ancestors of this Class.
Instance Method Summary collapse
- #find_or_add(name, &block) ⇒ Object
- #find_or_build(name, &block) ⇒ Object
-
#initialize(klass, registry) ⇒ ConsistentRegistry
constructor
A new instance of ConsistentRegistry.
- #register(name, item) ⇒ Object
Methods inherited from ActiveValidation::Decorator
const_missing, #method_missing, #respond_to_missing?, #send
Constructor Details
#initialize(klass, registry) ⇒ ConsistentRegistry
Returns a new instance of ConsistentRegistry.
10 11 12 13 14 |
# File 'lib/active_validation/decorators/consistent_registry.rb', line 10 def initialize(klass, registry) super registry @klass = klass.is_a?(Class) ? klass : klass.to_s.constantize end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveValidation::Decorator
Instance Attribute Details
#klass ⇒ Class (readonly)
All items in the registry must be ancestors of this Class
8 9 10 |
# File 'lib/active_validation/decorators/consistent_registry.rb', line 8 def klass @klass end |
Instance Method Details
#find_or_add(name, &block) ⇒ Object
24 25 26 27 |
# File 'lib/active_validation/decorators/consistent_registry.rb', line 24 def find_or_add(name, &block) record = find_or_build(name, &block) registered?(name) ? record : register(name, record) end |
#find_or_build(name, &block) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/active_validation/decorators/consistent_registry.rb', line 16 def find_or_build(name, &block) return klass.new(name, &block) unless registered?(name) found = find(name) found.instance_exec(found, &block) if block found end |
#register(name, item) ⇒ Object
29 30 31 32 33 |
# File 'lib/active_validation/decorators/consistent_registry.rb', line 29 def register(name, item) raise Errors::InconsistentRegistryError unless item.is_a? klass super end |