Sha256: 189a4d8c195d6ada0d81cb440a15abc77f792eeb0b5fb0497ab2f62bace250b5

Contents?: true

Size: 1.92 KB

Versions: 39

Compression:

Stored size: 1.92 KB

Contents

module CanvasSync
  module JobBatches
    module RedisModel
      extend ActiveSupport::Concern

      class_methods do
        def redis_attr(key, type = :string, read_only: true)
          class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{key}=(value)
              raise "#{key} is read-only once the batch has been started" if #{read_only.to_s} && (@initialized || @existing)
              @#{key} = value
              if :#{type} == :json
                value = JSON.unparse(value)
              end
              persist_bid_attr('#{key}', value)
            end

            def #{key}
              return @#{key} if defined?(@#{key})
              if (@initialized || @existing)
                value = read_bid_attr('#{key}')
                if :#{type} == :bool
                  value = value == 'true'
                elsif :#{type} == :int
                  value = value.to_i
                elsif :#{type} == :float
                  value = value.to_f
                elsif :#{type} == :json
                  value = JSON.parse(value)
                elsif :#{type} == :symbol
                  value = value&.to_sym
                end
                @#{key} = value
              end
            end
          RUBY
        end
      end

      def persist_bid_attr(attribute, value)
        if @initialized || @existing
          redis do |r|
            r.multi do
              r.hset(redis_key, attribute, value)
              r.expire(redis_key, Batch::BID_EXPIRE_TTL)
            end
          end
        else
          @pending_attrs ||= {}
          @pending_attrs[attribute] = value
        end
      end

      def read_bid_attr(attribute)
        redis do |r|
          r.hget(redis_key, attribute)
        end
      end

      def flush_pending_attrs
        redis do |r|
          r.mapped_hmset(redis_key, @pending_attrs)
        end
        @initialized = true
        @pending_attrs = {}
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
canvas_sync-0.17.29 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.28 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.27 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.27.beta1 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.26.beta1 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.24 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.23 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.23.beta8 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.23.beta7 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.23.beta6 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.23.beta5 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.23.beta4 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.23.beta2 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.23.beta1 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.20 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.19 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.18 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.17.beta1 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.16 lib/canvas_sync/job_batches/redis_model.rb
canvas_sync-0.17.15 lib/canvas_sync/job_batches/redis_model.rb