Module: Ratatouille::ArrayMethods

Defined in:
lib/ratatouille/array.rb

Overview

Module used to provide Array-specific validation methods

Instance Method Summary (collapse)

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)

Parameters:

  • min_size (Integer) (defaults to: 0)
  • max_size (Integer) (defaults to: nil)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :unwrap_block (Boolean) — default: false

    Perform block validation only -- skip length_between validation logic. Useless unless block provided



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, options={}, &block)
  parse_options(options)

  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.message}")
end

- (void) max_length(max_size = 0, options = {}, &block)

This method returns an undefined value.

Define Maximum Length of Array

Parameters:

  • max_size (Integer) (defaults to: 0)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :unwrap_block (Boolean) — default: false

    Perform block validation only -- skip max_length validation logic. Useless unless block provided



52
53
54
55
56
# File 'lib/ratatouille/array.rb', line 52

def max_length(max_size=0, options={}, &block)
  return length_between(0, max_size, options, &block)
rescue Exception => e
  validation_error("#{e.message}")
end

- (void) min_length(min_size = 0, options = {}, &block)

This method returns an undefined value.

Define Minimum Length of Array

Parameters:

  • min_size (Integer) (defaults to: 0)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :unwrap_block (Boolean) — default: false

    Perform block validation only -- skip min_length validation logic. Useless unless block provided



37
38
39
40
41
# File 'lib/ratatouille/array.rb', line 37

def min_length(min_size=0, options={}, &block)
  return length_between(min_size, nil, options, &block)
rescue Exception => e
  validation_error("#{e.message}")
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

Parameters:

  • options (Hash) (defaults to: {})

    Accepts global options in addition to the following:

Options Hash (options):

  • :name (Hash) — default: array_item

    Name each ratifiable object for use in validation message.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ratatouille/array.rb', line 14

def ratify_each(options={}, &block)
  parse_options(options)

  unless @skip
    if block_given?
      @ratifiable_object.each_with_index do |obj, i|
        options[:name] = options.fetch(:name, "array_item")
        child_object = Ratatouille::Ratifier.new(obj, options, &block)
        @errors["/"] << child_object.errors unless child_object.valid?
      end
    end
  end#skip
end