Class: FMCache::Helper
- Inherits:
-
Object
- Object
- FMCache::Helper
- Defined in:
- lib/fmcache/helper.rb
Constant Summary collapse
- KEY_PREFIX =
"fmcache:"
Class Method Summary collapse
- .sort(values, ids) ⇒ <Hash>
- .to_fields(field_mask, prefix: []) ⇒ <String>
- .to_id(key) ⇒ Integer
- .to_ids(keys) ⇒ <Integer>
- .to_key(id) ⇒ String
- .to_keys(ids) ⇒ <String>
Class Method Details
.sort(values, ids) ⇒ <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>
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
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>
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
21 22 23 |
# File 'lib/fmcache/helper.rb', line 21 def to_key(id) "#{KEY_PREFIX}#{id}" end |
.to_keys(ids) ⇒ <String>
15 16 17 |
# File 'lib/fmcache/helper.rb', line 15 def to_keys(ids) ids.map { |id| to_key(id) } end |