Module: Acfs::Resource::QueryMethods::ClassMethods

Defined in:
lib/acfs/resource/query_methods.rb

Instance Method Summary collapse

Instance Method Details

#all(params = {}, opts = {}) {|collection| ... } ⇒ Collection Also known as: where

Try to load all resources.

Parameters:

  • params (Hash) (defaults to: {})

    Request parameters that will be send to remote service.

Yields:

  • (collection)

    Callback block to be executed when resource collection was loaded successfully.

Yield Parameters:

  • collection (Collection)

    Collection of fetched resources.

Returns:

  • (Collection)

    Collection of requested resources.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/acfs/resource/query_methods.rb', line 80

def all(params = {}, opts = {}, &block)
  collection = ::Acfs::Collection.new self
  collection.__callbacks__ << block if block

  operation(:list, **opts, params: params) do |data, response|
    data.each {|obj| collection << create_resource(obj) }
    collection.process_response response
    collection.loaded!
    collection.__invoke__
  end

  collection
end

#each_item(opts = {}) {|item| ... } ⇒ Object

Iterates over all items of all pages returned by index action.

Server must return a paginated resource.

Examples:

index = 0
User.each_item do |page|
  index += 1
end
Acfs.run
print index
# => 142

Parameters:

  • opts (Hash) (defaults to: {})

    Options passed to #each_page.

Yields:

  • (item)

    Callback that will be invoked for each item.

Yield Parameters:



199
200
201
202
203
204
205
# File 'lib/acfs/resource/query_methods.rb', line 199

def each_item(opts = {})
  each_page(opts) do |collection|
    collection.each do |item|
      yield item, collection
    end
  end
end

#each_page(opts = {}) {|collection| ... } ⇒ Collection

Iterates over all pages returned by index action.

Server must return a paginated resource.

Examples:

User.each_page do |page|
  p page.size
end
Acfs.run
# => 50
# => 50
# => 42

Parameters:

  • opts (Hash) (defaults to: {})

    Options passed to #where.

Yields:

  • (collection)

    Callback that will be invoked for each page.

Yield Parameters:

  • collection (Collection)

    Paginated collection.

Returns:



170
171
172
173
174
175
176
# File 'lib/acfs/resource/query_methods.rb', line 170

def each_page(opts = {})
  cb = proc do |collection|
    yield collection
    collection.next_page(&cb)
  end
  where opts, &cb
end

#find(id, opts = {}) {|resource| ... } ⇒ self #find(ids, opts = {}) {|collection| ... } ⇒ Collection

Overloads:

  • #find(id, opts = {}) {|resource| ... } ⇒ self

    Find a single resource by given ID.

    Examples:

    user = User.find(5) # Will query `http://base.url/users/5`

    Parameters:

    • id (Fixnum)

      Resource IDs to fetch from remote service.

    • params (Hash)

      Additional options.

    Options Hash (opts):

    • :params (Hash)

      Additional parameters added to request. `:id` will be overridden with given ID.

    Yields:

    • (resource)

      Callback block to be executed after resource was fetched successfully.

    Yield Parameters:

    • resource (self)

      Fetched resources.

    Returns:

    • (self)

      Resource object if only one ID was given.

  • #find(ids, opts = {}) {|collection| ... } ⇒ Collection

    Load collection of specified resources by given IDs.

    Examples:

    User.find([1, 2, 5]) # Will return collection and will request
                         # `http://base.url/users/1`,
                         # `http://base.url/users/2`
                         # and `http://base.url/users/5` parallel

    Parameters:

    • ids (Array<Integer>)

      List of resource IDs.

    • opts (Hash) (defaults to: {})

      Additional options.

    Options Hash (opts):

    • :params (Hash)

      Additional parameters added to request. `:id` will be overridden with individual resource ID.

    Yields:

    • (collection)

      Callback block to be executed after collection was fetched successfully.

    Yield Parameters:

    • resource (Collection)

      Collection with fetched resources.

    Returns:

    • (Collection)

      Collection of requested resources.



59
60
61
62
63
64
65
# File 'lib/acfs/resource/query_methods.rb', line 59

def find(id_or_ids, **opts, &block)
  if id_or_ids.respond_to? :each
    find_multiple id_or_ids, opts, &block
  else
    find_single id_or_ids, opts, &block
  end
end

#find_by(params) {|resource| ... } ⇒ self

Try to load first resource. Return nil if no object can be loaded.

Parameters:

  • params (Hash)

    Request parameters that will be send to remote service.

Yields:

  • (resource)

    Callback block to be executed after resource was fetched (even if nil).

Yield Parameters:

  • resource (self)

    Fetched resource, nil if empty list is returned

Returns:

  • (self)

    Resource object, nil if empty list is returned



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/acfs/resource/query_methods.rb', line 109

def find_by(params, &block)
  Acfs::Util::ResourceDelegator.new(new).tap do |m|
    m.__callbacks__ << block unless block.nil?
    operation(:list, params: params) do |data|
      if data.empty?
        m.__setobj__ nil
      else
        m.__setobj__ create_resource(data.first, origin: m.__getobj__)
      end
      m.__invoke__
    end
  end
end

#find_by!(params) {|resource| ... } ⇒ self

Try to load first resource. Raise Acfs::ResourceNotFound exception if no object can be loaded.

Parameters:

  • params (Hash)

    Request parameters that will be send to remote service.

Yields:

  • (resource)

    Callback block to be executed after resource was fetched successfully.

Yield Parameters:

  • resource (self)

    Fetched resource, nil if empty list is returned

Returns:

  • (self)

    Resource object, nil if empty list is returned



138
139
140
141
142
143
144
145
146
# File 'lib/acfs/resource/query_methods.rb', line 138

def find_by!(params, &block)
  find_by params do |m|
    if m.nil?
      raise Acfs::ResourceNotFound.new message: 'Received erroneous ' \
                                                "response: no `#{name}` with params #{params} found"
    end
    block&.call m
  end
end