/
reference.rb
144 lines (115 loc) · 2.63 KB
/
reference.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class Reference
GROUPS = {
"generic" => "Keys",
"string" => "Strings",
"hash" => "Hashes",
"list" => "Lists",
"set" => "Sets",
"sorted_set" => "Sorted Sets",
"pubsub" => "Pub/Sub",
"transactions" => "Transactions",
"connection" => "Connection",
"server" => "Server",
}
class Command
class Argument
attr :argument
def initialize(argument)
@argument = argument
end
def type
[argument["type"]].flatten
end
def optional?
argument["optional"] || false
end
def multiple?
argument["multiple"] || false
end
def to_s
if argument["multiple"]
res = multiple(argument)
elsif argument["variadic"]
res = variadic(argument)
elsif argument["enum"]
res = enum(argument)
else
res = simple(argument)
end
argument["optional"] ? "[#{res}]" : res
end
private
def multiple(argument)
complex(argument) do |parts|
parts.collect! do |part|
part.unshift(argument["command"])
end
end
end
def variadic(argument)
complex(argument) do |parts|
parts.unshift(argument["command"])
end
end
def complex(argument)
parts = %w{1 2 N}.map do |suffix|
Array(argument["name"]).map do |arg|
"#{arg}#{suffix}"
end
end
yield(parts) if argument["command"]
parts.insert(-2, "...").flatten.join(" ")
end
def simple(argument)
[argument["command"], argument["name"]].compact.flatten.join(" ")
end
def enum(argument)
[argument["command"], argument["enum"].join("|")].compact.join(" ")
end
end
attr :name
attr :command
attr :group
def initialize(name, command)
@name = name
@command = command
end
def to_s
@to_s ||= [name, *arguments].join(" ")
end
def since
command["since"]
end
def group
command["group"]
end
def to_param
name.downcase.gsub(" ", "-")
end
def arguments
(command["arguments"] || []).map do |argument|
Argument.new(argument)
end
end
include Comparable
def ==(other)
name == other.name
end
alias eql? ==
def hash
name.hash
end
end
include Enumerable
def initialize(commands)
@commands = commands
end
def [](name)
Command.new(name, @commands[name]) if @commands[name]
end
def each
@commands.each do |name, attrs|
yield Command.new(name, attrs)
end
end
end