class StackerBee::Middleware::DictionaryFlattener

Constants

LB
RB

Public Class Methods

detokenize(key) click to toggle source
# File lib/stacker_bee/middleware/dictionary_flattener.rb, line 13
def self.detokenize(key)
  key.gsub(LB, '[').gsub(RB, ']')
end
tokenize(key) click to toggle source

TODO: should the tokenizing be separate from the flattener? it could be a middleware that finds [] in the keys and replaces them

# File lib/stacker_bee/middleware/dictionary_flattener.rb, line 9
def self.tokenize(key)
  key.gsub('[', LB).gsub(']', RB)
end

Public Instance Methods

flatten_map_values(params, hashes) click to toggle source
# File lib/stacker_bee/middleware/dictionary_flattener.rb, line 26
def flatten_map_values(params, hashes)
  hashes.each do |hash_name, hash|
    remove_falseish(hash).each_with_index do |(key, value), index|
      hash_url_key = self.class.tokenize("#{hash_name}[#{index}]")

      params["#{hash_url_key}.key"]   = key
      params["#{hash_url_key}.name"]  = key
      params["#{hash_url_key}.value"] = value
    end
    params.delete hash_name
  end
end
flatten_params(params) click to toggle source
# File lib/stacker_bee/middleware/dictionary_flattener.rb, line 21
def flatten_params(params)
  hashes = params.select { |_, val| val.respond_to?(:keys) }
  flatten_map_values params, hashes
end
remove_falseish(hash) click to toggle source

TODO: isn't this done with the RemoveEmptyStrings middleware?

# File lib/stacker_bee/middleware/dictionary_flattener.rb, line 40
def remove_falseish(hash)
  hash.reject { |_, v| v == '' || v =~ /false/i || !v }
end
transform_params(original) click to toggle source
# File lib/stacker_bee/middleware/dictionary_flattener.rb, line 17
def transform_params(original)
  original.dup.tap { |params| flatten_params(params) }
end