Class: UrlEncodedPairParser

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/fake_web/url_encoded_pair_parser.rb

Overview

This class was copied/adapted from ActionController

Constant Summary collapse

KEY_REGEXP =
%r{([^\[\]=&]+)}
BRACKETED_KEY_REGEXP =
%r{\[([^\[\]=&]+)\]}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pairs = []) ⇒ UrlEncodedPairParser



8
9
10
11
12
# File 'lib/fake_web/url_encoded_pair_parser.rb', line 8

def initialize(pairs = [])
  super('')
  @result = {}
  pairs.each { |key, value| parse(key, value) }
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/fake_web/url_encoded_pair_parser.rb', line 6

def parent
  @parent
end

#resultObject (readonly)

Returns the value of attribute result.



6
7
8
# File 'lib/fake_web/url_encoded_pair_parser.rb', line 6

def result
  @result
end

#topObject (readonly)

Returns the value of attribute top.



6
7
8
# File 'lib/fake_web/url_encoded_pair_parser.rb', line 6

def top
  @top
end

Instance Method Details

#parse(key, value) ⇒ Object

Parse the query string



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fake_web/url_encoded_pair_parser.rb', line 18

def parse(key, value)
  self.string = key
  @top, @parent = result, nil

  # First scan the bare key
  key = scan(KEY_REGEXP) or return
  key = post_key_check(key)

  # Then scan as many nestings as present
  until eos?
    r = scan(BRACKETED_KEY_REGEXP) or return
    key = self[1]
    key = post_key_check(key)
  end

  bind(key, value)
end