Class: Cloudtasker::MetaStore

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudtasker/meta_store.rb

Overview

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).

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ MetaStore

Build a new instance of the class.

Parameters:

  • hash (<Type>) (defaults to: {})

    The worker meta hash



16
17
18
# File 'lib/cloudtasker/meta_store.rb', line 16

def initialize(hash = {})
  @meta = JSON.parse((hash || {}).to_json, symbolize_names: true)
end

Instance Method Details

#==(other) ⇒ Boolean

Equality operator.

Parameters:

  • other (Any)

    The object being compared.

Returns:

  • (Boolean)

    True if the object is equal.



82
83
84
# File 'lib/cloudtasker/meta_store.rb', line 82

def ==(other)
  to_json == other.try(:to_json)
end

#del(key) ⇒ Any

Remove a meta information.

Parameters:

  • key (String, Symbol)

    The key of the entry to delete.

Returns:

  • (Any)

    The value of the deleted key



50
51
52
# File 'lib/cloudtasker/meta_store.rb', line 50

def del(key)
  @meta.delete(key.to_sym) if key
end

#get(key) ⇒ Any

Retrieve meta entry.

Parameters:

  • key (String, Symbol)

    The key of the meta entry.

Returns:

  • (Any)

    The value of the meta entry.



27
28
29
# File 'lib/cloudtasker/meta_store.rb', line 27

def get(key)
  @meta[key.to_sym] if key
end

#set(key, val) ⇒ Any

Set meta entry

Parameters:

  • key (String, Symbol)

    The key of the meta entry.

  • val (Any)

    The value of the meta entry.

Returns:

  • (Any)

    The value set



39
40
41
# File 'lib/cloudtasker/meta_store.rb', line 39

def set(key, val)
  @meta[key.to_sym] = val if key
end

#to_hHash

Return the meta store as Hash.

Returns:

  • (Hash)

    The meta store as Hash.



59
60
61
62
# File 'lib/cloudtasker/meta_store.rb', line 59

def to_h
  # Deep dup
  JSON.parse(@meta.to_json, symbolize_names: true)
end

#to_json(*arg) ⇒ String

Return the meta store as json.

Parameters:

  • *arg (Array<any>)

    The to_json args.

Returns:

  • (String)

    The meta store as json.



71
72
73
# File 'lib/cloudtasker/meta_store.rb', line 71

def to_json(*arg)
  @meta.to_json(*arg)
end