module Serializable
Public Class Methods
as_json(thing)
click to toggle source
Returns a hash which becomes the JSON
# File lib/game_2d/serializable.rb, line 23 def self.as_json(thing) { :class => thing.class.to_s } end
from_json(json)
click to toggle source
# File lib/game_2d/serializable.rb, line 40 def self.from_json(json) return nil unless json class_name = json[:class] fail "No class name in #{json.inspect}" unless class_name fail "Suspicious class name in #{json.inspect}: #{class_name}" unless (class_name.start_with? 'Entity::') || (class_name.start_with? 'Move::') require "game_2d/#{class_name.pathize}" clazz = constant(class_name) it = clazz.new if it.is_a? Registerable it.registry_id = json[:registry_id] if json[:registry_id] end it.update_from_json(json) end
Public Instance Methods
<=>(other)
click to toggle source
Based on all_state
# File lib/game_2d/serializable.rb, line 13 def <=>(other) self.all_state <=> other.all_state end
==(other)
click to toggle source
# File lib/game_2d/serializable.rb, line 16 def ==(other) other.class.equal?(self.class) && other.all_state == self.all_state end
all_state()
click to toggle source
Flat list of all object state For sorting purposes, most significant goes first
# File lib/game_2d/serializable.rb, line 8 def all_state [] end
eql?(other)
click to toggle source
# File lib/game_2d/serializable.rb, line 20 def eql?(other); self == other; end
hash()
click to toggle source
# File lib/game_2d/serializable.rb, line 19 def hash; self.class.hash ^ all_state.hash; end
to_json(*args)
click to toggle source
Based on as_json
# File lib/game_2d/serializable.rb, line 28 def to_json(*args) as_json.to_json(*args) end
to_s()
click to toggle source
# File lib/game_2d/serializable.rb, line 36 def to_s self.class.name end
update_from_json(hash)
click to toggle source
Make our state match what’s in the hash
# File lib/game_2d/serializable.rb, line 33 def update_from_json(hash) end