class Rokaki::FilterModel::LikeKeys

Converts deep hashes into keys effectively drops the leaf values and make's their keys the leaves

Attributes

args[R]
key_paths[R]
keys[R]

Public Class Methods

new(args) click to toggle source
# File lib/rokaki/filter_model/like_keys.rb, line 10
def initialize(args)
  @args = args
  @keys = []
  @key_paths = []
end

Public Instance Methods

call() click to toggle source
# File lib/rokaki/filter_model/like_keys.rb, line 18
def call
  args.keys.each do |key|
    map_keys(key: key, value: args[key])
  end
  keys
end

Private Instance Methods

deep_assign(keys, value) click to toggle source

Many thanks Cary Swoveland stackoverflow.com/questions/56634950/ruby-dig-set-assign-values-using-hashdig/56635124

# File lib/rokaki/filter_model/like_keys.rb, line 49
def deep_assign(keys, value)
  keys[0..-2].reverse_each.reduce ({ keys.last => value }) { |h,key| { key=>h } }
end
map_keys(key:, value:, key_path: []) click to toggle source
# File lib/rokaki/filter_model/like_keys.rb, line 27
def map_keys(key:, value:, key_path: [])

  if value.is_a?(Hash)
    key_path << key
    value.keys.each do |key|
      map_keys(key: key, value: value[key], key_path: key_path.dup)
    end
  end

  if value.is_a?(Symbol)
    keys << (key_path.empty? ? key : deep_assign(key_path, key))
    key_path << key
    key_paths << key_path
  end

  key_path

end