code-ruby 1.1.3 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +13 -13
- data/.github/workflows/ci.yml +25 -24
- data/.npm-version +1 -0
- data/.rubocop.yml +14 -11
- data/Gemfile +5 -4
- data/Gemfile.lock +134 -28
- data/VERSION +1 -1
- data/bin/console +6 -0
- data/bin/dorian +31 -0
- data/code-ruby.gemspec +6 -1
- data/lib/code/error.rb +4 -25
- data/lib/code/node/code.rb +1 -1
- data/lib/code/node/function_parameter.rb +10 -8
- data/lib/code/node/while.rb +1 -1
- data/lib/code/object/boolean.rb +20 -16
- data/lib/code/object/class.rb +10 -4
- data/lib/code/object/code.rb +7 -3
- data/lib/code/object/context.rb +8 -8
- data/lib/code/object/date.rb +41 -7
- data/lib/code/object/decimal.rb +101 -56
- data/lib/code/object/dictionary.rb +245 -191
- data/lib/code/object/duration.rb +11 -7
- data/lib/code/object/function.rb +38 -25
- data/lib/code/object/global.rb +95 -42
- data/lib/code/object/html.rb +12 -14
- data/lib/code/object/http.rb +220 -0
- data/lib/code/object/identifier_list.rb +16 -16
- data/lib/code/object/integer.rb +129 -89
- data/lib/code/object/json.rb +18 -22
- data/lib/code/object/list.rb +154 -92
- data/lib/code/object/parameter.rb +9 -13
- data/lib/code/object/range.rb +77 -45
- data/lib/code/object/string.rb +15 -34
- data/lib/code/object/time.rb +17 -16
- data/lib/code/object.rb +126 -93
- data/lib/code/parser/string.rb +2 -1
- data/lib/code/type/sig.rb +3 -3
- data/lib/code-ruby.rb +119 -0
- data/package-lock.json +1 -1
- data/package.json +1 -1
- data/spec/code/object/http_spec.rb +91 -0
- data/spec/code/type_spec.rb +1 -1
- data/spec/code_spec.rb +10 -5
- data/spec/spec_helper.rb +18 -0
- metadata +51 -7
data/lib/code/object/list.rb
CHANGED
@@ -4,81 +4,89 @@ class Code
|
|
4
4
|
class Object
|
5
5
|
class List < Object
|
6
6
|
def initialize(*args, **_kargs, &)
|
7
|
-
raw =
|
8
|
-
|
9
|
-
|
7
|
+
@raw =
|
8
|
+
if args.first.is_a?(List)
|
9
|
+
args.first.raw.map(&:to_code)
|
10
|
+
elsif args.first.is_an?(::Array)
|
11
|
+
args.first.map(&:to_code)
|
12
|
+
else
|
13
|
+
[]
|
14
|
+
end
|
10
15
|
end
|
11
16
|
|
12
17
|
def call(**args)
|
13
|
-
|
14
|
-
|
18
|
+
code_operator = args.fetch(:operator, nil).to_code
|
19
|
+
code_arguments = args.fetch(:arguments, []).to_code
|
15
20
|
globals = multi_fetch(args, *GLOBALS)
|
16
|
-
|
21
|
+
code_value = code_arguments.code_first
|
17
22
|
|
18
|
-
case
|
23
|
+
case code_operator.to_s
|
19
24
|
when "join"
|
20
25
|
sig(args) { String.maybe }
|
21
|
-
code_join(
|
26
|
+
code_join(code_value)
|
22
27
|
when "sort"
|
23
28
|
sig(args)
|
24
29
|
code_sort
|
25
30
|
when "<<", "append"
|
26
31
|
sig(args) { Object }
|
27
|
-
code_append(
|
32
|
+
code_append(code_value)
|
28
33
|
when "any?"
|
29
34
|
sig(args) { Function.maybe }
|
30
|
-
code_any?(
|
35
|
+
code_any?(code_value, **globals)
|
31
36
|
when "detect"
|
32
37
|
sig(args) { Function }
|
33
|
-
code_detect(
|
38
|
+
code_detect(code_value, **globals)
|
34
39
|
when "each"
|
35
40
|
sig(args) { Function }
|
36
|
-
code_each(
|
41
|
+
code_each(code_value, **globals)
|
37
42
|
when "first"
|
38
|
-
sig(args)
|
39
|
-
code_first
|
43
|
+
sig(args) { Integer.maybe }
|
44
|
+
code_first(code_value)
|
45
|
+
when "sample"
|
46
|
+
sig(args) { Integer.maybe }
|
47
|
+
code_sample(code_value)
|
40
48
|
when "flatten"
|
41
49
|
sig(args) { Integer.maybe }
|
42
50
|
code_flatten
|
43
51
|
when "include?"
|
44
52
|
sig(args) { Object }
|
45
|
-
code_include?(
|
53
|
+
code_include?(code_value)
|
46
54
|
when "last"
|
47
55
|
sig(args)
|
48
56
|
code_last
|
49
57
|
when "map"
|
50
58
|
sig(args) { Function }
|
51
|
-
code_map(
|
59
|
+
code_map(code_value, **globals)
|
52
60
|
when "map!"
|
53
61
|
sig(args) { Function }
|
54
|
-
code_map!(
|
62
|
+
code_map!(code_value, **globals)
|
55
63
|
when "max"
|
56
64
|
sig(args)
|
57
65
|
code_max
|
58
66
|
when "max_by"
|
59
67
|
sig(args) { Function }
|
60
|
-
code_max_by(
|
68
|
+
code_max_by(code_value, **globals)
|
61
69
|
when "none?"
|
62
70
|
sig(args) { Function.maybe }
|
63
|
-
code_none?(
|
71
|
+
code_none?(code_value, **globals)
|
64
72
|
when "reduce"
|
65
73
|
sig(args) { Function }
|
66
|
-
code_reduce(
|
74
|
+
code_reduce(code_value, **globals)
|
67
75
|
when "reverse"
|
68
76
|
sig(args)
|
69
77
|
code_reverse
|
70
78
|
when "select"
|
71
79
|
sig(args) { Function }
|
72
|
-
code_select(
|
80
|
+
code_select(code_value, **globals)
|
73
81
|
when "select!"
|
74
82
|
sig(args) { Function }
|
75
|
-
code_select!(
|
83
|
+
code_select!(code_value, **globals)
|
76
84
|
when "reject"
|
77
85
|
sig(args) { Function }
|
78
|
-
code_reject(
|
86
|
+
code_reject(code_value, **globals)
|
79
87
|
when "reject!"
|
80
88
|
sig(args) { Function }
|
81
|
-
code_reject!(
|
89
|
+
code_reject!(code_value, **globals)
|
82
90
|
when "size"
|
83
91
|
sig(args)
|
84
92
|
code_size
|
@@ -94,21 +102,22 @@ class Code
|
|
94
102
|
end
|
95
103
|
|
96
104
|
def code_any?(argument = nil, **globals)
|
97
|
-
|
105
|
+
code_argument = argument.to_code
|
106
|
+
|
98
107
|
index = 0
|
99
108
|
|
100
109
|
Boolean.new(
|
101
|
-
raw.any? do |
|
102
|
-
if
|
103
|
-
|
110
|
+
raw.any? do |code_element|
|
111
|
+
if code_argument.nothing?
|
112
|
+
true
|
104
113
|
else
|
105
|
-
|
106
|
-
arguments: List.new([
|
114
|
+
code_argument.call(
|
115
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
107
116
|
**globals
|
108
117
|
).truthy?
|
109
118
|
end
|
110
119
|
rescue Error::Next => e
|
111
|
-
e.
|
120
|
+
e.code_value
|
112
121
|
ensure
|
113
122
|
index += 1
|
114
123
|
end
|
@@ -116,58 +125,85 @@ class Code
|
|
116
125
|
end
|
117
126
|
|
118
127
|
def code_append(other)
|
119
|
-
|
128
|
+
code_other = other.to_code
|
129
|
+
|
130
|
+
raw << code_other
|
131
|
+
|
120
132
|
self
|
121
133
|
end
|
122
134
|
|
123
135
|
def code_detect(argument, **globals)
|
124
|
-
|
125
|
-
|
126
|
-
|
136
|
+
code_argument = argument.to_code
|
137
|
+
|
138
|
+
raw.detect.with_index do |code_element, index|
|
139
|
+
code_argument.call(
|
140
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
127
141
|
**globals
|
128
142
|
).truthy?
|
129
143
|
rescue Error::Next => e
|
130
|
-
e.
|
144
|
+
e.code_value
|
131
145
|
end || Nothing.new
|
132
146
|
end
|
133
147
|
|
134
148
|
def code_each(argument, **globals)
|
135
|
-
|
136
|
-
|
137
|
-
|
149
|
+
code_argument = argument.to_code
|
150
|
+
|
151
|
+
raw.each.with_index do |code_element, index|
|
152
|
+
code_argument.call(
|
153
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
138
154
|
**globals
|
139
155
|
)
|
140
156
|
rescue Error::Next => e
|
141
|
-
e.
|
157
|
+
e.code_value
|
142
158
|
end
|
159
|
+
|
143
160
|
self
|
144
161
|
end
|
145
162
|
|
146
|
-
def code_first
|
147
|
-
|
163
|
+
def code_first(value = nil)
|
164
|
+
code_value = value.to_code
|
165
|
+
|
166
|
+
if code_value.nothing?
|
167
|
+
raw.first || Nothing.new
|
168
|
+
else
|
169
|
+
List.new(raw.first(code_value.raw))
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def code_sample(value = nil)
|
174
|
+
code_value = value.to_code
|
175
|
+
|
176
|
+
if code_value.nothing?
|
177
|
+
raw.sample || Nothing.new
|
178
|
+
else
|
179
|
+
List.new(raw.sample(code_value.raw))
|
180
|
+
end
|
148
181
|
end
|
149
182
|
|
150
183
|
def code_flatten(level = nil)
|
151
|
-
|
152
|
-
|
184
|
+
code_level = level.to_code
|
185
|
+
code_level = Integer.new(-1) if code_level.nothing?
|
186
|
+
level = code_level.raw
|
153
187
|
|
154
188
|
List.new(
|
155
|
-
raw.reduce([]) do |acc,
|
156
|
-
if
|
189
|
+
raw.reduce([]) do |acc, code_element|
|
190
|
+
if code_element.is_a?(List) && level != 0
|
157
191
|
if level.positive?
|
158
|
-
acc +
|
192
|
+
acc + code_element.code_flatten(level - 1).raw
|
159
193
|
else
|
160
|
-
acc +
|
194
|
+
acc + code_element.code_flatten(level).raw
|
161
195
|
end
|
162
196
|
else
|
163
|
-
acc + [
|
197
|
+
acc + [code_element]
|
164
198
|
end
|
165
199
|
end
|
166
200
|
)
|
167
201
|
end
|
168
202
|
|
169
203
|
def code_include?(other)
|
170
|
-
|
204
|
+
code_other = other.to_code
|
205
|
+
|
206
|
+
Boolean.new(raw.include?(code_other))
|
171
207
|
end
|
172
208
|
|
173
209
|
def code_last
|
@@ -175,26 +211,30 @@ class Code
|
|
175
211
|
end
|
176
212
|
|
177
213
|
def code_map(argument, **globals)
|
214
|
+
code_argument = argument.to_code
|
215
|
+
|
178
216
|
List.new(
|
179
|
-
raw.map.with_index do |
|
180
|
-
|
181
|
-
arguments: List.new([
|
217
|
+
raw.map.with_index do |code_element, index|
|
218
|
+
code_argument.call(
|
219
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
182
220
|
**globals
|
183
221
|
)
|
184
222
|
rescue Error::Next => e
|
185
|
-
e.
|
223
|
+
e.code_value
|
186
224
|
end
|
187
225
|
)
|
188
226
|
end
|
189
227
|
|
190
228
|
def code_map!(argument, **globals)
|
191
|
-
|
192
|
-
|
193
|
-
|
229
|
+
code_argument = argument.to_code
|
230
|
+
|
231
|
+
raw.map!.with_index do |code_element, index|
|
232
|
+
code_argument.call(
|
233
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
194
234
|
**globals
|
195
235
|
)
|
196
236
|
rescue Error::Next => e
|
197
|
-
e.
|
237
|
+
e.code_value
|
198
238
|
end
|
199
239
|
|
200
240
|
self
|
@@ -205,32 +245,35 @@ class Code
|
|
205
245
|
end
|
206
246
|
|
207
247
|
def code_max_by(argument, **globals)
|
208
|
-
|
209
|
-
|
210
|
-
|
248
|
+
code_argument = argument.to_code
|
249
|
+
|
250
|
+
raw.max_by.with_index do |code_element, index|
|
251
|
+
code_argument.call(
|
252
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
211
253
|
**globals
|
212
254
|
)
|
213
255
|
rescue Error::Next => e
|
214
|
-
e.
|
256
|
+
e.code_value
|
215
257
|
end || Nothing.new
|
216
258
|
end
|
217
259
|
|
218
260
|
def code_none?(argument = nil, **globals)
|
219
|
-
|
261
|
+
code_argument = argument.to_code
|
262
|
+
|
220
263
|
index = 0
|
221
264
|
|
222
265
|
Boolean.new(
|
223
|
-
raw.none? do |
|
224
|
-
if
|
225
|
-
|
266
|
+
raw.none? do |code_element|
|
267
|
+
if code_argument.nothing?
|
268
|
+
code_element.truthy?
|
226
269
|
else
|
227
|
-
|
228
|
-
arguments: List.new([
|
270
|
+
code_argument.call(
|
271
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
229
272
|
**globals
|
230
273
|
).truthy?
|
231
274
|
end
|
232
275
|
rescue Error::Next => e
|
233
|
-
|
276
|
+
e.code_value.truthy?
|
234
277
|
ensure
|
235
278
|
index += 1
|
236
279
|
end
|
@@ -238,13 +281,16 @@ class Code
|
|
238
281
|
end
|
239
282
|
|
240
283
|
def code_reduce(argument, **globals)
|
241
|
-
|
242
|
-
|
243
|
-
|
284
|
+
code_argument = argument.to_code
|
285
|
+
|
286
|
+
raw.reduce.with_index do |code_acc, code_element, index|
|
287
|
+
code_argument.call(
|
288
|
+
arguments:
|
289
|
+
List.new([code_acc, code_element, Integer.new(index), self]),
|
244
290
|
**globals
|
245
291
|
)
|
246
292
|
rescue Error::Next => e
|
247
|
-
e.
|
293
|
+
e.code_value
|
248
294
|
end || Nothing.new
|
249
295
|
end
|
250
296
|
|
@@ -253,61 +299,69 @@ class Code
|
|
253
299
|
end
|
254
300
|
|
255
301
|
def code_select(argument, **globals)
|
302
|
+
code_argument = argument.to_code
|
303
|
+
|
256
304
|
List.new(
|
257
|
-
raw.select.with_index do |
|
258
|
-
|
259
|
-
arguments: List.new([
|
305
|
+
raw.select.with_index do |code_element, index|
|
306
|
+
code_argument.call(
|
307
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
260
308
|
**globals
|
261
309
|
).truthy?
|
262
310
|
rescue Error::Next => e
|
263
|
-
|
311
|
+
e.code_value.truthy?
|
264
312
|
end
|
265
313
|
)
|
266
314
|
end
|
267
315
|
|
268
316
|
def code_select!(argument, **globals)
|
269
|
-
|
270
|
-
|
271
|
-
|
317
|
+
code_argument = argument.to_code
|
318
|
+
|
319
|
+
raw.select!.with_index do |code_element, index|
|
320
|
+
code_argument.call(
|
321
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
272
322
|
**globals
|
273
323
|
).truthy?
|
274
324
|
rescue Error::Next => e
|
275
|
-
|
325
|
+
e.code_value.truthy?
|
276
326
|
end
|
277
327
|
|
278
328
|
self
|
279
329
|
end
|
280
330
|
|
281
331
|
def code_reject(argument, **globals)
|
332
|
+
code_argument = argument.to_code
|
333
|
+
|
282
334
|
List.new(
|
283
|
-
raw.reject.with_index do |
|
284
|
-
|
285
|
-
arguments: List.new([
|
335
|
+
raw.reject.with_index do |code_element, index|
|
336
|
+
code_argument.call(
|
337
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
286
338
|
**globals
|
287
339
|
).truthy?
|
288
340
|
rescue Error::Next => e
|
289
|
-
|
341
|
+
e.code_value.truthy?
|
290
342
|
end
|
291
343
|
)
|
292
344
|
end
|
293
345
|
|
294
346
|
def code_reject!(argument, **globals)
|
295
|
-
|
296
|
-
|
297
|
-
|
347
|
+
code_argument = argument.to_code
|
348
|
+
|
349
|
+
raw.reject!.with_index do |code_element, index|
|
350
|
+
code_argument.call(
|
351
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
298
352
|
**globals
|
299
353
|
).truthy?
|
300
354
|
rescue Error::Next => e
|
301
|
-
|
355
|
+
e.code_value.truthy?
|
302
356
|
end
|
303
357
|
|
304
358
|
self
|
305
359
|
end
|
306
360
|
|
307
361
|
def code_join(separator = nil)
|
308
|
-
|
362
|
+
code_separator = separator.to_s.to_code
|
309
363
|
|
310
|
-
String.new(raw.join(
|
364
|
+
String.new(raw.join(code_separator.raw))
|
311
365
|
end
|
312
366
|
|
313
367
|
def code_sort
|
@@ -326,7 +380,15 @@ class Code
|
|
326
380
|
raw.inject(&:code_plus) || Nothing.new
|
327
381
|
end
|
328
382
|
|
329
|
-
|
383
|
+
def code_get(argument)
|
384
|
+
code_argument = argument.to_code
|
385
|
+
|
386
|
+
raw[code_argument] || Nothing.new
|
387
|
+
end
|
388
|
+
|
389
|
+
def any?
|
390
|
+
code_any?.truthy?
|
391
|
+
end
|
330
392
|
end
|
331
393
|
end
|
332
394
|
end
|
@@ -4,35 +4,35 @@ class Code
|
|
4
4
|
class Object
|
5
5
|
class Parameter < Object
|
6
6
|
def initialize(*args, **_kargs, &)
|
7
|
-
@raw = Dictionary.new(args.first
|
7
|
+
@raw = Dictionary.new(args.first)
|
8
8
|
end
|
9
9
|
|
10
10
|
def code_name
|
11
|
-
String.new(raw.code_get(
|
11
|
+
String.new(raw.code_get(:name))
|
12
12
|
end
|
13
13
|
|
14
14
|
def code_regular?
|
15
|
-
Boolean.new(raw.code_get(
|
15
|
+
Boolean.new(raw.code_get(:regular?))
|
16
16
|
end
|
17
17
|
|
18
18
|
def code_keyword?
|
19
|
-
Boolean.new(raw.code_get(
|
19
|
+
Boolean.new(raw.code_get(:keyword?))
|
20
20
|
end
|
21
21
|
|
22
22
|
def code_regular_splat?
|
23
|
-
Boolean.new(raw.code_get(
|
23
|
+
Boolean.new(raw.code_get(:regular_splat?))
|
24
24
|
end
|
25
25
|
|
26
26
|
def code_keyword_splat?
|
27
|
-
Boolean.new(raw.code_get(
|
27
|
+
Boolean.new(raw.code_get(:keyword_splat?))
|
28
28
|
end
|
29
29
|
|
30
30
|
def code_default
|
31
|
-
Code.new(raw.code_get(
|
31
|
+
Code.new(raw.code_get(:default))
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
|
34
|
+
def code_evaluate(...)
|
35
|
+
code_default.code_evaluate(...)
|
36
36
|
end
|
37
37
|
|
38
38
|
def regular?
|
@@ -50,10 +50,6 @@ class Code
|
|
50
50
|
def keyword_splat?
|
51
51
|
code_keyword_splat?.truthy?
|
52
52
|
end
|
53
|
-
|
54
|
-
def to_h
|
55
|
-
raw.raw
|
56
|
-
end
|
57
53
|
end
|
58
54
|
end
|
59
55
|
end
|