Sha256: 252e217db7ad47d07919d3db2c37fc4ff75ab65a5adad29ed3c30aff5073c716

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Brandish
  module Processor
    # An inline-defined processor.  This allows processors to be defined easily
    # within a block.  This is mostly used for configuration.
    #
    # @example
    #   class RFC < Brandish::Processor::Inline
    #     command! :rfc
    #     pairs :number
    #
    #     perform do
    #       number = @pairs.fetch("number")
    #       "<a href='https://www.ietf.org/rfc/rfc#{number}.txt'>RFC #{number}</a>"
    #     end
    #   end
    class Inline < Base
      # Sets this processor as a command processor.  This includes
      # {Command}, and passes the given names to 
      # {NameFilter::ClassMethods#name}.
      #
      # @return [void]
      def self.command!(*names)
        include Command
        name(*names)
      end

      # Sets this processor as a block processor.  This include {Block}, and
      # passes the given names to {NameFilter::ClassMethods#name}.
      #
      # @return [void]
      def self.block!(*names)
        include Block
        name(*names)
      end

      # Defines the `#perform` method with the given block.  This is only
      # effective if one of {Command} or {Block} is included.  This takes the
      # block passed to it and uses that to define the `#perform` method.
      #
      # @return [void]
      def self.perform(&block)
        define_method(:perform, &block)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
brandish-0.1.3 lib/brandish/processor/inline.rb
brandish-0.1.2 lib/brandish/processor/inline.rb
brandish-0.1.1 lib/brandish/processor/inline.rb