Module: Buckaruby::FieldMapper
- Defined in:
- lib/buckaruby/field_mapper.rb
Overview
Map NVP fields to hashes and multiple values (with index) to arrays.
Class Method Summary collapse
Class Method Details
.map_fields(params, prefix) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/buckaruby/field_mapper.rb', line 10 def map_fields(params, prefix) results = [] index = 1 loop do index_key = "#{prefix}_#{index}_" # brq_services_1_ index_length = index_key.split(/_[0-9]*_/).length # 1 # Get all fields starting with prefix and index. fields = params.select { |key| key.to_s.start_with?(index_key) } break if fields.empty? result = Support::CaseInsensitiveHash.new fields.each do |key, value| splitted_key = key.to_s.split(/_[0-9]*_/) # ["brq_services", "name"] key_length = splitted_key.length # 2 new_key = splitted_key[index_length] # name if key_length == index_length + 1 # Add normal fields to the result. result[new_key] = value else # Recursively map indexed fields. result[new_key] ||= begin new_prefix = [index_key, new_key].join map_fields(fields, new_prefix) end end end break if result.empty? results << result index += 1 end results end |