Sha256: c429952b7636924e0800a4ce711bcf116656cf5dd339c953152dfd8e8b7026c2

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

#:nodoc:
module Zen
  #:nodoc:
  module Plugin
    ##
    # Module containing various methods that can be used for developing plugins.
    #
    # @author Yorick Peterse
    # @since  0.2.5
    #
    module Helper

      ##
      # Method that can be used to validate the class of a given variable. If the class
      # isn't included in the whitelist an error will be triggered explaining the error.
      #
      # @example
      #  username = 10
      #  validate_type(username, :username, [String]) # => TypeError: "\"username\" can only
      #  be an instance of String but got Integer."
      #
      # @author Yorick Peterse
      # @since  0.2.5
      # @param  [Mixed] variable The variable to validate.
      # @param  [Symbol/String] name The name of the variable to validate.
      # @param  [Array] whitelist Whitelist of the allowed classes.
      # @raise  TypeError Raised whenever the variable's class wasn't allowed.
      #
      def validate_type(variable, name, whitelist)
        if whitelist.class != Array
          whitelist = [whitelist]
        end

        name    = name.to_s
        classes = whitelist.join(' or ').to_s

        if !whitelist.include?(variable.class)
          raise(
            TypeError, 
            "\"#{name}\" can only be an instance of #{classes} but got #{variable.class}"
          )
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
zen-0.2.8 lib/zen/plugin/helper.rb
zen-0.2.7 lib/zen/plugin/helper.rb
zen-0.2.6.1 lib/zen/plugin/helper.rb
zen-0.2.6 lib/zen/plugin/helper.rb
zen-0.2.5 lib/zen/plugin/helper.rb