Class: FlexCommerce::SurrogateKeys

Inherits:
Object
  • Object
show all
Defined in:
app/services/surrogate_keys.rb

Class Method Summary collapse

Class Method Details

.append_keys(*keys) ⇒ Object

Appends the keys to the Thread shift_surrogate_keys variable.

Examples

Thread.current[:shift_surrogate_keys] = ["matalanb%2Fcarts", "matalanb%2Fcart%2F31719184"]
keys = ["matalanb%2Fcarts", "matalanb%2Fcart%2F31719184"]

FlexCommerce::SurrogateKeys.append_keys(keys)
    # => ["matalanb%2Fcarts", "matalanb%2Fcart%2F31719184", "matalanb%2Fcarts", "matalanb%2Fcart%2F31719184"]


36
37
38
39
40
41
# File 'app/services/surrogate_keys.rb', line 36

def self.append_keys(*keys)
  # Check if surrogate key is available, if not initiate it
  Thread.current[:shift_surrogate_keys] ||= []
  Thread.current[:shift_surrogate_keys] += keys.flatten
  Thread.current[:shift_surrogate_keys] = Thread.current[:shift_surrogate_keys].uniq
end

.keys(filter: nil) ⇒ Object

Returns the filtered external surrogate keys from api response, if no filter value is set, it will return all keys

Examples
Thread.current[:shift_surrogate_keys] = ["matalanb%2Fcarts", "matalanb%2Fshipping_methods", "matalanb%2Fpromotion_shipping_methods"]

FlexCommerce::SurrogateKeys.keys(filter: 'cart')
  # => ["matalanb%2Fcarts"]
FlexCommerce::SurrogateKeys.keys
  # => ["matalanb%2Fcarts", "matalanb%2Fshipping_methods", "matalanb%2Fpromotion_shipping_methods"]


19
20
21
22
23
24
25
# File 'app/services/surrogate_keys.rb', line 19

def self.keys(filter: nil)
  surrogate_keys = Thread.current[:shift_surrogate_keys]
  # Return all the surrogate keys, if not requested for a particular key
  return surrogate_keys if filter.nil?
  # Select only the surrogate keys, which includes the requested key
  surrogate_keys.nil?  ? [] : surrogate_keys.select { |k| k.include?(filter) }
end