Sha256: 089612a6f4b53ffac9c0c874a854806e1a1a818f8308b485ebe23e019649d877

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Pragma
  module Macro
    class << self
      # Returns a skill or raises a {#MissingSkillError}.
      #
      # @param macro [String] the name of the macro requiring the skill
      # @param skill [String] the name of the string
      # @param options [Hash] the +options+ hash of the operation
      #
      # @return [Object] the value of the skill
      #
      # @raise [MissingSkillError] if the skill is undefined or +nil+
      #
      # @private
      def require_skill(macro, skill, options)
        options[skill] || fail(MissingSkillError.new(macro, skill))
      end
    end

    # Error raised when a skill is required but not present.
    #
    # @private
    class MissingSkillError < StandardError
      # Initializes the error.
      #
      # @param macro [String] the macro requiring the skill
      # @param skill [String] the name of the missing skill
      def initialize(macro, skill)
        message = <<~ERROR
          You are attempting to use the #{macro} macro, but no `#{skill}' skill is defined.

          You can define the skill by adding the following to your operation:

            self['#{skill}'] = MyCustomClass

          If the skill holds a class, this can happen when the required class (e.g. the contract
          class) is not in the expected location. If that's the case, you can just move the class to
          the expected location and avoid defining the skill manually.
        ERROR

        super message
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pragma-2.5.0 lib/pragma/macro.rb