Sha256: bd5ccef257ef1841abcc4d21e44280ab8bf3362ba656c75e8f79c766f4df5ae2

Contents?: true

Size: 1.94 KB

Versions: 52

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Cloudtasker
  # Manage meta information on workers. This meta stored is intended
  # to be used by middlewares needing to store extra information on the
  # job.
  # The objective of this class is to provide a shared store to middleware
  # while controlling access to its keys by preveenting access the hash directly
  # (e.g. avoid wild merge or replace operations).
  class MetaStore
    #
    # Build a new instance of the class.
    #
    # @param [<Type>] hash The worker meta hash
    #
    def initialize(hash = {})
      @meta = JSON.parse((hash || {}).to_json, symbolize_names: true)
    end

    #
    # Retrieve meta entry.
    #
    # @param [String, Symbol] key The key of the meta entry.
    #
    # @return [Any] The value of the meta entry.
    #
    def get(key)
      @meta[key.to_sym] if key
    end

    #
    # Set meta entry
    #
    # @param [String, Symbol] key The key of the meta entry.
    # @param [Any] val The value of the meta entry.
    #
    # @return [Any] The value set
    #
    def set(key, val)
      @meta[key.to_sym] = val if key
    end

    #
    # Remove a meta information.
    #
    # @param [String, Symbol] key The key of the entry to delete.
    #
    # @return [Any] The value of the deleted key
    #
    def del(key)
      @meta.delete(key.to_sym) if key
    end

    #
    # Return the meta store as Hash.
    #
    # @return [Hash] The meta store as Hash.
    #
    def to_h
      # Deep dup
      JSON.parse(@meta.to_json, symbolize_names: true)
    end

    #
    # Return the meta store as json.
    #
    # @param [Array<any>] *arg The to_json args.
    #
    # @return [String] The meta store as json.
    #
    def to_json(*arg)
      @meta.to_json(*arg)
    end

    #
    # Equality operator.
    #
    # @param [Any] other The object being compared.
    #
    # @return [Boolean] True if the object is equal.
    #
    def ==(other)
      to_json == other.try(:to_json)
    end
  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
cloudtasker-0.14.rc1 lib/cloudtasker/meta_store.rb
cloudtasker-0.13.2 lib/cloudtasker/meta_store.rb
cloudtasker-0.13.1 lib/cloudtasker/meta_store.rb
cloudtasker-0.13.0 lib/cloudtasker/meta_store.rb
cloudtasker-0.13.rc2 lib/cloudtasker/meta_store.rb
cloudtasker-0.13.rc1 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.2 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.1 lib/cloudtasker/meta_store.rb
cloudtasker-0.11.1 lib/cloudtasker/meta_store.rb
cloudtasker-0.10.2 lib/cloudtasker/meta_store.rb
cloudtasker-0.9.5 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.0 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.rc11 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.rc10 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.rc9 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.rc8 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.rc7 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.rc6 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.rc5 lib/cloudtasker/meta_store.rb
cloudtasker-0.12.rc4 lib/cloudtasker/meta_store.rb