Module: FulfilApi::Relation::Countable

Included in:
FulfilApi::Relation
Defined in:
lib/fulfil_api/relation/countable.rb

Overview

The Countable extends the relation by

adding a method to count the records within a given context.

Instance Method Summary collapse

Instance Method Details

#countInteger

Note:

It takes into account the query conditions and can be used to find the count of a subset of resources.

Note:

The #count directly triggers an HTTP request to Fulfil but the return value is cached. When you want to recount, use #recount.

Finds the exact number of API resources in Fulfil.

Returns:

  • (Integer)

Raises:



17
18
19
20
21
22
23
24
# File 'lib/fulfil_api/relation/countable.rb', line 17

def count
  raise FulfilApi::Resource::ModelNameMissing if model_name.nil?

  @count ||= FulfilApi.client.put(
    "/model/#{model_name}/search_count",
    body: { filters: conditions }.compact_blank
  )
end

#counted?true, false

Checks if the relation has already been counted.

Returns:

  • (true, false)


29
30
31
# File 'lib/fulfil_api/relation/countable.rb', line 29

def counted?
  @count
end

#recountInteger

Note:

Under the hood, it uses the #count method by resetting the cached value and calling #count again.

Recounts the exact number of API resources in Fulfil.

Returns:

  • (Integer)


39
40
41
42
# File 'lib/fulfil_api/relation/countable.rb', line 39

def recount
  @count = nil
  count
end