Class: Code::Object::Dictionary
Constant Summary
Constants inherited
from Code::Object
NUMBER_CLASSES
Instance Attribute Summary
#methods, #raw
Instance Method Summary
collapse
-
#call(**args) ⇒ Object
-
#code_any?(argument, **globals) ⇒ Boolean
-
#code_clear ⇒ Object
-
#code_compact ⇒ Object
-
#code_compact! ⇒ Object
-
#code_compare(other) ⇒ Object
-
#code_deep_duplicate ⇒ Object
-
#code_delete(*arguments, index: 0, **globals) ⇒ Object
-
#code_delete_if(argument, **globals) ⇒ Object
-
#code_delete_unless(argument, **globals) ⇒ Object
-
#code_dig(*arguments) ⇒ Object
-
#code_each(argument, **globals) ⇒ Object
-
#code_empty? ⇒ Boolean
-
#code_except(*arguments) ⇒ Object
-
#code_fetch(*arguments, index: 0, **globals) ⇒ Object
-
#code_fetch_values(*arguments) ⇒ Object
-
#code_flatten(level = nil) ⇒ Object
-
#code_get(key) ⇒ Object
-
#code_has_key?(key) ⇒ Boolean
-
#code_has_value?(key) ⇒ Boolean
-
#code_inferior(other) ⇒ Object
-
#code_inferior_or_equal(other) ⇒ Object
-
#code_invert ⇒ Object
-
#code_keep_if(argument, **globals) ⇒ Object
-
#code_keep_unless(argument, **globals) ⇒ Object
-
#code_key(value, function = nil, **globals) ⇒ Object
-
#code_keys ⇒ Object
-
#code_merge(*arguments, **globals) ⇒ Object
-
#code_merge!(*arguments, **globals) ⇒ Object
-
#code_select(argument, **globals) ⇒ Object
-
#code_select!(argument, **globals) ⇒ Object
-
#code_set(key, value) ⇒ Object
-
#code_size ⇒ Object
-
#code_superior(other) ⇒ Object
-
#code_superior_or_equal(other) ⇒ Object
-
#code_to_context ⇒ Object
-
#code_to_query(namespace = nil) ⇒ Object
-
#code_transform_values(function, **globals) ⇒ Object
-
#code_values ⇒ Object
-
#initialize(*args, **kargs, &_block) ⇒ Dictionary
constructor
A new instance of Dictionary.
code_new, #code_new, maybe, #name, repeat, |
#<=>, #==, #as_json, #code_and, #code_as_json, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, code_get, #code_inclusive_range, #code_inspect, #code_methods, #code_name, #code_nothing?, #code_or, #code_self, code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?
Constructor Details
#initialize(*args, **kargs, &_block) ⇒ Dictionary
Returns a new instance of Dictionary.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/code/object/dictionary.rb', line 19
def initialize(*args, **kargs, &_block)
self.raw =
args
.map do |arg|
if arg.is_an?(::Hash)
arg.transform_keys(&:to_code).transform_values(&:to_code)
elsif arg.is_a?(Dictionary)
arg.raw.transform_keys(&:to_code).transform_values(&:to_code)
elsif arg.is_a?(Node::FunctionParameter)
arg.to_h.transform_keys(&:to_code).transform_values(&:to_code)
else
{}
end
end
.reduce({}, &:merge)
.merge(kargs.transform_keys(&:to_code).transform_values(&:to_code))
end
|
Instance Method Details
#call(**args) ⇒ Object
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/code/object/dictionary.rb', line 37
def call(**args)
code_operator = args.fetch(:operator, nil).to_code
code_arguments = args.fetch(:arguments, List.new).to_code
globals = multi_fetch(args, *GLOBALS)
code_value = code_arguments.code_first
case code_operator.to_s
when "<", "inferior"
sig(args) { Dictionary }
code_inferior(code_value)
when "<=", "inferior_or_equal"
sig(args) { Dictionary }
code_inferior_or_equal(code_value)
when "<=>", "compare"
sig(args) { Dictionary }
code_compare(code_value)
when ">", "superior"
sig(args) { Dictionary }
code_superior(code_value)
when ">=", "superior_or_equal"
sig(args) { Dictionary }
code_superior_or_equal(code_value)
when "[]", "at", "get"
sig(args) { Object }
code_get(code_value)
when "any?"
sig(args) { (Function | Class).maybe }
code_any?(code_value, **globals)
when "clear"
sig(args)
code_clear
when "compact!"
sig(args)
code_compact!
when "compact"
sig(args)
code_compact
when "delete"
sig(args) { Object.repeat(1) }
code_delete(*code_arguments.raw, **globals)
when "delete_if"
sig(args) { Function | Class }
code_delete_if(code_value, **globals)
when "delete_unless"
sig(args) { Function | Class }
code_delete_unless(code_value, **globals)
when "dig"
sig(args) { Object.repeat(1) }
code_dig(*code_arguments.raw)
when "each"
sig(args) { Function }
code_each(code_value, **globals)
when "eight?"
sig(args)
code_eight?
when "empty?"
sig(args)
code_empty?
when "except"
sig(args) { Object.repeat(1) }
code_except(*code_arguments.raw)
when "fetch"
sig(args) { Object.repeat(1) }
code_fetch(*code_arguments.raw, **globals)
when "fetch_values"
sig(args) { Object.repeat(1) }
code_fetch_values(*code_arguments.raw)
when "five?"
sig(args)
code_five?
when "flatten"
sig(args) { Integer.maybe }
code_flatten(code_value)
when "four?"
sig(args)
code_four?
when "has_key?"
sig(args) { Object }
code_has_key?(code_value)
when "has_value?"
sig(args) { Object }
code_has_value?(code_value)
when "invert"
sig(args)
code_invert
when "keep_if"
sig(args) { Function | Class }
code_keep_if(code_value, **globals)
when "keep_unless"
sig(args) { Function | Class }
code_keep_unless(code_value, **globals)
when "key"
sig(args) { [Object, Function.maybe] }
code_key(*code_arguments.raw, **globals)
when "keys"
sig(args)
code_keys
when "merge"
sig(args) { [Dictionary.repeat, Function.maybe] }
code_merge(*code_arguments.raw, **globals)
when "merge!"
sig(args) { [Dictionary.repeat, Function.maybe] }
code_merge!(*code_arguments.raw, **globals)
when "nine?"
sig(args)
code_nine?
when "one?"
sig(args)
code_one?
when "select!", "filter!"
sig(args) { Function | Class }
code_select!(code_value, **globals)
when "select", "filter"
sig(args) { Function | Class }
code_select(code_value, **globals)
when "seven?"
sig(args)
code_seven?
when "six?"
sig(args)
code_six?
when "size"
sig(args)
code_size
when "ten?"
sig(args)
code_ten?
when "three?"
sig(args)
code_three?
when "transform_values"
sig(args) { Function }
code_transform_values(code_value, **globals)
when "to_query"
sig(args) { String.maybe }
code_to_query(code_value)
when "two?"
sig(args)
code_two?
when "values"
sig(args)
code_values
when "zero?"
sig(args)
code_zero?
when "many?"
sig(args)
code_many?
when ->(code_operator) { code_has_key?(code_operator).truthy? }
result = code_fetch(code_operator)
if result.is_a?(Function)
result.call(**args, operator: nil)
else
sig(args)
result
end
else
super
end
end
|
#code_any?(argument, **globals) ⇒ Boolean
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/code/object/dictionary.rb', line 199
def code_any?(argument, **globals)
code_argument = argument.to_code
if code_argument.nothing?
Boolean.new(raw.any?)
elsif code_argument.is_a?(Class)
Boolean.new(raw.any? { |_, value| value.is_a?(code_argument.raw) })
else
index = 0
Boolean.new(
raw.any? do |key, value|
code_argument
.call(
arguments: List.new([key, value, Integer.new(index), self]),
**globals
)
.truthy?
.tap { index += 1 }
end
)
end
end
|
#code_clear ⇒ Object
223
224
225
226
|
# File 'lib/code/object/dictionary.rb', line 223
def code_clear
self.raw = {}
self
end
|
#code_compact ⇒ Object
228
229
230
|
# File 'lib/code/object/dictionary.rb', line 228
def code_compact
Dictionary.new(raw.dup.delete_if { |_, value| value.falsy? })
end
|
#code_compact! ⇒ Object
232
233
234
235
|
# File 'lib/code/object/dictionary.rb', line 232
def code_compact!
raw.delete_if { |_, value| value.falsy? }
self
end
|
#code_compare(other) ⇒ Object
237
238
239
240
|
# File 'lib/code/object/dictionary.rb', line 237
def code_compare(other)
code_other = other.to_code
Integer.new(raw <=> code_other.raw)
end
|
#code_deep_duplicate ⇒ Object
685
686
687
688
689
|
# File 'lib/code/object/dictionary.rb', line 685
def code_deep_duplicate
Dictionary.new(
raw.dup.to_h { |key, value| [key, value].map(&:code_deep_duplicate) }
)
end
|
#code_delete(*arguments, index: 0, **globals) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/code/object/dictionary.rb', line 242
def code_delete(*arguments, index: 0, **globals)
arguments = arguments.to_code.raw
code_index = index.to_code
code_default =
(
arguments.last if arguments.last.is_a?(Function) && arguments.many?
).to_code
arguments = arguments[...-1] unless code_default.nothing?
code_first = arguments.first.to_code
if arguments.one?
raw.delete(code_first) do
if code_default.nothing?
Nothing.new
else
code_default.call(
arguments: List.new([code_first, code_index, self]),
**globals
)
end
end
else
Dictionary.new(
arguments
.map
.with_index do |code_argument, index|
if code_default.nothing?
[
code_argument,
code_delete(code_argument, index: index, **globals)
]
else
[
code_argument,
code_delete(
code_argument,
code_default,
index: index,
**globals
)
]
end
end
.to_h
)
end
end
|
#code_delete_if(argument, **globals) ⇒ Object
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/code/object/dictionary.rb', line 292
def code_delete_if(argument, **globals)
code_argument = argument.to_code
if code_argument.is_a?(Class)
raw.delete_if { |_, value| value.is_a?(code_argument.raw) }
else
raw.delete_if.with_index do |(code_key, code_value), index|
argument.call(
arguments:
List.new([code_key, code_value, Integer.new(index), self]),
**globals
).truthy?
end
end
self
end
|
#code_delete_unless(argument, **globals) ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/code/object/dictionary.rb', line 310
def code_delete_unless(argument, **globals)
code_argument = argument.to_code
if code_argument.is_a?(Class)
raw.delete_if { |_, value| !value.is_a?(code_argument.raw) }
else
raw.delete_if.with_index do |(key, value), index|
code_argument.call(
arguments: List.new([key, value, Integer.new(index), self]),
**globals
).falsy?
end
end
self
end
|
#code_dig(*arguments) ⇒ Object
327
328
329
330
331
332
333
334
335
336
337
338
339
|
# File 'lib/code/object/dictionary.rb', line 327
def code_dig(*arguments)
code_arguments = arguments.to_code
code_arguments
.raw
.reduce(self) do |code_acc, code_element|
if code_acc.is_a?(Dictionary) || code_acc.is_a?(List)
code_acc.code_get(code_element)
else
Nothing.new
end
end
end
|
#code_each(argument, **globals) ⇒ Object
341
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/code/object/dictionary.rb', line 341
def code_each(argument, **globals)
code_argument = argument.to_code
raw.each.with_index do |(key, value), index|
code_argument.call(
arguments: List.new([key, value, Integer.new(index), self]),
**globals
)
end
self
end
|
#code_empty? ⇒ Boolean
354
355
356
|
# File 'lib/code/object/dictionary.rb', line 354
def code_empty?
Boolean.new(raw.empty?)
end
|
#code_except(*arguments) ⇒ Object
358
359
360
361
|
# File 'lib/code/object/dictionary.rb', line 358
def code_except(*arguments)
code_arguments = arguments.to_code
Dictionary.new(raw.except(*code_arguments.raw))
end
|
#code_fetch(*arguments, index: 0, **globals) ⇒ Object
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
# File 'lib/code/object/dictionary.rb', line 363
def code_fetch(*arguments, index: 0, **globals)
code_index = index.to_code
arguments = arguments.to_code.raw
code_default =
(
arguments.last if arguments.last.is_a?(Function) && arguments.many?
).to_code
arguments = arguments[..-2] unless code_default.nothing?
code_first = arguments.first.to_code
if arguments.one?
raw.fetch(code_first) do
if code_default.nothing?
Nothing.new
else
code_default.call(
arguments: List.new([code_first, code_index, self]),
**globals
)
end
end
else
Dictionary.new(
arguments
.map
.with_index do |code_argument, index|
if code_default.nothing?
[
code_argument,
code_fetch(code_argument, index: index, **globals)
]
else
[
code_argument,
code_fetch(
code_argument,
code_default,
index: index,
**globals
)
]
end
end
.to_h
)
end
end
|
#code_fetch_values(*arguments) ⇒ Object
413
414
415
416
417
|
# File 'lib/code/object/dictionary.rb', line 413
def code_fetch_values(*arguments)
code_arguments = arguments.to_code
List.new(raw.fetch_values(*code_arguments.raw))
end
|
#code_flatten(level = nil) ⇒ Object
419
420
421
422
423
|
# File 'lib/code/object/dictionary.rb', line 419
def code_flatten(level = nil)
code_level = level.to_code
code_level = Integer.new(-1) if code_level.nothing?
code_to_list.code_flatten(code_level)
end
|
#code_get(key) ⇒ Object
425
426
427
428
|
# File 'lib/code/object/dictionary.rb', line 425
def code_get(key)
code_key = key.to_code
raw[code_key] || Nothing.new
end
|
#code_has_key?(key) ⇒ Boolean
430
431
432
433
|
# File 'lib/code/object/dictionary.rb', line 430
def code_has_key?(key)
code_key = key.to_code
Boolean.new(raw.key?(code_key))
end
|
#code_has_value?(key) ⇒ Boolean
435
436
437
438
|
# File 'lib/code/object/dictionary.rb', line 435
def code_has_value?(key)
code_key = key.to_code
Boolean.new(raw.value?(code_key))
end
|
#code_inferior(other) ⇒ Object
440
441
442
443
|
# File 'lib/code/object/dictionary.rb', line 440
def code_inferior(other)
code_other = other.to_code
Boolean.new(raw < code_other.raw)
end
|
#code_inferior_or_equal(other) ⇒ Object
445
446
447
448
|
# File 'lib/code/object/dictionary.rb', line 445
def code_inferior_or_equal(other)
code_other = other.to_code
Boolean.new(raw <= code_other.raw)
end
|
#code_invert ⇒ Object
450
451
452
|
# File 'lib/code/object/dictionary.rb', line 450
def code_invert
Dictionary.new(raw.invert)
end
|
#code_keep_if(argument, **globals) ⇒ Object
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
# File 'lib/code/object/dictionary.rb', line 454
def code_keep_if(argument, **globals)
code_argument = argument.to_code
if code_argument.is_a?(Class)
raw.keep_if { |_, value| value.is_a?(code_argument.raw) }
else
raw.keep_if.with_index do |(key, value), index|
code_argument.call(
arguments: List.new([key, value, Integer.new(index), self]),
**globals
).truthy?
end
end
self
end
|
#code_keep_unless(argument, **globals) ⇒ Object
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
# File 'lib/code/object/dictionary.rb', line 471
def code_keep_unless(argument, **globals)
code_argument = argument.to_code
if code_argument.is_a?(Class)
raw.keep_if { |_, value| !value.is_a?(code_argument.raw) }
else
raw.keep_if.with_index do |(key, value), index|
code_argument.call(
arguments: List.new([key, value, Integer.new(index), self]),
**globals
).falsy?
rescue Error::Next => e
e.code_value.falsy?
end
end
self
end
|
#code_key(value, function = nil, **globals) ⇒ Object
490
491
492
493
494
495
496
497
498
499
500
501
502
|
# File 'lib/code/object/dictionary.rb', line 490
def code_key(value, function = nil, **globals)
code_value = value.to_code
code_function = function.to_code
if code_function.nothing?
raw.key(code_value) || Nothing.new
else
raw.key(code_value) ||
function.call(arguments: List.new([code_value, self]), **globals)
end
rescue Error::Next => e
e.code_value
end
|
#code_keys ⇒ Object
504
505
506
|
# File 'lib/code/object/dictionary.rb', line 504
def code_keys
List.new(raw.keys)
end
|
#code_merge(*arguments, **globals) ⇒ Object
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
# File 'lib/code/object/dictionary.rb', line 508
def code_merge(*arguments, **globals)
arguments = arguments.to_code.raw
code_conflict =
(
arguments.last if arguments.last.is_a?(Function) && arguments.many?
).to_code
arguments = arguments[..-2] unless code_conflict.nothing?
index = 0
Dictionary.new(
raw.merge(*arguments.map(&:raw)) do |key, old_value, new_value|
if code_conflict.nothing?
new_value.to_code.tap { index += 1 }
else
code_conflict
.call(
arguments:
List.new(
[
key.to_code,
old_value.to_code,
new_value.to_code,
index.to_code,
self
]
),
**globals
)
.tap { index += 1 }
end
rescue Error::Next => e
e.code_value.tap { index += 1 }
end
)
end
|
#code_merge!(*arguments, **globals) ⇒ Object
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
# File 'lib/code/object/dictionary.rb', line 547
def code_merge!(*arguments, **globals)
arguments = arguments.to_code.raw
code_conflict =
(
arguments.last if arguments.last.is_a?(Function) && arguments.many?
).to_code
arguments = arguments[..-2] unless code_conflict.nothing?
index = 0
raw.merge!(*arguments.map(&:raw)) do |key, old_value, new_value|
if code_conflict.nothing?
new_value.to_code.tap { index += 1 }
else
code_conflict
.call(
arguments:
List.new(
[
key.to_code,
old_value.to_code,
new_value.to_code,
index.to_code,
self
]
),
**globals
)
.tap { index += 1 }
end
rescue Error::Next => e
e.code_value.tap { index += 1 }
end
self
end
|
#code_select(argument, **globals) ⇒ Object
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
# File 'lib/code/object/dictionary.rb', line 606
def code_select(argument, **globals)
code_argument = argument.to_code
if code_argument.is_a?(Class)
Dictionary.new(
raw.select { |_, value| value.is_a?(code_argument.raw) }
)
else
Dictionary.new(
raw.select.with_index do |(key, value), index|
argument.call(
arguments:
List.new([key.to_code, value.to_code, index.to_code, self]),
**globals
).truthy?
rescue Error::Next => e
e.code_value.truthy?
end
)
end
end
|
#code_select!(argument, **globals) ⇒ Object
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
# File 'lib/code/object/dictionary.rb', line 586
def code_select!(argument, **globals)
code_argument = argument.to_code
if code_argument.is_a?(Class)
raw.select! { |_, value| value.is_a?(code_argument.raw) }
else
raw.select!.with_index do |(key, value), index|
argument.call(
arguments:
List.new([key.to_code, value.to_code, index.to_code, self]),
**globals
).truthy?
rescue Error::Next => e
e.code_value.truthy?
end
end
self
end
|
#code_set(key, value) ⇒ Object
628
629
630
631
632
633
|
# File 'lib/code/object/dictionary.rb', line 628
def code_set(key, value)
code_key = key.to_code
code_value = value.to_code
raw[code_key] = code_value
code_value
end
|
#code_size ⇒ Object
635
636
637
|
# File 'lib/code/object/dictionary.rb', line 635
def code_size
Integer.new(raw.size)
end
|
#code_superior(other) ⇒ Object
639
640
641
642
|
# File 'lib/code/object/dictionary.rb', line 639
def code_superior(other)
code_other = other.to_code
Boolean.new(raw > code_other.raw)
end
|
#code_superior_or_equal(other) ⇒ Object
644
645
646
647
|
# File 'lib/code/object/dictionary.rb', line 644
def code_superior_or_equal(other)
code_other = other.to_code
Boolean.new(raw >= code_other.raw)
end
|
#code_to_context ⇒ Object
649
650
651
|
# File 'lib/code/object/dictionary.rb', line 649
def code_to_context
Context.new(raw)
end
|
#code_to_query(namespace = nil) ⇒ Object
653
654
655
656
657
|
# File 'lib/code/object/dictionary.rb', line 653
def code_to_query(namespace = nil)
code_namespace = namespace.to_code
String.new(raw.to_query(code_namespace.raw))
end
|
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
# File 'lib/code/object/dictionary.rb', line 659
def code_transform_values(function, **globals)
code_function = function.to_code
Dictionary.new(
raw
.map
.with_index do |(key, value), index|
[
key.to_code,
code_function.call(
arguments:
List.new([key.to_code, value.to_code, index.to_code, self]),
**globals
)
]
rescue Error::Next => e
[key.to_code, e.code_value]
end
.to_h
)
end
|
#code_values ⇒ Object
681
682
683
|
# File 'lib/code/object/dictionary.rb', line 681
def code_values
List.new(raw.values)
end
|