Sha256: 602c4ea780bbb45f4fef3f47ad276a2b0e08c4cf044f4b5d3f15a6bc7c96819d

Contents?: true

Size: 980 Bytes

Versions: 6

Compression:

Stored size: 980 Bytes

Contents

require "active_support/concern"

module Tally
  module Keyable

    extend ActiveSupport::Concern

    included do
      attr_reader :key
      attr_reader :record
    end

    def initialize(key, record = nil)
      @key = key.to_s.gsub(":", ".").downcase.strip
      @record = record
    end

    def day
      @day ||= Time.current.utc.to_date
    end

    private

      def daily_key
        "#{ prefix }@#{ date_key }"
      end

      def date_key
        @date_key ||= day.strftime(Tally.config.date_format)
      end

      def prefix
        Tally.config.prefix
      end

      def record_key
        if record
          "#{ record.model_name.i18n_key }:#{ record.id }"
        end
      end

      def redis_key
        @redis_key ||= "#{ prefix }:#{ simple_key }@#{ date_key }"
      end

      def simple_key
        @simple_key ||= if record.respond_to?(:model_name)
          "#{ record_key }:#{ key }"
        else
          key
        end
      end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tally-2.1.0 lib/tally/keyable.rb
tally-2.0.0 lib/tally/keyable.rb
tally-1.0.2 lib/tally/keyable.rb
tally-1.0.1 lib/tally/keyable.rb
tally-1.0.0 lib/tally/keyable.rb
tally-1.0.0.beta1 lib/tally/keyable.rb