class Object

Public Class Methods

exclude_from_serial(ary) click to toggle source
# File lib/gamefic/serialize.rb, line 48
def exclude_from_serial ary
  @excluded_from_serial = ary
end
excluded_from_serial() click to toggle source
# File lib/gamefic/serialize.rb, line 52
def excluded_from_serial
  @excluded_from_serial ||= []
end

Public Instance Methods

from_serial(index = []) click to toggle source
# File lib/gamefic/serialize.rb, line 65
def from_serial(index = [])
  if self.is_a?(Hash)
    if self['instance']
      elematch = self['instance'].match(/^#<ELE_([\d]+)>$/)
      object = index[elematch[1].to_i]
      raise "Unable to load indexed element ##{elematch[1]} #{self}" if object.nil?
    elsif self['class']
      if self['class'] == 'Hash'
        object = {}
        self['data'].each do |arr|
          object[arr[0].from_serial(index)] = arr[1].from_serial(index)
        end
        return object
      elsif self['class'] == 'Class'
        return Gamefic::Serialize.string_to_constant(self['name'])
      elsif self['class'] == 'Set'
        return Set.new(self['data'].map { |el| el.from_serial(index) })
      else
        elematch = self['class'].match(/^#<ELE_([\d]+)>$/)
        if elematch
          klass = index[elematch[1].to_i]
        else
          klass = Gamefic::Serialize.string_to_constant(self['class'])
        end
        raise "Unable to find class #{self['class']} #{self}" if klass.nil?
        object = klass.allocate
      end
    end
    self['ivars'].each_pair do |k, v|
      object.instance_variable_set(k, v.from_serial(index))
    end
    object
  elsif self.is_a?(Numeric)
    self
  elsif self.is_a?(String)
    match = self.match(/#<ELE_([0-9]+)>/)
    return index.index(match[1].to_i) if match
    match = self.match(/#<SYM:([a-z0-9_\?\!]+)>/i)
    return match[1].to_sym if match
    self
  else
    # true, false, or nil
    self
  end
end
serialize_instance_variables(index) click to toggle source
# File lib/gamefic/serialize.rb, line 111
def serialize_instance_variables(index)
  result = {}
  instance_variables.each do |k|
    next if self.class.excluded_from_serial.include?(k)
    val = instance_variable_get(k)
    if index.include?(val)
      result[k.to_s] = {
        'instance' => "#<ELE_#{index.index(val)}>",
        'ivars' => {}
      }
    else
      result[k.to_s] = val.to_serial(index)
    end
  end
  result
end
to_serial(_index) click to toggle source
# File lib/gamefic/serialize.rb, line 57
def to_serial(_index)
  return self if [true, false, nil].include?(self)
  # @todo This warning is a little too spammy. Set up a logger so it can be
  # limited to an info or debug level.
  # STDERR.puts "Unable to convert #{self} to element"
  "#<UNKNOWN>"
end