impress/app/models/pet.rb

55 lines
1.3 KiB
Ruby
Raw Normal View History

class Pet < ApplicationRecord
belongs_to :pet_type
attr_reader :items, :pet_state, :alt_style
def load!(timeout: nil)
viewer_data_hash = Neopets::CustomPets.fetch_viewer_data(name, timeout:)
use_modeling_snapshot(ModelingSnapshot.new(viewer_data_hash))
end
2013-12-08 20:59:36 -08:00
def use_modeling_snapshot(snapshot)
self.pet_type = snapshot.pet_type
@pet_state = snapshot.pet_state
@alt_style = snapshot.alt_style
@items = snapshot.items
2013-12-08 20:59:36 -08:00
end
2010-10-10 19:18:42 -07:00
def wardrobe_query
{
name: self.name,
color: self.pet_type.color_id,
species: self.pet_type.species_id,
pose: self.pet_state.pose,
state: self.pet_state.id,
objects: self.items.map(&:id),
style: self.alt_style ? self.alt_style.id : nil,
2010-10-10 19:18:42 -07:00
}.to_query
end
2010-11-06 16:07:15 -07:00
def contributables
2024-01-24 03:54:43 -08:00
contributables = [pet_type, @pet_state, @alt_style].filter(&:present?)
2010-11-06 16:07:15 -07:00
items.each do |item|
contributables << item
contributables += item.pending_swf_assets
end
contributables
end
2010-11-06 15:08:42 -07:00
before_validation do
2010-11-06 16:07:15 -07:00
pet_type.save!
@pet_state.save! if @pet_state
@alt_style.save! if @alt_style
(@items || []).each(&:save!)
2010-11-06 15:08:42 -07:00
end
def self.load(name, **options)
pet = Pet.find_or_initialize_by(name: name)
pet.load!(**options)
pet
end
class UnexpectedDataFormat < RuntimeError;end
end