Sha256: 90e6d05a9d702a10ef825d1301a02271888df3d0e226aebf547f0b01bba84daa

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Bario
  # Internal storage backed in redis for bar
  class Storage < Ohm::Model
    attribute :name
    index :name

    attribute :root
    index :root

    attribute :total
    attribute :current
    counter :current

    list :children, "Bario::Storage"
    reference :parent, "Bario::Storage"

    # Extracted from ohm-contrib
    Timestamp = ->(time) { time && time.to_i.to_s }
    attribute :created_at, Timestamp
    attribute :updated_at, Timestamp

    class << self
      def time_now
        Time.now.utc.to_i
      end
    end

    def save
      time_now = self.class.time_now

      self.created_at = time_now if new?
      self.updated_at = time_now
      super
    end

    # Override increment update `updated_at` attribute
    # `decrement` call to `increment`.
    def increment(att, count = 1)
      time_now = self.class.time_now

      redis.queue("HINCRBY", key[:counters], att, count)
      redis.queue("HSET", key, "updated_at", time_now)

      self.updated_at = time_now
      redis.commit
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bario-0.1.2 lib/bario/storage.rb
bario-0.1.1 lib/bario/storage.rb