Module: DataMapper::Validations::Context
- Included in:
- DataMapper::Validations
- Defined in:
- lib/dm-validations/context.rb
Overview
Module with validation context functionality.
Contexts are implemented using a thread-local array-based stack.
Class Method Summary collapse
-
.any?(&block) ⇒ Boolean
Are there any contexts on the stack?.
-
.current ⇒ Symbol, NilClass
Get the current validation context or nil (if no context is on the stack).
-
.in_context(context) ⇒ Object
Execute a block of code within a specific validation context.
-
.stack ⇒ Object
private
The (thread-local) validation context stack This allows object graphs to be saved within potentially nested contexts without having to pass the validation context throughout.
Instance Method Summary collapse
-
#default_validation_context ⇒ Symbol
The default validation context for this Resource.
Class Method Details
.any?(&block) ⇒ Boolean
Are there any contexts on the stack?
36 37 38 |
# File 'lib/dm-validations/context.rb', line 36 def self.any?(&block) stack.any?(&block) end |
.current ⇒ Symbol, NilClass
Get the current validation context or nil (if no context is on the stack).
26 27 28 |
# File 'lib/dm-validations/context.rb', line 26 def self.current stack.last end |
.in_context(context) ⇒ Object
Execute a block of code within a specific validation context
14 15 16 17 18 19 |
# File 'lib/dm-validations/context.rb', line 14 def self.in_context(context) stack << context yield ensure stack.pop end |
.stack ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The (thread-local) validation context stack This allows object graphs to be saved within potentially nested contexts without having to pass the validation context throughout
45 46 47 |
# File 'lib/dm-validations/context.rb', line 45 def self.stack Thread.current[:dm_validations_context_stack] ||= [] end |
Instance Method Details
#default_validation_context ⇒ Symbol
The default validation context for this Resource. This Resource’s default context can be overridden by implementing #default_validation_context
58 59 60 |
# File 'lib/dm-validations/context.rb', line 58 def default_validation_context model.validators.current_context || :default end |