Sha256: c822505707b84cf338b7acbcc2859cc76900ba863b860d75ee20aff402583519

Contents?: true

Size: 1005 Bytes

Versions: 10

Compression:

Stored size: 1005 Bytes

Contents

module Proposal
  class ArgumentsValidator < ActiveModel::Validator

    def validate_expected record, sym
      unless record.arguments[sym].present?
        record.errors.add :arguments, "is missing #{sym}"
      end
    end

    def validate record
      if record.expects.is_a? Proc
        unless record.expects.call(record.arguments)
          record.errors.add :arguments, "is invalid"
        end
      elsif record.arguments.is_a? Hash
        case record.expects
        when Symbol
          validate_expected record, record.expects
        when Array
          record.expects.each { |sym| validate_expected record, sym }
        end
      else
        record.errors.add :arguments, "must be a hash"
        case record.expects
        when Symbol
          record.errors.add :arguments, "is missing #{record.expects}"
        when Array
          record.expects.each do |sym|
            record.errors.add :arguments, "is missing #{sym}"
          end
        end
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
proposal-2.0.2 app/validators/proposal/arguments_validator.rb
proposal-2.0.1 app/validators/proposal/arguments_validator.rb
proposal-2.0.0 app/validators/proposal/arguments_validator.rb
proposal-1.0.0 app/validators/proposal/arguments_validator.rb
proposal-0.0.6 app/validators/proposal/arguments_validator.rb
proposal-0.0.5 app/validators/proposal/arguments_validator.rb
proposal-0.0.4 app/validators/proposal/arguments_validator.rb
proposal-0.0.3 app/validators/proposal/arguments_validator.rb
proposal-0.0.2 app/validators/proposal/arguments_validator.rb
proposal-0.0.1 app/validators/proposal/arguments_validator.rb