Sha256: 27fabf4db2b97719236e830baaed784ce298569045f04ce2cf95cfe98f6974e2

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

# namespace
module TeeLogger
  # util
  module Utils
    # @param options [Array]
    # @return [ParsedOption]
    def extract_options(options)
      options.each_with_object(ParsedOption.new(nil, 0)) do |val, obj|
        case val
        when Symbol then obj.logdev_name = name_reverse(val)
        when Integer then obj.indent_level = val
        else incorrect_option_error(val)
        end
      end
    end

    # @param progname
    # @param block
    # @param indent_level [Fixnum]
    # @return [Array]
    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

    # @param val [Object]
    # @return [String]
    def formatting(val)
      case val
      when Symbol then ":#{val}"
      when nil    then 'nil'
      else val
      end
    end

    # @param val [Symbol]
    # @return [Symbol]
    def name_reverse(val)
      correct_name?(val)
      LOGDEV_REVERSE[val]
    end

    # @param name [Symbol]
    # @return [true]
    def correct_name?(name)
      LOGDEV_NAMES.include?(name) ? true : incorrect_name_error(name)
    end

    # @param name [Symbol]
    def incorrect_name_error(name)
      fail IncorrectNameError,
           "logdev_name is :console or :logfile. logdev_name=[:#{name}]"
    end

    # @param val [Symbol]
    def incorrect_option_error(val)
      fail IncorrectOptionError,
           "option params is Symbol or Fixnum. class=[#{val.class}]"
    end

    # @param logdev_name [Symbol]
    # @return [Logger]
    def logdev_instance(logdev_name)
      instance_variable_get("@#{logdev_name}")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tee_logger-3.2.4 lib/tee_logger/utils.rb
tee_logger-3.2.3 lib/tee_logger/utils.rb