Sha256: 045016f1983b6af93efb0820535241f3261e3d101ffdfec905615810e5c8fc55

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 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.const_get(Dry::Inflector.new.camelize(method)).new("#{that.pipeline}::#{key}", expiration: that.ttl)
          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.new("#{pipeline}::#{key}", expiration: ttl).value = value
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
strum-pipeline-0.3.0 lib/strum/pipeline/storage.rb
strum-pipeline-0.2.2 lib/strum/pipeline/storage.rb
strum-pipeline-0.2.1 lib/strum/pipeline/storage.rb
strum-pipeline-0.2.0 lib/strum/pipeline/storage.rb
strum-pipeline-0.1.4 lib/strum/pipeline/storage.rb
strum-pipeline-0.1.3 lib/strum/pipeline/storage.rb