Sha256: c2369190448aa12a9cd806e4bdeff04658dd02afec28345e1ddef4d3b5c2e952

Contents?: true

Size: 948 Bytes

Versions: 2

Compression:

Stored size: 948 Bytes

Contents

module MapFields
  class ParamsParser
    def self.parse(params, field = nil)
      result = []
      params.each do |key, value|
        next if [:controller, :action].include?(key.to_sym)
        if field.nil? || field.to_s == key.to_s
          check_values(value) do |k,v|
            result << ["#{key.to_s}#{k}", v]
          end
        end
      end
      result
    end

    private
    def self.check_values(value, &block)
      result = []
      if value.kind_of?(Hash)
        value.each do |k,v|
          check_values(v) do |k2,v2|
            result << ["[#{k.to_s}]#{k2}", v2]
          end
        end
      elsif value.kind_of?(Array)
        value.each do |v|
          check_values(v) do |k2, v2|
            result << ["[]#{k2}", v2]
          end
        end
      else
        result << ["", value] unless value.respond_to?(:read)
      end
      result.each do |arr|
        yield arr[0], arr[1]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
map_fields-2.0.0.beta2 lib/map_fields/params_parser.rb
map_fields-2.0.0.beta lib/map_fields/params_parser.rb