Class: AcidicJob::IdempotencyKey

Inherits:
Object
  • Object
show all
Defined in:
lib/acidic_job/idempotency_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(identifier = :job_id) ⇒ IdempotencyKey

Returns a new instance of IdempotencyKey.



5
6
7
# File 'lib/acidic_job/idempotency_key.rb', line 5

def initialize(identifier = :job_id)
  @identifier = identifier
end

Instance Method Details

#value_for(hash_or_job, *args, **kwargs) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/acidic_job/idempotency_key.rb', line 9

def value_for(hash_or_job, *args, **kwargs)
  value = case @identifier
          when Proc
            value_from_proc(hash_or_job, *args, **kwargs)
          when :job_args
            value_from_job_args(hash_or_job, *args, **kwargs)
          else
            if hash_or_job.is_a?(Hash)
              value_from_job_id_for_hash(hash_or_job)
            else
              value_from_job_id_for_obj(hash_or_job)
            end
          end

  result = value || value_from_job_args(hash_or_job, *args, **kwargs)

  if result.start_with?("STG__")
    # "STG__#{idempotency_key}__#{encoded_global_id}"
    _prefix, idempotency_key, _encoded_global_id = result.split("__")
    idempotency_key
  else
    result
  end
end