Sha256: 01d38077d3ca144d924b68da68807bc06eef6ae4c159005f9c53355150767ee9

Contents?: true

Size: 817 Bytes

Versions: 2

Compression:

Stored size: 817 Bytes

Contents

module NubeFact::Validator
  def self.included(klass)
    klass.extend ClassMethods
    klass.class_variable_set :@@required_fields, []
  end

  module ClassMethods
    def add_required_fields(*fields)
      base = required_fields 
      
      self.class_variable_set :@@required_fields, \
        required_fields + fields
    end

    def required_fields
      required_fields = self.class_variable_get :@@required_fields
      return self::REQUIRED_FIELDS if required_fields.empty?
      required_fields
    end
  end

  def validate!
    #ToDO change for a real validator like ActiveModel::Validations or Hanami::Validations
    self.class::required_fields.each do |field|
      if send(field).nil?
        raise NubeFact::ValidationError.new "#{field} it's a required field."
      end
    end
    true
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nubefact-0.0.10 lib/util/validator.rb
nubefact-0.0.9 lib/util/validator.rb