Module: Engine::Serializable

Included in:
Component, Font, GameObject, Material, Mesh, Shader, Texture
Defined in:
lib/engine/serialization/serializable.rb

Defined Under Namespace

Modules: ClassMethods Classes: InitializeNotAllowedError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allowed_class?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/engine/serialization/serializable.rb', line 30

def self.allowed_class?(class_name)
  @allowed_classes.key?(class_name)
end

.get_class(class_name) ⇒ Object



34
35
36
# File 'lib/engine/serialization/serializable.rb', line 34

def self.get_class(class_name)
  @allowed_classes[class_name]
end

.included(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/engine/serialization/serializable.rb', line 11

def self.included(base)
  base.extend(ClassMethods)
  @allowed_classes[base.name] = base if base.name

  # Prevent subclasses from defining initialize
  base.define_singleton_method(:method_added) do |method_name|
    if method_name == :initialize
      raise InitializeNotAllowedError,
        "#{self.name} cannot define 'initialize'. " \
        "Serializable classes must use 'awake' for initialization logic and be created with '.create'"
    end
    super(method_name) if defined?(super)
  end
end

.register_class(klass) ⇒ Object



38
39
40
# File 'lib/engine/serialization/serializable.rb', line 38

def self.register_class(klass)
  @allowed_classes[klass.name] = klass if klass.name
end

Instance Method Details

#awakeObject

Default awake implementation - override in subclasses



27
28
# File 'lib/engine/serialization/serializable.rb', line 27

def awake
end

#uuidObject



74
75
76
# File 'lib/engine/serialization/serializable.rb', line 74

def uuid
  @uuid ||= SecureRandom.uuid
end