Class: XRB::Query::Delegate

Inherits:
Object
  • Object
show all
Defined in:
lib/xrb/query.rb

Overview

Receives query parser events and assembles them into nested hashes and arrays.

Instance Method Summary collapse

Constructor Details

#initialize(top = {}) ⇒ Delegate

Initialize a query delegate with the top-level result hash.



27
28
29
30
31
32
# File 'lib/xrb/query.rb', line 27

def initialize(top = {})
  @top = top
  
  @current = @top
  @index = nil
end

Instance Method Details

#appendObject

Descend into an array and select its next index.



68
69
70
71
72
73
74
75
76
# File 'lib/xrb/query.rb', line 68

def append
  if @index
    @current = @current.fetch(@index) do
      @current[@index] = []
    end
  end
  
  @index = @current.size
end

#assign(value, encoded) ⇒ Object

Assign a parsed value to the selected key.



82
83
84
85
86
87
88
89
90
91
# File 'lib/xrb/query.rb', line 82

def assign(value, encoded)
  if encoded
    value = ::URI.decode_www_form_component(value)
  end
  
  @current[@index] = value
  
  @current = @top
  @index = nil
end

#index(key) ⇒ Object

Descend into the previously selected key and select the next nested key.



56
57
58
59
60
61
62
63
64
# File 'lib/xrb/query.rb', line 56

def index(key)
  if @index
    @current = @current.fetch(@index) do
      @current[@index] = {}
    end
  end
  
  @index = key
end

#integer(key) ⇒ Object

Select an integer key parsed from an array or hash index.



49
50
51
# File 'lib/xrb/query.rb', line 49

def integer(key)
  index(key.to_i)
end

#pairObject

Assign true to a query key which has no explicit value.



95
96
97
98
99
100
101
102
# File 'lib/xrb/query.rb', line 95

def pair
  if @index
    @current[@index] = true
  end
  
  @current = @top
  @index = nil
end

#string(key, encoded) ⇒ Object

Select a symbolic key parsed from a query string.



38
39
40
41
42
43
44
# File 'lib/xrb/query.rb', line 38

def string(key, encoded)
  if encoded
    key = ::URI.decode_www_form_component(key)
  end
  
  index(key.to_sym)
end