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

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

Instance Method Details

#contractsObject



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

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