-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparam_list.rb
91 lines (72 loc) · 1.71 KB
/
param_list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'stringio'
module Aws
module Query
class ParamList
include Enumerable
# @api private
def initialize
@params = {}
end
# @param [String] param_name
# @param [String, nil] param_value
# @return [Param]
def set(param_name, param_value = nil)
param = Param.new(param_name, param_value)
@params[param.name] = param
param
end
alias []= set
# @return [Param, nil]
def [](param_name)
@params[param_name.to_s]
end
# @param [String] param_name
# @return [Param, nil]
def delete(param_name)
@params.delete(param_name)
end
# @return [Enumerable]
def each(&block)
to_a.each(&block)
end
# @return [Boolean]
def empty?
@params.empty?
end
# @return [Array<Param>] Returns an array of sorted {Param} objects.
def to_a
@params.values.sort
end
# @return [String]
def to_s
to_a.map(&:to_s).join('&')
end
# @return [#read, #rewind, #size]
def to_io
IoWrapper.new(self)
end
# @api private
class IoWrapper
# @param [ParamList] param_list
def initialize(param_list)
@param_list = param_list
@io = StringIO.new(param_list.to_s)
end
# @return [ParamList]
attr_reader :param_list
# @return [Integer]
def size
@io.size
end
# @return [void]
def rewind
@io.rewind
end
# @return [String, nil]
def read(bytes = nil, output_buffer = nil)
@io.read(bytes, output_buffer)
end
end
end
end
end