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.



455
456
457
458
# File 'lib/miscellany/param_validator.rb', line 455

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



453
454
455
# File 'lib/miscellany/param_validator.rb', line 453

def errors
  @errors
end

#fieldsObject (readonly)

Returns the value of attribute fields.



453
454
455
# File 'lib/miscellany/param_validator.rb', line 453

def fields
  @fields
end

Instance Method Details

#merge!(other_store) ⇒ Object



495
496
497
498
499
500
501
502
503
504
# File 'lib/miscellany/param_validator.rb', line 495

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)


474
475
476
# File 'lib/miscellany/param_validator.rb', line 474

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

#push(error) ⇒ Object



460
461
462
463
464
465
466
467
468
# File 'lib/miscellany/param_validator.rb', line 460

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



470
471
472
# File 'lib/miscellany/param_validator.rb', line 470

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

#serializeObject



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/miscellany/param_validator.rb', line 478

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



506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/miscellany/param_validator.rb', line 506

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