Sha256: 394bae9ee0e8180d1f516979ae2b7b3c795c44f75a6e31a40d742f862048b6e8

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# namespace
module TeeLogger
  # util
  module Utils
    class IncorrectNameError < StandardError; end

    # defined log devices names
    LOGDEVS = %i(console logfile)

    private

    def parse_to_hash_from(options)
      result = { enabling_target: nil, indent_level: 0 }
      options.each_with_object(result) do |val, obj|
        if val.is_a?(Symbol)
          correct_name?(val)
          obj[:enabling_target] = LOGDEVS.include?(val) ? val : nil
        elsif val.is_a?(Fixnum)
          obj[:indent_level] = val
        end
      end
    end

    def reverse_target(logdev_name)
      case logdev_name
      when :console then :logfile
      when :logfile then :console
      end
    end

    def indentation(progname, block, indent_level)
      if block.nil?
        progname = "#{' ' * indent_level}#{formatting(progname)}"
      else
        result = block.call
        block  = proc { "#{' ' * indent_level}#{formatting(result)}" }
      end
      [progname, block]
    end

    def formatting(val)
      if val.is_a?(Symbol)
        ":#{val}"
      elsif val.nil?
        'nil'
      else
        val
      end
    end

    def correct_name?(logdev_name)
      if LOGDEVS.include?(logdev_name)
        true
      else
        fail IncorrectNameError,
             "logdev_name=[:#{logdev_name}]:logdev_name is :console or :logfile"
      end
    end

    def logdev_instance(logdev_name)
      instance_variable_get("@#{logdev_name}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tee_logger-3.0.0 lib/tee_logger/utils.rb