Class: SecID::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/sec_id/errors.rb

Overview

Immutable value object representing validation errors for an identifier. Follows Rails/ActiveModel conventions: use #details for structured error data and #messages for human-readable strings.

Examples:

No errors

errors = SecID::Errors.new([])
errors.none?     #=> true
errors.empty?    #=> true
errors.messages  #=> []

With errors

err = [{ error: :invalid_length, message: "Expected 12 characters, got 5" }]
errors = SecID::Errors.new(err)
errors.none?     #=> false
errors.details   #=> [{ error: :invalid_length, message: "..." }]
errors.messages  #=> ["Expected 12 characters, got 5"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors) ⇒ Errors

Returns a new instance of Errors.

Parameters:

  • errors (Array<Hash{Symbol => Symbol, String}>)

    array of error hashes



25
26
27
28
# File 'lib/sec_id/errors.rb', line 25

def initialize(errors)
  @details = errors.freeze
  freeze
end

Instance Attribute Details

#detailsArray<Hash{Symbol => Symbol, String}> (readonly)

Returns array of error hashes with :error and :message keys.

Returns:

  • (Array<Hash{Symbol => Symbol, String}>)

    array of error hashes with :error and :message keys



22
23
24
# File 'lib/sec_id/errors.rb', line 22

def details
  @details
end

Instance Method Details

#any?Boolean

Returns true when there are errors.

Returns:

  • (Boolean)

    true when there are errors



36
37
38
# File 'lib/sec_id/errors.rb', line 36

def any?
  !@details.empty?
end

#as_jsonArray<Hash>

Returns a JSON-compatible array of error detail hashes.

Returns:

  • (Array<Hash>)


70
71
72
# File 'lib/sec_id/errors.rb', line 70

def as_json(*)
  details
end

#each {|detail| ... } ⇒ Enumerator, self

Yields each error detail hash to the block.

Yield Parameters:

  • detail (Hash{Symbol => Symbol, String})

Returns:

  • (Enumerator, self)


58
59
60
# File 'lib/sec_id/errors.rb', line 58

def each(&)
  @details.each(&)
end

#empty?Boolean Also known as: none?

Returns true when there are no errors.

Returns:

  • (Boolean)

    true when there are no errors



41
42
43
# File 'lib/sec_id/errors.rb', line 41

def empty?
  @details.empty?
end

#messagesArray<String>

Returns human-readable error messages.

Returns:

  • (Array<String>)

    human-readable error messages



31
32
33
# File 'lib/sec_id/errors.rb', line 31

def messages
  @details.map { |e| e[:message] }
end

#sizeInteger

Returns number of errors.

Returns:

  • (Integer)

    number of errors



50
51
52
# File 'lib/sec_id/errors.rb', line 50

def size
  @details.size
end

#to_aArray<String>

Returns alias for #messages.

Returns:



63
64
65
# File 'lib/sec_id/errors.rb', line 63

def to_a
  messages
end