Module: Ratatouille::ArrayMethods
- Defined in:
- lib/ratatouille/array.rb
Overview
Module used to provide Array-specific validation methods
Instance Method Summary (collapse)
-
- (void) length_between(min_size = 0, max_size = nil, options = {}, &block)
Define length range of Array (inclusive).
-
- (void) max_length(max_size = 0, options = {}, &block)
Define Maximum Length of Array.
-
- (void) min_length(min_size = 0, options = {}, &block)
Define Minimum Length of Array.
-
- (void) ratify_each(options = {}, &block)
Iterator method to encapsulate validation.
Instance Method Details
- (void) length_between(min_size = 0, max_size = nil, options = {}, &block)
This method returns an undefined value.
Define length range of Array (inclusive)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ratatouille/array.rb', line 68 def length_between(min_size=0, max_size=nil, ={}, &block) () unless @skip == true unless @unwrap_block == true # Minimum Length Validation unless min_size.to_i >= 0 validation_error("min_length must be a number greater than or equal to 0") return end unless @ratifiable_object.size >= min_size.to_i validation_error("length must be #{min_size} or more") return end # Maximum Length Validation unless max_size.nil? unless max_size.to_i >= 0 validation_error("max_size must be a number greater than or equal to 0") return end if @ratifiable_object.size > max_size.to_i validation_error("length must be less than #{max_size.to_i}") return end unless max_size > min_size validation_error("max_size must be greater than min_size") return end end end#unwrap_block instance_eval(&block) if block_given? end#skip rescue Exception => e validation_error("#{e.}") end |
- (void) max_length(max_size = 0, options = {}, &block)
This method returns an undefined value.
Define Maximum Length of Array
52 53 54 55 56 |
# File 'lib/ratatouille/array.rb', line 52 def max_length(max_size=0, ={}, &block) return length_between(0, max_size, , &block) rescue Exception => e validation_error("#{e.}") end |
- (void) min_length(min_size = 0, options = {}, &block)
This method returns an undefined value.
Define Minimum Length of Array
37 38 39 40 41 |
# File 'lib/ratatouille/array.rb', line 37 def min_length(min_size=0, ={}, &block) return length_between(min_size, nil, , &block) rescue Exception => e validation_error("#{e.}") end |
- (void) ratify_each(options = {}, &block)
Note:
Method will NOT work without a block
This method returns an undefined value.
Iterator method to encapsulate validation
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ratatouille/array.rb', line 14 def ratify_each(={}, &block) () unless @skip if block_given? @ratifiable_object.each_with_index do |obj, i| [:name] = .fetch(:name, "array_item") child_object = Ratatouille::Ratifier.new(obj, , &block) @errors["/"] << child_object.errors unless child_object.valid? end end end#skip end |