class Eco::API::Common::Session::Logger::Cache

Constants

LEVELS

Public Class Methods

new() click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 9
def initialize
  reset
end

Public Instance Methods

add(level, datetime, message, formatted) click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 17
def add(level, datetime, message, formatted)
  Logger::Log.new(level, datetime, message, formatted).tap do |log|
    self.level(level).push(log)
  end
end
cache() click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 23
def cache
  @cache ||= {}
end
level(level) click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 13
def level(level)
  cache[to_level(level)] ||= []
end
logs(level: nil, start_time: nil, end_time: nil) click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 36
def logs(level: nil, start_time: nil, end_time: nil)
  where(start_time, end_time) do |cond|
    to_levels(level).map do |lev|
      self.level(lev).select(&cond)
    end.flatten
  end.sort
end
reset(level: nil, start_time: nil, end_time: nil) click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 27
def reset(level: nil, start_time: nil, end_time: nil)
  where(start_time, end_time) do |cond|
    to_levels(level).map do |lev|
      self.level(lev).reject(&cond)
    end
  end
  self
end

Private Instance Methods

nil_or_upcase(value) { |value| ... } click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 80
def nil_or_upcase(value)
  value = value.to_s.upcase if value
  return yield(value) if block_given?
  value
end
to_datetime(value) click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 57
def to_datetime(value)
  return nil unless value
  Time.parse(value)
end
to_level(value) click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 67
def to_level(value)
  nil_or_upcase(value).tap do |out|
    valid_level!(out)
  end
end
to_levels(value) click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 62
def to_levels(value)
  levels = [value].flatten.map {|v| to_level(v)}.compact
  levels = levels.empty?? LEVELS : levels
end
valid_level!(str) click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 73
def valid_level!(str)
  return true if !str
  unless LEVELS.any? {|lev| str == lev}
    raise "Unknown level #{str}. Should be one of #{LEVELS}"
  end
end
where(start_time, end_time) { |condition| ... } click to toggle source
# File lib/eco/api/common/session/logger/cache.rb, line 46
def where(start_time, end_time)
  tstart = to_datetime(start_time)
  tend   = to_datetime(end_time)
  condition = Proc.new do |log|
    next true unless tstart || tend
    log.after?(tstart) && log.before?(tend)
  end

  yield(condition)
end