Class: FMCache::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/fmcache/helper.rb

Constant Summary collapse

KEY_PREFIX =
"fmcache:"

Class Method Summary collapse

Class Method Details

.sort(values, ids) ⇒ <Hash>

Parameters:

  • values (<Hash>)
  • ids (<Integer>)

Returns:

  • (<Hash>)


44
45
46
47
48
49
# File 'lib/fmcache/helper.rb', line 44

def sort(values, ids)
  id_map = ids.map.with_index { |id, i| [id, i] }.to_h
  values.sort do |a, b|
    id_map.fetch(a.fetch(:id)) <=> id_map.fetch(b.fetch(:id))
  end
end

.to_fields(field_mask, prefix: []) ⇒ <String>

Parameters:

  • field_mask (FieldMaskParser::Node)
  • prefix (<Symbol>) (defaults to: [])

Returns:

  • (<String>)


9
10
11
# File 'lib/fmcache/helper.rb', line 9

def to_fields(field_mask, prefix: [])
  field_mask.to_paths(prefix: prefix, sort: false)
end

.to_id(key) ⇒ Integer

Parameters:

  • id (String)

Returns:

  • (Integer)


33
34
35
36
37
38
39
# File 'lib/fmcache/helper.rb', line 33

def to_id(key)
  if key[0..7] == KEY_PREFIX
    key[8..-1].to_i
  else
    raise "invalid key: #{key}"
  end
end

.to_ids(keys) ⇒ <Integer>

Parameters:

  • keys (<String>)

Returns:

  • (<Integer>)


27
28
29
# File 'lib/fmcache/helper.rb', line 27

def to_ids(keys)
  keys.map { |key| to_id(key) }
end

.to_key(id) ⇒ String

Parameters:

  • id (Integer)

Returns:

  • (String)


21
22
23
# File 'lib/fmcache/helper.rb', line 21

def to_key(id)
  "#{KEY_PREFIX}#{id}"
end

.to_keys(ids) ⇒ <String>

Parameters:

  • ids (<Integer>)

Returns:

  • (<String>)


15
16
17
# File 'lib/fmcache/helper.rb', line 15

def to_keys(ids)
  ids.map { |id| to_key(id) }
end