Class: ElectionBuddy::Validation::LineErrors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/election_buddy/entities/validation/line_errors.rb

Overview

Represents a collection of line-by-line validation errors

Examples:

line_errors = LineErrors.new(response)
line_errors.total #=> 5
line_errors.total_pages #=> 1
line_errors.empty? #=> false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ LineErrors

Returns a new instance of LineErrors.

Parameters:

  • response (Hash)

    API response containing validation errors



26
27
28
29
30
31
# File 'lib/election_buddy/entities/validation/line_errors.rb', line 26

def initialize(response)
  @line_errors = response["voter_line_errors"]
  @total = response["meta"]["total"]
  @page = response["meta"]["page"]
  @per_page = response["meta"]["per_page"]
end

Instance Attribute Details

#pageInteger (readonly)

Returns Current page number.

Returns:

  • (Integer)

    Current page number



20
21
22
# File 'lib/election_buddy/entities/validation/line_errors.rb', line 20

def page
  @page
end

#per_pageInteger (readonly)

Returns Number of items per page.

Returns:

  • (Integer)

    Number of items per page



23
24
25
# File 'lib/election_buddy/entities/validation/line_errors.rb', line 23

def per_page
  @per_page
end

#totalInteger (readonly)

Returns Total number of validation errors.

Returns:

  • (Integer)

    Total number of validation errors



17
18
19
# File 'lib/election_buddy/entities/validation/line_errors.rb', line 17

def total
  @total
end

Instance Method Details

#collectionObject (private)



56
57
58
# File 'lib/election_buddy/entities/validation/line_errors.rb', line 56

def collection
  @collection ||= @line_errors.map { |line_error| LineError.new(line_error) }
end

#each(&block) ⇒ Enumerator

Iterates through each line error

Returns:

  • (Enumerator)

    Collection of line errors



43
44
45
# File 'lib/election_buddy/entities/validation/line_errors.rb', line 43

def each(&block)
  collection.each(&block)
end

#empty?Boolean

Checks if there are any line errors

Returns:

  • (Boolean)

    true if no errors exist, false otherwise



50
51
52
# File 'lib/election_buddy/entities/validation/line_errors.rb', line 50

def empty?
  collection.empty?
end

#total_pagesInteger

Calculates the total number of pages

Returns:

  • (Integer)

    Total number of pages



36
37
38
# File 'lib/election_buddy/entities/validation/line_errors.rb', line 36

def total_pages
  (total.to_f / per_page).ceil
end