Sha256: 3c8a7c51e75e3915827245b32c9a8471bb8a70e6242866a1b66c45eff369124d

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

require 'ansi'

module SearchKit
  class Messages
    # The goal of the Messaging module is to provide an easy to include internal
    # interface which will allow a SearchKit gem to dutifully log and provide
    # output of what it's up to and how it may be doing.
    #
    module Messaging
      def info(message)
        Message.new(message).info
      end

      def warning(message)
        Message.new(message).warn
      end

      private

      # Most of the logic for the Messaging module exists in this (not so)
      # private class.  This lets more complex handling of message logic enter
      # into the module gracefully, for example silence or logging level.
      #
      class Message
        attr_reader :message

        def initialize(message)
          @message = message
        end

        def warn
          Kernel.warn(Prefixed(message.ansi(:red))) if SearchKit.config.verbose
          SearchKit.logger.warn message
        end

        def info
          Kernel.puts(Prefixed(message.ansi(:cyan))) if SearchKit.config.verbose
          SearchKit.logger.info message
        end

        private

        def Prefixed(*messages)
          Prefixed.new.join(*messages)
        end

        class Prefixed
          attr_reader :body

          def initialize
            env   = SearchKit.config.app_env.to_s.ansi(:magenta)
            @body = "--> [ #{env} ]: "
          end

          def join(*messages)
            [body, *messages].join(" ")
          end

        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
search-kit-0.0.5 lib/search_kit/messages/messaging.rb
search-kit-0.0.4 lib/search_kit/messages/messaging.rb
search-kit-0.0.3 lib/search_kit/messages/messaging.rb