Class: Draco::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/draco.rb

Overview

Public: The data to associate with an Entity.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Component

Public: Initializes a new Component.

values - The Hash of values to set for the Component instance (default: {}).

Each key should be the Symbol name of the attribute.

Examples

class Position < Draco::Component
  attribute :x, default: 0
  attribute :y, default: 0
end

Position.new(x: 100, y: 100)


346
347
348
349
350
351
352
# File 'lib/draco.rb', line 346

def initialize(values = {})
  self.class.attribute_options.each do |name, options|
    value = values.fetch(name.to_sym, options[:default])
    instance_variable_set("@#{name}", value)
  end
  after_initialize
end

Class Attribute Details

.attribute_optionsObject (readonly)

Returns the value of attribute attribute_options.



321
322
323
# File 'lib/draco.rb', line 321

def attribute_options
  @attribute_options
end

Class Method Details

.attribute(name, options = {}) ⇒ Object

Public: Defines an attribute for the Component.

name - The Symbol name of the attribute. options - The Hash options for the Component (default: {}):

:default - The initial value for the attribute if one is not provided.

Returns nothing.



313
314
315
316
317
# File 'lib/draco.rb', line 313

def self.attribute(name, options = {})
  attr_accessor name

  @attribute_options[name] = options
end

.inherited(sub) ⇒ Object

Internal: Resets the attribute options for each class that inherits Component.

sub - The class that is inheriting Entity.

Returns nothing.



301
302
303
304
# File 'lib/draco.rb', line 301

def self.inherited(sub)
  super
  sub.instance_variable_set(:@attribute_options, {})
end

.Tag(name) ⇒ Object

Public: Creates a tag Component. If the tag already exists, return it.

name - The string or symbol name of the component.

Returns a class with subclass Draco::Component.



329
330
331
# File 'lib/draco.rb', line 329

def self.Tag(name) # rubocop:disable Naming/MethodName
  Draco::Tag(name)
end

Instance Method Details

#after_initializeObject

Public: Callback run after the component is initialized.

This is empty by default but is present to allow plugins to tie into.

Returns nothing.



359
# File 'lib/draco.rb', line 359

def after_initialize; end

#inspectObject

Public: Returns a String representation of the Component.



376
377
378
# File 'lib/draco.rb', line 376

def inspect
  serialize.to_s
end

#serializeObject

Public: Serializes the Component to save the current state.

Returns a Hash representing the Component.



364
365
366
367
368
369
370
371
372
373
# File 'lib/draco.rb', line 364

def serialize
  attrs = { class: self.class.name.to_s }

  instance_variables.each do |attr|
    name = attr.to_s.gsub("@", "").to_sym
    attrs[name] = instance_variable_get(attr)
  end

  attrs
end

#to_sObject

Public: Returns a String representation of the Component.



381
382
383
# File 'lib/draco.rb', line 381

def to_s
  serialize.to_s
end