class Tally::KeyFinder::Entry

@!visibility private

Attributes

match[R]
raw_key[R]

Public Class Methods

new(match, raw_key, date) click to toggle source
# File lib/tally/key_finder/entry.rb, line 7
def initialize(match, raw_key, date)
  @match    = match
  @raw_key  = raw_key
  @date     = date if Date === date
end

Public Instance Methods

date() click to toggle source
# File lib/tally/key_finder/entry.rb, line 13
def date
  @date ||= build_date_from_raw_key
end
id() click to toggle source
# File lib/tally/key_finder/entry.rb, line 17
def id
  @id ||= if match[:record]
    match[:record].split(":").last.to_i
  end
end
key() click to toggle source
# File lib/tally/key_finder/entry.rb, line 23
def key
  match[:key]
end
record() click to toggle source
# File lib/tally/key_finder/entry.rb, line 27
def record
  return nil unless type.present?
  return nil unless id.present? && id > 0

  if model = type.classify.safe_constantize
    model.find_by(id: id)
  end
end
type() click to toggle source
# File lib/tally/key_finder/entry.rb, line 36
def type
  @type ||= if match[:record]
    match[:record].split(":").first
  end
end
value() click to toggle source
# File lib/tally/key_finder/entry.rb, line 42
def value
  Tally.redis do |conn|
    conn.get(key_for_value_lookup).to_i
  end
end

Private Instance Methods

build_date_from_raw_key() click to toggle source
# File lib/tally/key_finder/entry.rb, line 52
def build_date_from_raw_key
  if raw_key.to_s =~ /@/
    Date.parse(raw_key.to_s.split("@").last)
  end
end
key_for_value_lookup() click to toggle source
# File lib/tally/key_finder/entry.rb, line 58
def key_for_value_lookup
  if raw_key.starts_with?("#{ Tally.config.prefix }:")
    raw_key
  else
    "#{ Tally.config.prefix }:#{ raw_key }@#{ date&.strftime('%Y-%m-%d') }"
  end
end