Sha256: 21f99cc9dea32a64d904a89d3f39c3dcef4dcd5d088ae6c51c03275697acbcff

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module Spec
  module Integration
    module Extensions

      module Hash # :nodoc:
        def name_with_prefix(prefix, name)
          prefix ? "#{prefix}[#{name}]" : name.to_s
        end

        def requestify(parameters, collector = [], prefix=nil)
          if Hash === parameters
            return collector if parameters.empty?
            parameters.each { |k,v| requestify(v, collector, name_with_prefix(prefix, k)) }
          elsif Array === parameters
            parameters.each { |v| requestify(v, collector, name_with_prefix(prefix, "")) }
          elsif prefix.nil?
            collector
          else
            collector << [prefix, parameters]
          end
          collector
        end
        
        # Extend Hash to give it the ability to be converted to Rails'esque
        # HTML form field names and values. This is used to verify forms.
        #
        # The return value is an Array of key value pairs. This is to maintain
        # the behaviour that a browser will submit enabled fields in the order
        # they are found in the document. Rails leverages that behaviour to
        # construct data structures from the request parameters. Specifically,
        # when you have an Array of Hashes, the Hash a parameter appears in
        # depends on something like this:
        #
        #   one[][one]=1&one[][two]=2&one[][three]=3one[][one]=4&one[][five]=5
        #   => [{'one' => 1, 'two' => 2, 'three' => 3}{'one' => 4, 'five' => 5}]
        #
        def to_fields
          requestify(self)
        end
      end

    end
  end
end

Hash.module_eval do
  include Spec::Integration::Extensions::Hash
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
radiant-0.7.2 vendor/plugins/spec_integration/lib/spec/integration/extensions/hash.rb
radiant-0.7.0 vendor/plugins/spec_integration/lib/spec/integration/extensions/hash.rb
radiant-0.7.1 vendor/plugins/spec_integration/lib/spec/integration/extensions/hash.rb