require "./lib/class_attribute" module TinyState class Event include ClassAttribute tiny_class_attribute :from_states tiny_class_attribute :to_state attr_reader :resource, :attribute def self.transitions(from:, to:) self.from_states = [*from] self.to_state = to end def initialize(resource:, attribute:) @resource = resource @attribute = attribute end def transition! if !transition? raise InvalidTransitionError, "Invalid transition from #{current_state} to #{to_state}. Expected #{from_states}." end change_state! end def transition? = from_states.include?(current_state) def change_state! = @resource.send(:"#{attribute}=", to_state) private def current_state = @resource.send(attribute) end end