Module: ActiveModel::Validations::ClassMethods
- Defined in:
- lib/validates_serialized/validators/hash_validator.rb,
lib/validates_serialized/validators/array_validator.rb,
lib/validates_serialized/validators/hash_block_validator.rb,
lib/validates_serialized/validators/serialized_validator.rb,
lib/validates_serialized/validators/array_block_validator.rb,
lib/validates_serialized/validators/object_block_validator.rb
Instance Method Summary collapse
-
#validates_array_values(*attributes) ⇒ Object
Helper to accept arguments in the style of the
validatesclass method. - #validates_array_values!(*attributes) ⇒ Object
- #validates_array_values_with(*args, &block) ⇒ Object
-
#validates_each_in_array(*attr_names, &block) ⇒ Object
Helper to accept arguments in the style of the
validatesclass method. - #validates_each_in_array!(*attr_names, &block) ⇒ Object
-
#validates_hash_keys(*attr_names, &block) ⇒ Object
Helper to accept arguments in the style of the
validatesclass method. - #validates_hash_keys!(*attr_names, &block) ⇒ Object
-
#validates_hash_values(*attributes) ⇒ Object
Helper to accept arguments in the style of the
validatesclass method. - #validates_hash_values!(*attributes) ⇒ Object
- #validates_hash_values_with(*args, &block) ⇒ Object
-
#validates_serialized(*attr_names, &block) ⇒ Object
Helper to accept arguments in the style of the
validatesclass method. - #validates_serialized!(*attr_names, &block) ⇒ Object
- #validates_serialized_with(*args, &block) ⇒ Object
Instance Method Details
#validates_array_values(*attributes) ⇒ Object
Helper to accept arguments in the style of the validates class method
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/validates_serialized/validators/array_validator.rb', line 40 def validates_array_values(*attributes) defaults = attributes..dup validations = defaults.slice!(*_validates_default_keys) raise ArgumentError, "You need to supply at least one attribute" if attributes.empty? raise ArgumentError, "You need to supply at least one validation" if validations.empty? defaults[:attributes] = attributes validations.each do |key, | next unless key = "#{key.to_s.camelize}Validator" begin validator = key.include?('::') ? key.constantize : const_get(key) rescue NameError raise ArgumentError, "Unknown validator: '#{key}'" end validates_array_values_with(validator, defaults.merge(())) end end |
#validates_array_values!(*attributes) ⇒ Object
63 64 65 66 67 |
# File 'lib/validates_serialized/validators/array_validator.rb', line 63 def validates_array_values!(*attributes) = attributes. [:strict] = true validates_array_values(*(attributes << )) end |
#validates_array_values_with(*args, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/validates_serialized/validators/array_validator.rb', line 18 def validates_array_values_with(*args, &block) # TODO: It would be nice for these to all make a call to 'validates_serialized_with' # so that there's more code re-use # validates_serialized_with args.push(ArrayValidator), &block = args. [:class] = self validator = ArrayValidator.new(args, , &block) if validator.respond_to?(:attributes) && !validator.attributes.empty? validator.attributes.each do |attribute| _validators[attribute.to_sym] << validator end else _validators[nil] << validator end validate(validator, ) end |
#validates_each_in_array(*attr_names, &block) ⇒ Object
Helper to accept arguments in the style of the validates class method
53 54 55 56 |
# File 'lib/validates_serialized/validators/array_block_validator.rb', line 53 def validates_each_in_array(*attr_names, &block) raise ArgumentError, "You need to supply at least one attribute" if attr_names.empty? validates_with ArrayBlockValidator, _merge_attributes(attr_names), &block end |
#validates_each_in_array!(*attr_names, &block) ⇒ Object
58 59 60 61 62 |
# File 'lib/validates_serialized/validators/array_block_validator.rb', line 58 def validates_each_in_array!(*attr_names, &block) = attr_names. [:strict] = true validates_each_in_array(*(attr_names << ), &block) end |
#validates_hash_keys(*attr_names, &block) ⇒ Object
Helper to accept arguments in the style of the validates class method
47 48 49 50 |
# File 'lib/validates_serialized/validators/hash_block_validator.rb', line 47 def validates_hash_keys(*attr_names, &block) raise ArgumentError, "You need to supply at least one attribute" if attr_names.empty? validates_with HashBlockValidator, _merge_attributes(attr_names), &block end |
#validates_hash_keys!(*attr_names, &block) ⇒ Object
52 53 54 55 56 |
# File 'lib/validates_serialized/validators/hash_block_validator.rb', line 52 def validates_hash_keys!(*attr_names, &block) = attr_names. [:strict] = true validates_hash_keys(*(attr_names << ), &block) end |
#validates_hash_values(*attributes) ⇒ Object
Helper to accept arguments in the style of the validates class method
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/validates_serialized/validators/hash_validator.rb', line 37 def validates_hash_values(*attributes) defaults = attributes..dup validations = defaults.slice!(*_validates_default_keys) raise ArgumentError, "You need to supply at least one attribute" if attributes.empty? raise ArgumentError, "You need to supply at least one validation" if validations.empty? defaults[:attributes] = attributes validations.each do |key, | next unless key = "#{key.to_s.camelize}Validator" begin validator = key.include?('::') ? key.constantize : const_get(key) rescue NameError raise ArgumentError, "Unknown validator: '#{key}'" end validates_hash_values_with(validator, defaults.merge(())) end end |
#validates_hash_values!(*attributes) ⇒ Object
60 61 62 63 64 |
# File 'lib/validates_serialized/validators/hash_validator.rb', line 60 def validates_hash_values!(*attributes) = attributes. [:strict] = true validates_hash_values(*(attributes << )) end |
#validates_hash_values_with(*args, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/validates_serialized/validators/hash_validator.rb', line 18 def validates_hash_values_with(*args, &block) = args. [:class] = self validator = HashValidator.new(args, , &block) if validator.respond_to?(:attributes) && !validator.attributes.empty? validator.attributes.each do |attribute| _validators[attribute.to_sym] << validator end else _validators[nil] << validator end validate(validator, ) end |
#validates_serialized(*attr_names, &block) ⇒ Object
Helper to accept arguments in the style of the validates class method
46 47 48 49 |
# File 'lib/validates_serialized/validators/object_block_validator.rb', line 46 def validates_serialized(*attr_names, &block) raise ArgumentError, "You need to supply at least one attribute" if attr_names.empty? validates_with ObjectBlockValidator, _merge_attributes(attr_names), &block end |
#validates_serialized!(*attr_names, &block) ⇒ Object
51 52 53 54 55 |
# File 'lib/validates_serialized/validators/object_block_validator.rb', line 51 def validates_serialized!(*attr_names, &block) = attr_names. [:strict] = true validates_serialized(*(attr_names << ), &block) end |
#validates_serialized_with(*args, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/validates_serialized/validators/serialized_validator.rb', line 46 def validates_serialized_with(*args, &block) = args. [:class] = self serialized_validator_class = args.shift validator = serialized_validator_class.new(args, , &block) if validator.respond_to?(:attributes) && !validator.attributes.empty? validator.attributes.each do |attribute| _validators[attribute.to_sym] << validator end else _validators[nil] << validator end validate(validator, ) end |