Class: Pobo::DTO::PaginatedResponse
- Inherits:
-
Object
- Object
- Pobo::DTO::PaginatedResponse
- Defined in:
- lib/pobo/dto/paginated_response.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data:, current_page:, per_page:, total:) ⇒ PaginatedResponse
constructor
A new instance of PaginatedResponse.
- #more_pages? ⇒ Boolean (also: #has_more_pages?)
- #total_pages ⇒ Object
Constructor Details
#initialize(data:, current_page:, per_page:, total:) ⇒ PaginatedResponse
Returns a new instance of PaginatedResponse.
8 9 10 11 12 13 |
# File 'lib/pobo/dto/paginated_response.rb', line 8 def initialize(data:, current_page:, per_page:, total:) @data = data @current_page = current_page @per_page = per_page @total = total end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
6 7 8 |
# File 'lib/pobo/dto/paginated_response.rb', line 6 def current_page @current_page end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/pobo/dto/paginated_response.rb', line 6 def data @data end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
6 7 8 |
# File 'lib/pobo/dto/paginated_response.rb', line 6 def per_page @per_page end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
6 7 8 |
# File 'lib/pobo/dto/paginated_response.rb', line 6 def total @total end |
Class Method Details
.from_hash(hash, item_class) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pobo/dto/paginated_response.rb', line 15 def self.from_hash(hash, item_class) items = (hash["data"] || hash[:data] || []).map do |item| item_class.from_hash(item) end = hash["meta"] || hash[:meta] || {} new( data: items, current_page: ["current_page"] || [:current_page] || 1, per_page: ["per_page"] || [:per_page] || 100, total: ["total"] || [:total] || 0 ) end |
Instance Method Details
#more_pages? ⇒ Boolean Also known as: has_more_pages?
36 37 38 |
# File 'lib/pobo/dto/paginated_response.rb', line 36 def more_pages? @current_page < total_pages end |
#total_pages ⇒ Object
30 31 32 33 34 |
# File 'lib/pobo/dto/paginated_response.rb', line 30 def total_pages return 0 if @per_page.zero? (@total.to_f / @per_page).ceil end |