Class: ForemanApiClient::PagedResponse
- Inherits:
-
Object
- Object
- ForemanApiClient::PagedResponse
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/foreman_api_client/paged_response.rb
Overview
like the WillPaginate collection
Instance Attribute Summary collapse
-
#page ⇒ Object
Returns the value of attribute page.
-
#results ⇒ Object
Returns the value of attribute results.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #denormalize ⇒ Object
-
#initialize(json) ⇒ PagedResponse
constructor
per_page, search, sort.
-
#map!(&block) ⇒ Object
modify the structure inline.
Constructor Details
#initialize(json) ⇒ PagedResponse
per_page, search, sort
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/foreman_api_client/paged_response.rb', line 12 def initialize(json) if json.kind_of?(Hash) && json["results"] @results = json["results"] @total = json["total"].to_i @page = json["page"] else @results = json.kind_of?(Array) ? json : Array[json] @total = json.size @page = 1 end end |
Instance Attribute Details
#page ⇒ Object
Returns the value of attribute page.
7 8 9 |
# File 'lib/foreman_api_client/paged_response.rb', line 7 def page @page end |
#results ⇒ Object
Returns the value of attribute results.
9 10 11 |
# File 'lib/foreman_api_client/paged_response.rb', line 9 def results @results end |
#total ⇒ Object
Returns the value of attribute total.
8 9 10 |
# File 'lib/foreman_api_client/paged_response.rb', line 8 def total @total end |
Instance Method Details
#==(other) ⇒ Object
32 33 34 |
# File 'lib/foreman_api_client/paged_response.rb', line 32 def ==(other) results == other.results end |
#denormalize ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/foreman_api_client/paged_response.rb', line 36 def denormalize self.class.new( results.collect do |record| ancestors(results, record["ancestry"]).each_with_object({}) do |ancestor, h| h.merge!(ancestor.select { |_n, v| !v.nil? && v != "" }) end.merge!(record.select { |_n, v| !v.nil? && v != "" }) end ) end |
#map!(&block) ⇒ Object
modify the structure inline
27 28 29 30 |
# File 'lib/foreman_api_client/paged_response.rb', line 27 def map!(&block) self.results = results.map(&block) self end |