Sha256: 5e28f3548439e2fb797a463a75fda35ecca112fc79da1cadeb544a47d43b2238

Contents?: true

Size: 1.07 KB

Versions: 12

Compression:

Stored size: 1.07 KB

Contents

module Sinatra
  class EventContext
    def params
      @params ||= ParamsParser.new(@route_params.merge(@request.params)).to_hash
    end
    
    private
    
    class ParamsParser
      attr_reader :hash
      
      def initialize(hash)
        @hash = nested(hash)
      end
      
      alias :to_hash :hash
      
      protected
      
        def nested(hash)
          hash.inject(indifferent_hash) do |par, (key,val)|
            if key =~ /([^\[]+)\[([^\]]+)\](.*)/ # a[b] || a[b][c] ($1 == a, $2 == b, $3 == [c])
              par[$1] ||= indifferent_hash
              par[$1].merge_recursive nested("#{$2}#{$3}" => val)
            else
              par[key] = val
            end
            par
          end
        end

        def indifferent_hash
          Hash.new {|h,k| h[k.to_s] if Symbol === k}
        end
    end
  end
end

class Hash
  def merge_recursive(other)
    update(other) do |key, old_value, new_value|
      if Hash === old_value && Hash === new_value
        old_value.merge_recursive(new_value)
      else
        new_value
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 4 rubygems

Version Path
bantic-integrity-0.1.4.1 vendor/sinatra-hacks/lib/hacks.rb
bantic-integrity-0.1.4.2 vendor/sinatra-hacks/lib/hacks.rb
bantic-integrity-0.1.4.3 vendor/sinatra-hacks/lib/hacks.rb
bantic-integrity-0.1.4.4 vendor/sinatra-hacks/lib/hacks.rb
bantic-integrity-0.1.4 vendor/sinatra-hacks/lib/hacks.rb
defunkt-integrity-0.1.1 vendor/sinatra-hacks/lib/hacks.rb
foca-integrity-0.1.0 vendor/sinatra-hacks/lib/hacks.rb
foca-integrity-0.1.1 vendor/sinatra-hacks/lib/hacks.rb
foca-integrity-0.1.2 vendor/sinatra-hacks/lib/hacks.rb
foca-integrity-0.1.3 vendor/sinatra-hacks/lib/hacks.rb
foca-integrity-0.1.4 vendor/sinatra-hacks/lib/hacks.rb
giraffesoft-integrity-0.1.4 vendor/sinatra-hacks/lib/hacks.rb