module Draco::State::EntityPlugin

Public Instance Methods

after_component_added(component) click to toggle source
Calls superclass method
# File lib/draco/state.rb, line 55
def after_component_added(component)
  component = super || component
  options = self.class.instance_variable_get(:@state_options)

  return component unless options.include?(component.class)
  state_change = state_change_component
  components.delete(state_change.from) if state_change
end
after_initialize() click to toggle source
Calls superclass method
# File lib/draco/state.rb, line 35
def after_initialize
  super
  components.add(self.class.instance_variable_get(:@default_state))
end
before_component_added(component) click to toggle source
Calls superclass method
# File lib/draco/state.rb, line 40
def before_component_added(component)
  component = super || component
  options = self.class.instance_variable_get(:@state_options)
  return component unless options.include?(component.class)

  from = previous_state
  if !has_state_change?(component) && from
    state_change = Draco::StateChanged.new(from: from, to: component, at: Time.now)

    components.add(state_change)
  end

  component
end
before_component_removed(component) click to toggle source
Calls superclass method
# File lib/draco/state.rb, line 64
def before_component_removed(component)
  component = super || component
  options = self.class.instance_variable_get(:@state_options)

  return component unless options.include?(component.class)
  state_change = state_change_component
  raise Draco::State::StateNotSetError, "removing #{component.inspect} would leave this entity in an invalid state" unless state_change && state_change.from == component
end

Private Instance Methods

has_state_change?(component) click to toggle source
# File lib/draco/state.rb, line 79
def has_state_change?(component)
  return false unless state_change_component
  return state_change_component.to == component
end
previous_state() click to toggle source
# File lib/draco/state.rb, line 84
def previous_state
  name =
    self.class.instance_variable_get(:@state_options)
    .map { |state| Draco.underscore(state.name.to_s).to_sym }
    .find { |name| components[name] }

  components[name] if name
end
state_change_component() click to toggle source
# File lib/draco/state.rb, line 74
def state_change_component
  state_change_name = Draco.underscore(Draco::StateChanged.name.to_s).to_sym
  components[state_change_name]
end