Currently Uploadcare::Api::ResourceList objects (Uploadcare::Api::FileList, Uploadcare::Api::GroupList) are working like so:
list = api.file_list(limit: 10)
list[0] # => #<Uploadcare::Api::File ...>
# but
list[10] # => nil (since only first 10 files were loaded so far)
# but
list.first(11).last # => #<Uploadcare::Api::File ...>
This happens because Uploadcare::Api::ResourceList delegates #[] method to #objects (array of currently loaded objects), so the requested object will only be returned if it was loaded so far.
It seems to me that this behaviour violates the least surprise principle: the expected behaviour for list[10] is to return the requested element of the list (triggering next page requests if needed) or nil
So how about reimplementing the #[] method to conform with this expectation, for exampe like so:
Currently
Uploadcare::Api::ResourceList
objects (Uploadcare::Api::FileList
,Uploadcare::Api::GroupList
) are working like so:This happens because
Uploadcare::Api::ResourceList
delegates#[]
method to#objects
(array of currently loaded objects), so the requested object will only be returned if it was loaded so far.It seems to me that this behaviour violates the least surprise principle: the expected behaviour for
list[10]
is to return the requested element of the list (triggering next page requests if needed) or nilSo how about reimplementing the
#[]
method to conform with this expectation, for exampe like so: