Sha256: d6c1166160c6a15e0ae4545497f6372a1585b5bb0a8533813773cfe703ee9399

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module SimpleRedisOrm
  class NotConnected < StandardError; end

  class Entry < Dry::Struct
    include Helpers::CoreCommands
    extend Helpers::CoreCommands::ClassMethods

    attribute :key, Types::String

    def id
      self.class.without_redis_key_prefix(key)
    end

    class << self
      def new(id:, **attributes)
        attrs_with_defaults = fill_in_missing_keys(**attributes)
        super(key: id, **attrs_with_defaults)
      end

      def create(id:, **attributes)
        new(id: id, **attributes).save
      end

      def find(id)
        value = read_by(id)
        return if value.nil?

        new(id: id, **value)
      end

      def redis
        @redis || raise(NotConnected, "#{name}.redis not set to a Redis.new connection pool")
      end

      def redis=(conn)
        @redis = ConnectionPoolProxy.proxy_if_needed(conn)
      end

      def without_redis_key_prefix(id)
        return if id.nil?

        id.gsub(/^(.*):/, '')
      end

      private

      def fill_in_missing_keys(**attributes)
        keys = attribute_names - [:key]
        missing_keys = keys - attributes.keys
        return attributes if missing_keys.empty?

        missing_hash = missing_keys.to_h { |x| [x, nil] }
        missing_hash.merge(attributes)
      end
    end

    def attributes
      super.except(:key)
    end

    def redis
      self.class.redis
    end

    def save
      set_hash(attributes.except(:key))

      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple-redis-orm-0.1.3 lib/simple-redis-orm/entry.rb