Class: Draco::Entity::ComponentStore

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

Overview

Internal: An Array that notifies it’s parent of updates.

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ ComponentStore

Internal: Initializes a new ComponentStore

parent - The object to notify about updates.


213
214
215
216
# File 'lib/draco.rb', line 213

def initialize(parent)
  @components = {}
  @parent = parent
end

Instance Method Details

#<<(*components) ⇒ Object

Internal: Adds Components to the ComponentStore.

Side Effects: Notifies the parent that the components were updated.

components - The Component or Array list of Components to add to the ComponentStore.

Returns the ComponentStore.


225
226
227
228
229
# File 'lib/draco.rb', line 225

def <<(*components)
  components.flatten.each { |component| add(component) }

  self
end

#[](underscored_component) ⇒ Object

Internal: Returns the Component with the underscored Component name.

underscored_component - The String underscored version of the Component’s class name.

Returns the Component instance or nil.


236
237
238
# File 'lib/draco.rb', line 236

def [](underscored_component)
  @components[underscored_component]
end

#add(component) ⇒ Object

Internal: Adds a Component to the ComponentStore.

Side Effects: Notifies the parent that the components were updated.

components - The Component to add to the ComponentStore.

Returns the ComponentStore.


247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/draco.rb', line 247

def add(component)
  unless component.is_a?(Draco::Component)
    message = component.is_a?(Class) ? " You might need to initialize the component before you add it." : ""
    raise Draco::NotAComponentError, "The given value is not a component.#{message}"
  end

  component = @parent.before_component_added(component)
  name = Draco.underscore(component.class.name.to_s).to_sym
  @components[name] = component
  @parent.after_component_added(component)

  self
end

#delete(component) ⇒ Object

Internal: Removes a Component from the ComponentStore.

Side Effects: Notifies the parent that the components were updated.

components - The Component to remove from the ComponentStore.

Returns the ComponentStore.


268
269
270
271
272
273
274
275
# File 'lib/draco.rb', line 268

def delete(component)
  component = @parent.before_component_removed(component)
  name = Draco.underscore(component.class.name.to_s).to_sym
  @components.delete(name)
  @parent.after_component_removed(component)

  self
end

#each(&block) ⇒ Object

Internal: Returns an Enumerator for all of the Entities.


285
286
287
# File 'lib/draco.rb', line 285

def each(&block)
  @components.values.each(&block)
end

#empty?Boolean

Internal: Returns true if there are no entries in the Set.

Returns a boolean.

Returns:

  • (Boolean)

280
281
282
# File 'lib/draco.rb', line 280

def empty?
  @components.empty?
end