Sha256: 35b57f200589e988b2f02808962ee22deb2605687ca3bd3b8f14afcd37cb1da0

Contents?: true

Size: 819 Bytes

Versions: 1

Compression:

Stored size: 819 Bytes

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tiny_state-0.2.0 lib/tiny_state/event.rb