Class: Hyde::Util::Query

Inherits:
Object
  • Object
show all
Includes:
ParserSorting
Defined in:
lib/hyde/util/query.rb

Overview

Query string parser

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Query

Returns a new instance of Query.

Parameters:

  • query (String)


12
13
14
# File 'lib/hyde/util/query.rb', line 12

def initialize(query)
  @query = query
end

Instance Method Details

#[](key) ⇒ String, Array

Get key from query.

Parameters:

  • key (String)

Returns:

  • (String, Array)


39
40
41
# File 'lib/hyde/util/query.rb', line 39

def [](key)
  (@cache ||= parse)[key]
end

#parseHash

Better(tm) query parser. Returns a hash with arrays. Key semantics:

  • key=value creates a key value pair
  • key[]=value appends value to an array named key
  • key[index]=value sets value at index of array named key

Returns:

  • (Hash)


32
33
34
# File 'lib/hyde/util/query.rb', line 32

def parse
  construct_deep_hash(URI.decode_www_form(@query))
end

#parse_shallowHash

Shallow query parser (does not do PHP-like array keys)

Returns:

  • (Hash)


18
19
20
21
22
# File 'lib/hyde/util/query.rb', line 18

def parse_shallow
  URI.decode_www_form(@query)
     .sort_by { |array| array[0] }
     .to_h
end