Module: SuperState::InstanceMethods

Defined in:
lib/super_state.rb

Instance Method Summary collapse

Instance Method Details

#current_super_stateObject



86
87
88
# File 'lib/super_state.rb', line 86

def current_super_state
  self[self.class.super_state_column] || SuperState.canonicalise(self.class.initial_super_state)
end

#ensure_super_state!(state_name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/super_state.rb', line 114

def ensure_super_state!(state_name)
  self.transaction do
    self.lock!

    unless self.current_super_state == SuperState.canonicalise(state_name)
      raise BadState, "the super state is not what we expected"
    end
      
    yield
  end
end

#human_super_stateObject



104
105
106
# File 'lib/super_state.rb', line 104

def human_super_state
  self.current_super_state.humanize
end

#set_initial_super_stateObject



90
91
92
# File 'lib/super_state.rb', line 90

def set_initial_super_state
  set_super_state(self.class.initial_super_state)
end

#set_super_state(state_name, set_timestamps = true) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/super_state.rb', line 94

def set_super_state(state_name, set_timestamps=true)
  self[self.class.super_state_column] = SuperState.canonicalise(state_name)
  
  if set_timestamps && self.respond_to?("#{state_name}_at=")
    self.send("#{state_name}_at=", Time.now)
  end
  
  self.current_super_state
end

#validate_super_stateObject



108
109
110
111
112
# File 'lib/super_state.rb', line 108

def validate_super_state
  unless self.class.__super_states.include?(self.current_super_state)
    errors[self.class.super_state_column] << "is not a valid super state"
  end
end