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.



544
545
546
547
548
# File 'lib/rack/utils.rb', line 544

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

Instance Method Details

#[](key) ⇒ Object



550
551
552
# File 'lib/rack/utils.rb', line 550

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

#[]=(key, value) ⇒ Object

Raises:

  • (RangeError)


554
555
556
557
558
# File 'lib/rack/utils.rb', line 554

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)


560
561
562
# File 'lib/rack/utils.rb', line 560

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

#to_params_hashObject



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/rack/utils.rb', line 564

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