Sha256: 77ea68772939aff30962eb1fef57713595ac3007a49fa23d8c85f571f3339f5a

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Reform::Validation
  # A Group is a set of native validations, targeting a validation backend (AM, Lotus, Dry).
  # Group receives configuration via #validates and #validate and translates that to its
  # internal backend.
  #
  # The #call method will run those validations on the provided objects.

  # Set of Validation::Group objects.
  # This implements adding, iterating, and finding groups, including "inheritance" and insertions.
  class Groups < Array
    def initialize(group_class)
      @group_class = group_class
    end

    def add(name, options)
      if options[:inherit]
        return self[name] if self[name]
      end

      i = index_for(options)

      self.insert(i, [name, group = @group_class.new(options), options]) # Group.new
      group
    end

  private

    def index_for(options)
      return find_index { |el| el.first == options[:after] } + 1 if options[:after]
      size # default index: append.
    end

    def [](name)
      cfg = find { |c| c.first == name }
      return unless cfg
      cfg[1]
    end


    # Runs all validations groups according to their rules and returns all Result objects.
    class Validate
      def self.call(groups, form)
        results = {}

        groups.collect do |(name, group, options)|
          next unless evaluate?(options[:if], results, form)

          results[name] = group.(form) # run validation for group. store and collect <Result>.
        end
      end

      def self.evaluate?(depends_on, results, form)
        return true if depends_on.nil?
        return results[depends_on].success? if depends_on.is_a?(Symbol)
        form.instance_exec(results, &depends_on)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reform-2.3.0.rc1 lib/reform/validation/groups.rb