class Clarc::FormBase

Public Class Methods

new(params) click to toggle source
# File lib/clarc/form_base.rb, line 8
def initialize params
  @params = params
  build
end

Public Instance Methods

persist!() click to toggle source
# File lib/clarc/form_base.rb, line 47
def persist!
  validators.each do |attr, _|
    model_name = attr.to_s.camelize
    repository = "#{model_name}Repository".constantize
    repository.save(send(attr))
  end
end
to_hash() click to toggle source
# File lib/clarc/form_base.rb, line 41
def to_hash
  result = { errors: @errors }
  validators.each{ |attr, _| result[attr] = send(attr) }
  result
end
valid?() click to toggle source
# File lib/clarc/form_base.rb, line 17
def valid?
  @errors = {}
  valid = true

  validators.each do |attr, klass|
    value = send(attr)
    if value.is_a?(Array)
      value.each_with_index do |value, i|
        validator = klass.new(value)
        valid &= validator.valid?

        @errors[attr] ||= {}
        @errors[attr][i] = validator.errors
      end
    else
      validator = klass.new(value)
      valid &= validator.valid?
      @errors[attr] = validator.errors
    end
  end

  valid
end
validators() click to toggle source
# File lib/clarc/form_base.rb, line 13
def validators
  {}
end