Class: RubyLokaliseApi::Collections::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, RubyLokaliseApi::Concerns::AttrsLoadable, Utils::Attributes
Includes:
Enumerable, Utils::Keys
Defined in:
lib/ruby_lokalise_api/collections/base.rb

Overview

Base collection. Collection is an array of resources. The actual resources can be found by calling the ‘.collection` method

Constant Summary collapse

ATTRS_FILENAME =
'collection_attributes.yml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RubyLokaliseApi::Concerns::AttrsLoadable

extended, inherited

Methods included from Utils::Keys

#collection_key_for, #data_key_for

Constructor Details

#initialize(response) ⇒ Base

Returns a new instance of Base.



23
24
25
26
27
28
# File 'lib/ruby_lokalise_api/collections/base.rb', line 23

def initialize(response)
  @self_endpoint = response.endpoint

  populate_common_attrs_from response
  produce_collection_from response
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



20
21
22
# File 'lib/ruby_lokalise_api/collections/base.rb', line 20

def collection
  @collection
end

#current_pageObject (readonly)

Returns the value of attribute current_page.



20
21
22
# File 'lib/ruby_lokalise_api/collections/base.rb', line 20

def current_page
  @current_page
end

#results_per_pageObject (readonly)

Returns the value of attribute results_per_page.



20
21
22
# File 'lib/ruby_lokalise_api/collections/base.rb', line 20

def results_per_page
  @results_per_page
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



20
21
22
# File 'lib/ruby_lokalise_api/collections/base.rb', line 20

def total_pages
  @total_pages
end

#total_resultsObject (readonly)

Returns the value of attribute total_results.



20
21
22
# File 'lib/ruby_lokalise_api/collections/base.rb', line 20

def total_results
  @total_results
end

Instance Method Details

#first_page?Boolean

Checks whether the current page is the first one

Returns:

  • (Boolean)


74
75
76
# File 'lib/ruby_lokalise_api/collections/base.rb', line 74

def first_page?
  !prev_page?
end

#last_page?Boolean

Checks whether the current page is the last one

Returns:

  • (Boolean)


62
63
64
# File 'lib/ruby_lokalise_api/collections/base.rb', line 62

def last_page?
  !next_page?
end

#next_pageObject

Tries to fetch the next page for paginated collection Returns a new collection or nil if the next page is not available



32
33
34
35
36
37
38
39
40
# File 'lib/ruby_lokalise_api/collections/base.rb', line 32

def next_page
  return nil if last_page?

  self.class.new(
    reinit_endpoint(
      override_req_params: { page: current_page + 1 }
    ).do_get
  )
end

#next_page?Boolean

Checks whether the next page is available

Returns:

  • (Boolean)


56
57
58
# File 'lib/ruby_lokalise_api/collections/base.rb', line 56

def next_page?
  current_page.positive? && current_page < total_pages
end

#prev_pageObject

Tries to fetch the previous page for paginated collection Returns a new collection or nil if the previous page is not available



44
45
46
47
48
49
50
51
52
# File 'lib/ruby_lokalise_api/collections/base.rb', line 44

def prev_page
  return nil if first_page?

  self.class.new(
    reinit_endpoint(
      override_req_params: { page: current_page - 1 }
    ).do_get
  )
end

#prev_page?Boolean

Checks whether the previous page is available

Returns:

  • (Boolean)


68
69
70
# File 'lib/ruby_lokalise_api/collections/base.rb', line 68

def prev_page?
  current_page > 1
end