Class: Hcp::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Chainable
Defined in:
lib/hcp/relation.rb

Overview

Every record of one kind a Housecall Pro account holds, walked a page at a time.

Constant Summary collapse

PAGE =

Records a page: the most Housecall Pro answers with, and more than it refuses.

200

Instance Method Summary collapse

Methods included from Chainable

#includes, #limit, #order, #where

Constructor Details

#initialize(type:, path: nil, company_id: nil, conditions: {}, sorts: {}, limit: nil, expands: []) ⇒ Relation

Returns a new instance of Relation.

Parameters:

  • type (Class)

    what each record is read as.

  • path (String) (defaults to: nil)

    where Housecall Pro keeps them, under the host.

  • company_id (String, nil) (defaults to: nil)

    the location to read them as.



12
13
14
15
16
17
18
19
20
21
# File 'lib/hcp/relation.rb', line 12

def initialize(type:, path: nil, company_id: nil, conditions: {}, sorts: {}, limit: nil,
  expands: [])
  @type = type
  @path = path || type.path
  @company_id = company_id
  @conditions = conditions
  @sorts = sorts
  @limit = limit
  @expands = expands
end

Instance Method Details

#countInteger

A page of one carries the total beside it, so this is one small read however long the list is.

Returns:

  • (Integer)

    how many records the list holds.



41
# File 'lib/hcp/relation.rb', line 41

def count = @count ||= [ read(@path, page_size: 1)['total_items'], @limit ].compact.min

#each(&block) ⇒ Enumerator, Relation

Nothing is read until the walk starts, and a page only once the one before it runs out.

Returns:

  • (Enumerator, Relation)

    every record the list holds, or the list once walked.



25
26
27
28
29
30
# File 'lib/hcp/relation.rb', line 25

def each(&block)
  return walk unless block

  walk.each(&block)
  self
end

#find(id) ⇒ Resource

Shadows Enumerable#find the way Active Record does: a record is reached by the ID Housecall Pro files it under, not by asking every record whether it is the one.

Parameters:

  • id (String)

    the Housecall Pro ID.

Returns:

  • (Resource)

    the record, raising Hcp::NotFound where there is none.



36
# File 'lib/hcp/relation.rb', line 36

def find(id) = record_for read("#{@path}/#{id}")