Class: Draco::Component
- Inherits:
-
Object
- Object
- Draco::Component
- Defined in:
- lib/draco.rb
Overview
Public: The data to associate with an Entity.
Class Attribute Summary collapse
-
.attribute_options ⇒ Object
readonly
Returns the value of attribute attribute_options.
Class Method Summary collapse
-
.attribute(name, options = {}) ⇒ Object
Public: Defines an attribute for the Component.
-
.inherited(sub) ⇒ Object
Internal: Resets the attribute options for each class that inherits Component.
-
.Tag(name) ⇒ Object
Public: Creates a tag Component.
Instance Method Summary collapse
-
#after_initialize ⇒ Object
Public: Callback run after the component is initialized.
-
#initialize(values = {}) ⇒ Component
constructor
Public: Initializes a new Component.
-
#inspect ⇒ Object
Public: Returns a String representation of the Component.
-
#serialize ⇒ Object
Public: Serializes the Component to save the current state.
-
#to_s ⇒ Object
Public: Returns a String representation of the Component.
Constructor Details
#initialize(values = {}) ⇒ Component
346 347 348 349 350 351 352 |
# File 'lib/draco.rb', line 346 def initialize(values = {}) self.class..each do |name, | value = values.fetch(name.to_sym, [:default]) instance_variable_set("@#{name}", value) end after_initialize end |
Class Attribute Details
.attribute_options ⇒ Object (readonly)
Returns the value of attribute attribute_options.
321 322 323 |
# File 'lib/draco.rb', line 321 def @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, = {}) attr_accessor name @attribute_options[name] = 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_initialize ⇒ Object
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 |
#inspect ⇒ Object
Public: Returns a String representation of the Component.
376 377 378 |
# File 'lib/draco.rb', line 376 def inspect serialize.to_s end |
#serialize ⇒ Object
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_s ⇒ Object
Public: Returns a String representation of the Component.
381 382 383 |
# File 'lib/draco.rb', line 381 def to_s serialize.to_s end |