Class: Rack::Utils::KeySpaceConstrainedParams

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(limit = Utils.key_space_limit) ⇒ KeySpaceConstrainedParams

Returns a new instance of KeySpaceConstrainedParams.



518
519
520
521
522
# File 'lib/rack/utils.rb', line 518

def initialize(limit = Utils.key_space_limit)
  @limit  = limit
  @size   = 0
  @params = {}
end

Instance Method Details

#[](key) ⇒ Object



524
525
526
# File 'lib/rack/utils.rb', line 524

def [](key)
  @params[key]
end

#[]=(key, value) ⇒ Object

Raises:

  • (RangeError)


528
529
530
531
532
# File 'lib/rack/utils.rb', line 528

def []=(key, value)
  @size += key.size if key && !@params.key?(key)
  raise RangeError, 'exceeded available parameter key space' if @size > @limit
  @params[key] = value
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


534
535
536
# File 'lib/rack/utils.rb', line 534

def key?(key)
  @params.key?(key)
end

#to_params_hashObject



538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/rack/utils.rb', line 538

def to_params_hash
  hash = @params
  hash.keys.each do |key|
    value = hash[key]
    if value.kind_of?(self.class)
      hash[key] = value.to_params_hash
    elsif value.kind_of?(Array)
      value.map! {|x| x.kind_of?(self.class) ? x.to_params_hash : x}
    end
  end
  hash
end