Class: FelFlame::Helper::ComponentManager

Inherits:
Object
  • Object
show all
Defined in:
component_manager.rb

Overview

Component Managers are what is used to create individual components which can be attached to entities. When a Component is created from a Component Manager that has accessors given to it, you can set or get the values of those accessors using standard ruby message sending, or by using the #attrs and #update_attrs methods instead.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



45
46
47
# File 'component_manager.rb', line 45

def id
  @id
end

Class Method Details

.[](val) ⇒ Object



52
53
54
# File 'component_manager.rb', line 52

def [](val)
  data[val]
end

.dataObject



48
49
50
# File 'component_manager.rb', line 48

def data
  @data ||= []
end

.eachObject



73
74
75
76
77
# File 'component_manager.rb', line 73

def each
  data.each do |component|
    yield component
  end
end

.new(**attrs) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'component_manager.rb', line 56

def new(**attrs)
  new_component = super

  # Generate ID
  new_id = self.data.find_index { |i| i.nil? }
  new_id = self.data.size if new_id.nil?
  new_component.id = new_id

  # Fill params
  attrs.each do |key, value|
    new_component.send "#{key}=", value
  end

  # Save Component
  data[new_id] = new_component
end

Instance Method Details

#attrsObject

def initialize_attr(name, default = nil)

#TODO: fill this out
#TODO: This might be bad form, maybe exclude completly

end



114
115
116
117
118
# File 'component_manager.rb', line 114

def attrs #TODO: maybe optimize removing the @ symbol
  instance_variables.each_with_object({}) do |key, final|
    final[key.to_s.delete_prefix('@').to_sym] = instance_variable_get(key)
  end
end

#deleteObject

Returns true



96
97
98
99
100
101
102
103
104
105
106
107
# File 'component_manager.rb', line 96

def delete
  #TODO: Remove component
  linked_entities.each do |entity_id|
    FelFlame::Entities[entity_id].remove self
  end
  self.class.data[id] = nil
  @linked_entities = nil
  instance_variables.each do |var|
    instance_variable_set(var, nil)
  end
  true
end

#linked_entitiesObject



84
85
86
# File 'component_manager.rb', line 84

def linked_entities
  @linked_entities ||= []
end

#to_iObject



80
81
82
# File 'component_manager.rb', line 80

def to_i
  id
end

#to_jsonObject

TODO: Needs to get id and stuff?



120
121
122
# File 'component_manager.rb', line 120

def to_json #TODO: Needs to get id and stuff?
  # should return a json or hash of all data in this component
end

#update_attrs(**opts) ⇒ Object

returns hash of updated attributes



89
90
91
92
93
# File 'component_manager.rb', line 89

def update_attrs(**opts)
  opts.each do |key, value|
    send "#{key}=", value
  end
end