Class: Code::Object::Dictionary

Inherits:
Code::Object show all
Defined in:
lib/code/object/dictionary.rb

Direct Known Subclasses

Context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, call, #code_and_operator, code_and_operator, #code_different, code_different, code_equal_equal, #code_equal_equal, #code_equal_equal_equal, code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_or_operator, #code_or_operator, code_self, #code_self, code_to_string, #code_to_string, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, repeat, sig, #sig, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(raw = {}) ⇒ Dictionary

Returns a new instance of Dictionary.



8
9
10
11
# File 'lib/code/object/dictionary.rb', line 8

def initialize(raw = {})
  raw = raw.raw if raw.is_a?(Dictionary)
  @raw = raw.to_h
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/code/object/dictionary.rb', line 6

def raw
  @raw
end

Class Method Details

.nameObject



13
14
15
# File 'lib/code/object/dictionary.rb', line 13

def self.name
  "Dictionary"
end

Instance Method Details

#as_jsonObject



666
667
668
# File 'lib/code/object/dictionary.rb', line 666

def as_json(...)
  raw.as_json(...)
end

#call(**args) ⇒ Object



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
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
# File 'lib/code/object/dictionary.rb', line 17

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  globals = multi_fetch(args, *GLOBALS)
  value = arguments.first&.value

  case operator.to_s
  when "<", "inferior"
    sig(args) { Dictionary }
    code_inferior(value)
  when "<=", "inferior_or_equal"
    sig(args) { Dictionary }
    code_inferior_or_equal(value)
  when "<=>", "compare"
    sig(args) { Dictionary }
    code_compare(value)
  when ">", "superior"
    sig(args) { Dictionary }
    code_superior(value)
  when ">=", "superior_or_equal"
    sig(args) { Dictionary }
    code_superior_or_equal(value)
  when "[]", "at", "get"
    sig(args) { Object }
    code_get(value)
  when "any?"
    sig(args) { Function | Class }
    code_any?(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(*arguments.map(&:value), **globals)
  when "delete_if"
    sig(args) { Function | Class }
    code_delete_if(value, **globals)
  when "delete_unless"
    sig(args) { Function | Class }
    code_delete_unless(value, **globals)
  when "dig"
    sig(args) { Object.repeat(1) }
    code_dig(*arguments.map(&:value))
  when "each"
    sig(args) { Function }
    code_each(value, **globals)
  when "eight?"
    sig(args)
    code_eight?
  when "empty?"
    sig(args)
    code_empty?
  when "except"
    sig(args) { Object.repeat(1) }
    code_except(*arguments.map(&:value))
  when "fetch"
    sig(args) { Object.repeat(1) }
    code_fetch(*arguments.map(&:value), **globals)
  when "fetch_values"
    sig(args) { Object.repeat(1) }
    code_fetch_values(*arguments.map(&:value))
  when "five?"
    sig(args)
    code_five?
  when "flatten"
    sig(args) { Integer.maybe }
    code_flatten(value)
  when "four?"
    sig(args)
    code_four?
  when "has_key?"
    sig(args) { Object }
    code_has_key?(value)
  when "has_value?"
    sig(args) { Object }
    code_has_value?(value)
  when "invert"
    sig(args)
    code_invert
  when "keep_if"
    sig(args) { Function | Class }
    code_keep_if(value, **globals)
  when "keep_unless"
    sig(args) { Function | Class }
    code_keep_unless(value, **globals)
  when "key"
    sig(args) { [Object, Function.maybe] }
    code_key(*arguments.map(&:value), **globals)
  when "keys"
    sig(args)
    code_keys
  when "merge"
    sig(args) { [Dictionary.repeat, Function.maybe] }
    code_merge(*arguments.map(&:value), **globals)
  when "nine?"
    sig(args)
    code_nine?
  when "one?"
    sig(args)
    code_one?
  when "select!", "filter!"
    sig(args) { Function | Class }
    code_select!(value, **globals)
  when "select", "filter"
    sig(args) { Function | Class }
    code_select(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 "to_list"
    sig(args)
    code_to_list
  when "transform_values"
    sig(args) { Function }
    code_transform_values(value, **globals)
  when "two?"
    sig(args)
    code_two?
  when "values"
    sig(args)
    code_values
  when "zero?"
    sig(args)
    code_zero?
  when ->(operator) { code_has_key?(String.new(operator)).truthy? }
    result = code_fetch(operator)

    if result.is_a?(Function)
      result.call(**args.merge(operator: nil))
    else
      sig(args)
      result
    end
  else
    super
  end
end

#code_any?(argument, **globals) ⇒ Boolean

Returns:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/code/object/dictionary.rb', line 173

def code_any?(argument, **globals)
  if argument.is_a?(Class)
    Boolean.new(raw.any? { |_, value| value.is_a?(argument.raw) })
  else
    index = 0

    Boolean.new(
      raw.any? do |key, value|
        argument
          .call(
            arguments: [
              Argument.new(key),
              Argument.new(value),
              Argument.new(self),
              Argument.new(Integer.new(index))
            ],
            **globals
          )
          .truthy?
          .tap { index += 1 }
      end
    )
  end
end

#code_clearObject



198
199
200
201
# File 'lib/code/object/dictionary.rb', line 198

def code_clear
  @raw = {}
  self
end

#code_compactObject



203
204
205
# File 'lib/code/object/dictionary.rb', line 203

def code_compact
  self.class.new(raw.dup.delete_if { |_, value| value.falsy? })
end

#code_compact!Object



207
208
209
210
# File 'lib/code/object/dictionary.rb', line 207

def code_compact!
  raw.delete_if { |_, value| value.falsy? }
  self
end

#code_compare(other) ⇒ Object



212
213
214
# File 'lib/code/object/dictionary.rb', line 212

def code_compare(other)
  Integer.new(raw <=> other.raw)
end

#code_delete(*arguments, index: Integer.new(0), **globals) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
# File 'lib/code/object/dictionary.rb', line 216

def code_delete(*arguments, index: Integer.new(0), **globals)
  default =
    (
      if arguments.last.is_a?(Function) && arguments.size > 1
        arguments.last
      end
    )

  arguments = arguments[..-2] if default

  if arguments.one?
    raw.delete(arguments.first) do
      if default
        default.call(
          arguments: [
            Argument.new(arguments.first),
            Argument.new(self),
            Argument.new(index)
          ],
          **globals
        )
      else
        raise(
          Code::Error::KeyNotFound,
          "#{arguments.first.inspect} not found on #{inspect}"
        )
      end
    end
  else
    self.class.new(
      arguments
        .map
        .with_index do |argument, index|
          if default
            [
              argument,
              code_delete(
                argument,
                default,
                index: Integer.new(index),
                **globals
              )
            ]
          else
            [
              argument,
              code_delete(argument, index: Integer.new(index), **globals)
            ]
          end
        end
        .to_h
    )
  end
end

#code_delete_if(argument, **globals) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/code/object/dictionary.rb', line 271

def code_delete_if(argument, **globals)
  if argument.is_a?(Class)
    raw.delete_if { |_, value| value.is_a?(argument.raw) }
  else
    raw.delete_if.with_index do |(key, value), index|
      argument.call(
        arguments: [
          Argument.new(key),
          Argument.new(value),
          Argument.new(self),
          Argument.new(Integer.new(index))
        ],
        **globals
      ).truthy?
    end
  end

  self
end

#code_delete_unless(argument, **globals) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/code/object/dictionary.rb', line 291

def code_delete_unless(argument, **globals)
  if argument.is_a?(Class)
    raw.delete_if { |_, value| !value.is_a?(argument.raw) }
  else
    raw.delete_if.with_index do |(key, value), index|
      argument.call(
        arguments: [
          Argument.new(key),
          Argument.new(value),
          Argument.new(self),
          Argument.new(Integer.new(index))
        ],
        **globals
      ).falsy?
    end
  end

  self
end

#code_digObject



311
312
313
# File 'lib/code/object/dictionary.rb', line 311

def code_dig(*)
  raw.dig(*) || Nothing.new
end

#code_each(argument, **globals) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/code/object/dictionary.rb', line 315

def code_each(argument, **globals)
  raw.each.with_index do |(key, value), index|
    argument.call(
      arguments: [
        Argument.new(key),
        Argument.new(value),
        Argument.new(self),
        Argument.new(Integer.new(index))
      ],
      **globals
    )
  end

  self
end

#code_eight?Boolean

Returns:



331
332
333
# File 'lib/code/object/dictionary.rb', line 331

def code_eight?
  code_size.code_eight?
end

#code_empty?Boolean

Returns:



335
336
337
# File 'lib/code/object/dictionary.rb', line 335

def code_empty?
  Boolean.new(raw.empty?)
end

#code_exceptObject



339
340
341
# File 'lib/code/object/dictionary.rb', line 339

def code_except(*)
  self.class.new(raw.except(*))
end

#code_fetch(*arguments, index: Integer.new(0), **globals) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
# File 'lib/code/object/dictionary.rb', line 343

def code_fetch(*arguments, index: Integer.new(0), **globals)
  default =
    (
      if arguments.last.is_a?(Function) && arguments.size > 1
        arguments.last
      end
    )

  arguments = arguments[..-2] if default

  if arguments.one?
    raw.fetch(arguments.first) do
      if default
        default.call(
          arguments: [
            Argument.new(arguments.first),
            Argument.new(self),
            Argument.new(index)
          ],
          **globals
        )
      else
        raise(
          Code::Error::KeyNotFound,
          "#{arguments.first.inspect} not found on #{inspect}"
        )
      end
    end
  else
    self.class.new(
      arguments
        .map
        .with_index do |argument, index|
          if default
            [
              argument,
              code_fetch(
                argument,
                default,
                index: Integer.new(index),
                **globals
              )
            ]
          else
            [
              argument,
              code_fetch(argument, index: Integer.new(index), **globals)
            ]
          end
        end
        .to_h
    )
  end
end

#code_fetch_valuesObject



398
399
400
# File 'lib/code/object/dictionary.rb', line 398

def code_fetch_values(*)
  List.new(raw.fetch_values(*))
end

#code_five?Boolean

Returns:



402
403
404
# File 'lib/code/object/dictionary.rb', line 402

def code_five?
  code_size.code_five?
end

#code_flatten(level = nil) ⇒ Object



406
407
408
409
# File 'lib/code/object/dictionary.rb', line 406

def code_flatten(level = nil)
  level ||= Integer.new(-1)
  code_to_list.code_flatten(level)
end

#code_four?Boolean

Returns:



411
412
413
# File 'lib/code/object/dictionary.rb', line 411

def code_four?
  code_size.code_four?
end

#code_get(key) ⇒ Object



415
416
417
# File 'lib/code/object/dictionary.rb', line 415

def code_get(key)
  raw[key] || Nothing.new
end

#code_has_key?(key) ⇒ Boolean

Returns:



419
420
421
# File 'lib/code/object/dictionary.rb', line 419

def code_has_key?(key)
  Boolean.new(raw.key?(key))
end

#code_has_value?(key) ⇒ Boolean

Returns:



423
424
425
# File 'lib/code/object/dictionary.rb', line 423

def code_has_value?(key)
  Boolean.new(raw.value?(key))
end

#code_inferior(other) ⇒ Object



427
428
429
# File 'lib/code/object/dictionary.rb', line 427

def code_inferior(other)
  Boolean.new(raw < other.raw)
end

#code_inferior_or_equal(other) ⇒ Object



431
432
433
# File 'lib/code/object/dictionary.rb', line 431

def code_inferior_or_equal(other)
  Boolean.new(raw <= other.raw)
end

#code_invertObject



435
436
437
# File 'lib/code/object/dictionary.rb', line 435

def code_invert
  self.class.new(raw.invert)
end

#code_keep_if(argument, **globals) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/code/object/dictionary.rb', line 439

def code_keep_if(argument, **globals)
  if argument.is_a?(Class)
    raw.keep_if { |_, value| value.is_a?(argument.raw) }
  else
    raw.keep_if.with_index do |(key, value), index|
      argument.call(
        arguments: [
          Argument.new(key),
          Argument.new(value),
          Argument.new(self),
          Argument.new(Integer.new(index))
        ],
        **globals
      ).truthy?
    end
  end

  self
end

#code_keep_unless(argument, **globals) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/code/object/dictionary.rb', line 459

def code_keep_unless(argument, **globals)
  if argument.is_a?(Class)
    raw.keep_if { |_, value| !value.is_a?(argument.raw) }
  else
    raw.keep_if.with_index do |(key, value), index|
      argument.call(
        arguments: [
          Argument.new(key),
          Argument.new(value),
          Argument.new(self),
          Argument.new(Integer.new(index))
        ],
        **globals
      ).falsy?
    rescue Error::Next => e
      (e.value || Nothing.new).falsy?
    end
  end

  self
end

#code_key(value, function = nil, **globals) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/code/object/dictionary.rb', line 481

def code_key(value, function = nil, **globals)
  if function
    raw.key(value) ||
      function.call(
        arguments: [Argument.new(value), Argument.new(self)],
        **globals
      )
  else
    raw.key(value) || Nothing.new
  end
rescue Error::Next => e
  e.value || Nothing.new
end

#code_keysObject



495
496
497
# File 'lib/code/object/dictionary.rb', line 495

def code_keys
  List.new(raw.keys)
end

#code_merge(*arguments, **globals) ⇒ Object



499
500
501
502
503
504
505
506
507
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
# File 'lib/code/object/dictionary.rb', line 499

def code_merge(*arguments, **globals)
  conflict =
    (
      if arguments.last.is_a?(Function) && arguments.size > 1
        arguments.last
      end
    )

  arguments = arguments[..-2] if conflict

  index = 0

  self.class.new(
    raw.merge(*arguments.map(&:raw)) do |key, old_value, new_value|
      if conflict
        conflict
          .call(
            arguments: [
              Argument.new(key),
              Argument.new(old_value),
              Argument.new(new_value),
              Argument.new(self),
              Argument.new(Integer.new(index))
            ],
            **globals
          )
          .tap { index += 1 }
      else
        new_value.tap { index += 1 }
      end
    rescue Error::Next => e
      index += 1
      e.value || Nothing.new
    end
  )
end

#code_nine?Boolean

Returns:



536
537
538
# File 'lib/code/object/dictionary.rb', line 536

def code_nine?
  code_size.code_nine?
end

#code_one?Boolean

Returns:



540
541
542
# File 'lib/code/object/dictionary.rb', line 540

def code_one?
  code_size.code_one?
end

#code_select(argument, **globals) ⇒ Object



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/code/object/dictionary.rb', line 566

def code_select(argument, **globals)
  if argument.is_a?(Class)
    self.class.new(raw.select { |_, value| value.is_a?(argument.raw) })
  else
    self.class.new(
      raw.select.with_index do |(key, value), index|
        argument.call(
          arguments: [
            Argument.new(key),
            Argument.new(value),
            Argument.new(self),
            Argument.new(Integer.new(index))
          ],
          **globals
        ).truthy?
      rescue Error::Next => e
        (e.value || Nothing.new).truthy?
      end
    )
  end
end

#code_select!(argument, **globals) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/code/object/dictionary.rb', line 544

def code_select!(argument, **globals)
  if argument.is_a?(Class)
    raw.select! { |_, value| value.is_a?(argument.raw) }
  else
    raw.select!.with_index do |(key, value), index|
      argument.call(
        arguments: [
          Argument.new(key),
          Argument.new(value),
          Argument.new(self),
          Argument.new(Integer.new(index))
        ],
        **globals
      ).truthy?
    rescue Error::Next => e
      (e.value || Nothing.new).truthy?
    end
  end

  self
end

#code_set(key, value) ⇒ Object



588
589
590
591
# File 'lib/code/object/dictionary.rb', line 588

def code_set(key, value)
  raw[key] = value
  value
end

#code_seven?Boolean

Returns:



593
594
595
# File 'lib/code/object/dictionary.rb', line 593

def code_seven?
  code_size.code_seven?
end

#code_six?Boolean

Returns:



597
598
599
# File 'lib/code/object/dictionary.rb', line 597

def code_six?
  code_size.code_six?
end

#code_sizeObject



601
602
603
# File 'lib/code/object/dictionary.rb', line 601

def code_size
  Integer.new(raw.size)
end

#code_superior(other) ⇒ Object



605
606
607
# File 'lib/code/object/dictionary.rb', line 605

def code_superior(other)
  Boolean.new(raw > other.raw)
end

#code_superior_or_equal(other) ⇒ Object



609
610
611
# File 'lib/code/object/dictionary.rb', line 609

def code_superior_or_equal(other)
  Boolean.new(raw >= other.raw)
end

#code_ten?Boolean

Returns:



613
614
615
# File 'lib/code/object/dictionary.rb', line 613

def code_ten?
  code_size.code_ten?
end

#code_three?Boolean

Returns:



617
618
619
# File 'lib/code/object/dictionary.rb', line 617

def code_three?
  code_size.code_three?
end

#code_to_contextObject



621
622
623
# File 'lib/code/object/dictionary.rb', line 621

def code_to_context
  Context.new(raw)
end

#code_to_listObject



625
626
627
# File 'lib/code/object/dictionary.rb', line 625

def code_to_list
  List.new(raw.to_a.map { |key_value| List.new(key_value) })
end

#code_transform_values(function, **globals) ⇒ Object



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/code/object/dictionary.rb', line 629

def code_transform_values(function, **globals)
  self.class.new(
    raw.transform_values.with_index do |value, index|
      function.call(
        arguments: [
          Argument.new(value),
          Argument.new(self),
          Argument.new(Integer.new(index))
        ],
        **globals
      )
    rescue Error::Next => e
      e.value || Nothing.new
    end
  )
end

#code_two?Boolean

Returns:



646
647
648
# File 'lib/code/object/dictionary.rb', line 646

def code_two?
  code_size.code_two?
end

#code_valuesObject



650
651
652
# File 'lib/code/object/dictionary.rb', line 650

def code_values
  List.new(raw.values)
end

#code_zero?Boolean

Returns:



654
655
656
# File 'lib/code/object/dictionary.rb', line 654

def code_zero?
  code_size.code_zero?
end

#inspectObject



658
659
660
# File 'lib/code/object/dictionary.rb', line 658

def inspect
  to_s
end

#to_sObject



662
663
664
# File 'lib/code/object/dictionary.rb', line 662

def to_s
  "{#{raw.map { |key, value| "#{key.inspect} => #{value.inspect}" }.join(", ")}}"
end