Module: Validatable
- Included in:
- Hash
- Defined in:
- lib/hash_validations.rb
Defined Under Namespace
Classes: Errors
Class Method Summary collapse
-
.included(klass) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#valid_for_group?(group) ⇒ Boolean
:nodoc:.
-
#validate_only(key) ⇒ Object
call-seq: validate_only(key).
Class Method Details
.included(klass) ⇒ Object
:nodoc:
5 6 7 |
# File 'lib/hash_validations.rb', line 5 def self.included(klass) #:nodoc: #ignore end |
Instance Method Details
#valid_for_group?(group) ⇒ Boolean
:nodoc:
9 10 11 12 13 14 15 |
# File 'lib/hash_validations.rb', line 9 def valid_for_group?(group) #:nodoc: run_before_validations errors.clear self.validate_children(self, group) self.validate(group) errors.empty? end |
#validate_only(key) ⇒ Object
call-seq: validate_only(key)
Only executes a specified validation. The argument should follow a pattern based on the key of the validation.
Examples:
* validates_presence_of :name can be run with obj.validate_only("presence_of/name")
* validates_presence_of :birthday, :key => "a key" can be run with obj.validate_only("presence_of/a key")
23 24 25 26 27 28 29 30 31 |
# File 'lib/hash_validations.rb', line 23 def validate_only(key) validation_name, attribute_name = key.split("/") validation_name = validation_name.split("_").collect{|word| word.capitalize}.join validation_key = "#{self.name}/Validatable::Validates#{validation_name}/#{attribute_name}" validation = self.all_validations.find { |validation| validation.key == validation_key } raise ArgumentError.new("validation with key #{validation_key} could not be found") if validation.nil? errors.clear run_validation(validation) end |