Module: Draco::State::EntityPlugin

Defined in:
lib/draco/state.rb

Instance Method Summary collapse

Instance Method Details

#after_component_added(component) ⇒ Object


55
56
57
58
59
60
61
62
# 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_initializeObject


35
36
37
38
# 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) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object


64
65
66
67
68
69
70
71
# 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