Class: Miscellany::ParamValidator::ErrorStore

Inherits:
Object
  • Object
show all
Defined in:
lib/miscellany/param_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrorStore

Returns a new instance of ErrorStore.



422
423
424
425
# File 'lib/miscellany/param_validator.rb', line 422

def initialize
  @errors = []
  @fields = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



420
421
422
# File 'lib/miscellany/param_validator.rb', line 420

def errors
  @errors
end

#fieldsObject (readonly)

Returns the value of attribute fields.



420
421
422
# File 'lib/miscellany/param_validator.rb', line 420

def fields
  @fields
end

Instance Method Details

#merge!(other_store) ⇒ Object



462
463
464
465
466
467
468
469
470
471
# File 'lib/miscellany/param_validator.rb', line 462

def merge!(other_store)
  return self if other_store == self

  @errors |= other_store.errors
  other_store.fields.each do |k, v|
    store_for(k).merge!(v)
  end

  self
end

#present?Boolean

Returns:

  • (Boolean)


441
442
443
# File 'lib/miscellany/param_validator.rb', line 441

def present?
  @errors.present? || @fields.values.any?(&:present?)
end

#push(error) ⇒ Object



427
428
429
430
431
432
433
434
435
# File 'lib/miscellany/param_validator.rb', line 427

def push(error)
  if error.is_a?(ErrorStore)
    merge!(error)
  elsif error.is_a?(Array)
    error.each{|e| push(e) }
  else
    @errors << error
  end
end

#push_to(field, error) ⇒ Object



437
438
439
# File 'lib/miscellany/param_validator.rb', line 437

def push_to(field, error)
  store_for(field).push(error)
end

#serializeObject



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/miscellany/param_validator.rb', line 445

def serialize
  if @fields.values.any?(&:present?)
    h = { }
    h[:_SELF_] = @errors if @errors.present?
    @fields.each do |k, v|
      s = v.serialize
      next unless s.present?
      h[k] = s
    end
    h
  elsif @errors.present?
    @errors
  else
    nil
  end
end

#store_for(field) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/miscellany/param_validator.rb', line 473

def store_for(field)
  if field.is_a?(Symbol) || field.is_a?(Numeric)
    field = [field]
  elsif field.is_a?(String)
    field = field.split('.')
  elsif field.is_a?(Array)
    field = [*field]
  end

  sfield = field.shift
  store = @fields[sfield.to_s] ||= ErrorStore.new
  return store if field.count == 0
  return store.store_for(field)
end