Sha256: 44de64baef118542678ac688d4841c3b2ecde3f55af88a1a29f15c2d946780c0

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Gamefic
  class Rulebook
    # A collection of hooks that can be executed before and after an action.
    #
    class Hooks
      attr_reader :before_actions

      attr_reader :after_actions

      def initialize
        @before_actions = []
        @after_actions = []
      end

      def freeze
        super
        @before_actions.freeze
        @after_actions.freeze
        self
      end

      def before_action *verbs, &block
        before_actions.push Action::Hook.new(*verbs, &block)
      end

      def after_action *verbs, &block
        after_actions.push Action::Hook.new(*verbs, &block)
      end

      def empty?
        before_actions.empty? && after_actions.empty?
      end

      def run_before action, narrative
        run_action_hooks action, narrative, before_actions
      end

      def run_after action, narrative
        run_action_hooks action, narrative, after_actions
      end

      private

      def run_action_hooks action, narrative, hooks
        hooks.each do |hook|
          break if action.cancelled?

          next unless hook.match?(action.verb)

          Stage.run(narrative) { instance_exec(action, &hook.block) }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gamefic-3.3.0 lib/gamefic/rulebook/hooks.rb
gamefic-3.2.1 lib/gamefic/rulebook/hooks.rb
gamefic-3.2.0 lib/gamefic/rulebook/hooks.rb
gamefic-3.1.0 lib/gamefic/rulebook/hooks.rb
gamefic-3.0.0 lib/gamefic/rulebook/hooks.rb