Sha256: cd4bed303cf47e62894ef5c3611061cb1904db4f9ff012b909c6e5e9e696a54d

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require "redis-objects"
require "singleton"
require "dry/inflector"

# One storage per One Pipiline
# One Storage type per each Pipeline type

module Strum
  module Pipeline
    # Class to w/r to store
    class Storage
      attr_accessor :pipeline, :ttl

      def initialize(pipeline, ttl)
        @pipeline = pipeline
        @ttl = ttl
      end

      def method_missing(method)
        that = self
        Class.new do
          define_singleton_method :[] do |key|
            redis_entity = Redis.const_get(Dry::Inflector.new.camelize(method)).new("#{that.pipeline}::#{key}")
            redis_entity.expire(that.ttl)
            redis_entity
          end
        end
      rescue StandardError
        super
      end

      def respond_to_missing?(method)
        Redis.const_get(Dry::Inflector.new.camelize(method))
      rescue StandardError
        super
      end

      def [](key)
        Redis::Value.new("#{pipeline}::#{key}").value
      end

      def []=(key, value)
        redis_value = Redis::Value.new("#{pipeline}::#{key}")
        redis_value.value = value
        redis_value.expire(ttl)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
strum-pipeline-0.1.1 lib/strum/pipeline/storage.rb
strum-pipeline-0.1.0 lib/strum/pipeline/storage.rb