Class: KB::Pet

Inherits:
BaseModel show all
Includes:
Destroyable, FindOrCreatable, Findable, Updatable, Upsertable
Defined in:
lib/kb/models/pet.rb

Constant Summary collapse

STRING_FIELDS =
%i[key pet_parent_key name age_category sex breed chip species].freeze
DATE_FIELDS =
%i[birth_date deleted_at].freeze
BOOLEAN_FIELDS =
%i[neutered mongrel].freeze
FIELDS =
[*STRING_FIELDS, *DATE_FIELDS, *BOOLEAN_FIELDS].freeze

Instance Attribute Summary

Attributes inherited from BaseModel

#persisted

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Findable

#reload

Methods inherited from BaseModel

#==, define_attribute_methods, define_attributes, #initialize, #persist!, #persisted?

Methods included from Inspectionable

#inspect

Constructor Details

This class inherits a constructor from KB::BaseModel

Class Method Details

.transfer(pet_key:, destination_pet_parent_key:) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/kb/models/pet.rb', line 54

def self.transfer(pet_key:, destination_pet_parent_key:)
  kb_client.request('transfer', filters: { pet_key: pet_key,
                                           destination_pet_parent_key: destination_pet_parent_key },
                                method: :post)
rescue Faraday::Error => e
  raise KB::Error.from_faraday(e)
end

Instance Method Details

#contractsObject



62
63
64
65
66
# File 'lib/kb/models/pet.rb', line 62

def contracts
  self.class.kb_client.request("#{key}/contracts").map do |contract|
    PetContract.from_api(contract)
  end
end

#destroy!Object



46
47
48
49
50
51
52
# File 'lib/kb/models/pet.rb', line 46

def destroy!
  return unless @persisted

  self.class.destroy key
  @destroyed = true
  freeze
end

#destroyed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/kb/models/pet.rb', line 42

def destroyed?
  @destroyed
end

#save!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kb/models/pet.rb', line 28

def save!
  return unless changed?

  run_callbacks :save do
    self.attributes = if @persisted
                        self.class.update key, changes.transform_values(&:last)
                      else
                        self.class.create changes.transform_values(&:last)
                      end

    self
  end
end