Module: Stretchy::Utils::Validation
- Included in:
- Boosts::Base, Filters::Base, Queries::Base, Types::Base, Logger
- Defined in:
- lib/stretchy/utils/validation.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #errors ⇒ Object
- #initialize(attributes = nil) ⇒ Object
- #require_one!(*attrs) ⇒ Object
- #require_only_one!(*attrs) ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
- #validator ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 |
# File 'lib/stretchy/utils/validation.rb', line 5 def self.included(base) base.send(:include, Virtus.model) base.extend(ClassMethods) end |
Instance Method Details
#errors ⇒ Object
55 56 57 |
# File 'lib/stretchy/utils/validation.rb', line 55 def errors validator.errors end |
#initialize(attributes = nil) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/stretchy/utils/validation.rb', line 10 def initialize(attributes = nil) self.class.attribute_set.set(self, attributes) if attributes set_default_attributes validate! after_initialize if respond_to?(:after_initialize) end |
#require_one!(*attrs) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/stretchy/utils/validation.rb', line 25 def require_one!(*attrs) rule = ::Validation::Rule::Required.new errors = {} unless attrs.any? {|a| rule.valid_value?( send(a) ) } raise Errors::ValidationError.new( attrs.join(', ') => { rule: :require_one_of } ) end true end |
#require_only_one!(*attrs) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/stretchy/utils/validation.rb', line 38 def require_only_one!(*attrs) rule = ::Validation::Rule::Required.new errors = {} if attrs.select {|a| rule.valid_value?( send(a) ) }.count > 1 raise Errors::ValidationError.new( attrs.join(', ') => { rule: :require_only_one } ) end true end |
#valid? ⇒ Boolean
51 52 53 |
# File 'lib/stretchy/utils/validation.rb', line 51 def valid? validator.valid? end |
#validate! ⇒ Object
21 22 23 |
# File 'lib/stretchy/utils/validation.rb', line 21 def validate! raise Errors::ValidationError.new(errors) unless valid? end |
#validator ⇒ Object
17 18 19 |
# File 'lib/stretchy/utils/validation.rb', line 17 def validator @validator ||= self.class.validator.new(self) end |