Module: Code::Concerns::Shared

Included in:
Object, Object
Defined in:
lib/code/concerns/shared.rb

Constant Summary collapse

COMPOUND_ASSIGNMENT_OPERATORS =
%w[
  +=
  -=
  *=
  /=
  %=
  <<=
  >>=
  &=
  |=
  ^=
  ||=
  &&=
].freeze
SHARED_OPERATORS =
%w[
  documentation
  present?
  blank?
  presence
  presence_in
  is_a?
  is_an?
  kind_of?
  instance_of?
  new
  !
  not
  !=
  different
  &&
  and
  +
  self
  ..
  inclusive_range
  ...
  exclusive_range
  ==
  equal
  equal?
  same_object?
  >
  greater
  >=
  greater_or_equal
  <=>
  compare
  <
  less
  <=
  less_or_equal
  ===
  strict_equal
  !==
  strict_different
  falsy?
  truthy?
  true?
  false?
  ||
  or
  to_boolean
  to_class
  to_date
  to_decimal
  to_dictionary
  to_duration
  to_integer
  to_list
  to_nothing
  to_range
  to_string
  inspect
  to_time
  as_json
  duplicate
  deep_duplicate
  to_parameter
  to_json
  functions
  instance_functions
  class_functions
  respond_to?
  send
  itself
  tap
  then
  name
  nothing?
  something?
].freeze
OPERATOR_METHOD_ALIASES =
{
  "[]" => "get",
  "at" => "get",
  "length" => "size",
  "member?" => "include?",
  "key?" => "has_key?",
  "value?" => "has_value?",
  "filter" => "select",
  "filter!" => "select!"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#functionsObject

Returns the value of attribute functions.



6
7
8
# File 'lib/code/concerns/shared.rb', line 6

def functions
  @functions
end

#rawObject

Returns the value of attribute raw.



6
7
8
# File 'lib/code/concerns/shared.rb', line 6

def raw
  @raw
end

Class Method Details

.code_fetchObject



490
491
492
# File 'lib/code/concerns/shared.rb', line 490

def self.code_fetch(...)
  Object::Nothing.new
end

.code_getObject



498
499
500
# File 'lib/code/concerns/shared.rb', line 498

def self.code_get(...)
  Object::Nothing.new
end

.code_setObject



494
495
496
# File 'lib/code/concerns/shared.rb', line 494

def self.code_set(...)
  Object::Nothing.new
end

Instance Method Details

#<=>(other) ⇒ Object



325
326
327
328
329
330
# File 'lib/code/concerns/shared.rb', line 325

def <=>(other)
  code_other = other.to_code
  return -1 if self.class != code_other.class

  raw <=> code_other.raw
end

#==(other) ⇒ Object



332
333
334
335
336
337
# File 'lib/code/concerns/shared.rb', line 332

def ==(other)
  code_other = other.to_code
  return false if self.class != code_other.class

  raw == code_other.raw
end

#as_jsonObject



458
459
460
# File 'lib/code/concerns/shared.rb', line 458

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

#blank?Boolean

Returns:

  • (Boolean)


718
719
720
# File 'lib/code/concerns/shared.rb', line 718

def blank?
  !present?
end

#call(**args) ⇒ Object



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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/code/concerns/shared.rb', line 110

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  dynamic_result = code_dynamic_call(code_operator, **args)
  return dynamic_result if dynamic_result

  case code_operator.to_s
  when "documentation"
    sig(args)
    code_documentation
  when "present?"
    sig(args)
    code_present?
  when "blank?"
    sig(args)
    code_blank?
  when "presence"
    sig(args)
    code_presence
  when "presence_in"
    sig(args) { Object::List }
    code_presence_in(code_value)
  when "is_a?", "is_an?", "kind_of?"
    sig(args) { Object::Class }
    code_is_a?(code_value)
  when "instance_of?"
    sig(args) { Object::Class }
    code_instance_of?(code_value)
  when "new"
    sig(args) { Object.repeat }
    code_new(*code_arguments.raw)
  when "!", "not"
    sig(args)
    code_exclamation_mark
  when "!=", "different"
    sig(args) { Object }
    code_different(code_value)
  when "&&", "and"
    sig(args) { Object }
    code_and(code_value)
  when "+", "self"
    sig(args)
    code_self
  when "..", "inclusive_range"
    sig(args) { Object }
    code_inclusive_range(code_value)
  when "...", "exclusive_range"
    sig(args) { Object }
    code_exclusive_range(code_value)
  when "==", "equal", "equal?"
    sig(args) { Object }
    code_equal(code_value)
  when "same_object?"
    sig(args) { Object }
    code_same_object?(code_value)
  when ">", "greater"
    sig(args) { Object }
    code_greater(code_value)
  when ">=", "greater_or_equal"
    sig(args) { Object }
    code_greater_or_equal(code_value)
  when "<=>", "compare"
    sig(args) { Object }
    code_compare(code_value)
  when "<", "less"
    sig(args) { Object }
    code_less(code_value)
  when "<=", "less_or_equal"
    sig(args) { Object }
    code_less_or_equal(code_value)
  when "===", "strict_equal"
    sig(args) { Object }
    code_strict_equal(code_value)
  when "!==", "strict_different"
    sig(args) { Object }
    code_strict_different(code_value)
  when "falsy?"
    sig(args)
    code_falsy?
  when "truthy?"
    sig(args)
    code_truthy?
  when "true?"
    sig(args)
    code_true?
  when "false?"
    sig(args)
    code_false?
  when "||", "or"
    sig(args) { Object }
    code_or(code_value)
  when "to_boolean"
    sig(args)
    code_to_boolean
  when "to_class"
    sig(args)
    code_to_class
  when "to_date"
    sig(args)
    code_to_date
  when "to_decimal"
    sig(args)
    code_to_decimal
  when "to_dictionary"
    sig(args)
    code_to_dictionary
  when "to_duration"
    sig(args)
    code_to_duration
  when "to_integer"
    sig(args)
    code_to_integer
  when "to_list"
    sig(args)
    code_to_list
  when "to_nothing"
    sig(args)
    code_to_nothing
  when "to_range"
    sig(args)
    code_to_range
  when "to_string"
    sig(args)
    code_to_string
  when "inspect"
    sig(args)
    code_inspect
  when "to_time"
    sig(args)
    code_to_time
  when "as_json"
    sig(args)
    code_as_json
  when "duplicate"
    sig(args)
    code_duplicate
  when "deep_duplicate"
    sig(args)
    code_deep_duplicate
  when "to_parameter"
    sig(args)
    code_to_parameter
  when "to_json"
    sig(args) { { pretty: Object::Boolean.maybe } }

    if code_arguments.any?
      code_to_json(pretty: code_value.code_get(:pretty))
    else
      code_to_json
    end
  when "functions"
    sig(args)
    code_functions
  when "instance_functions"
    sig(args)
    code_instance_functions
  when "class_functions"
    sig(args)
    code_class_functions
  when "respond_to?"
    sig(args) { Object }
    code_respond_to?(code_value)
  when "send"
    sig(args) { [Object, Object.repeat] }
    code_send(*code_arguments.raw, **args)
  when "itself"
    sig(args)
    code_itself
  when "tap"
    sig(args) { Object::Function }
    code_tap(code_value, **args)
  when "then"
    sig(args) { Object::Function }
    code_then(code_value, **args)
  when "name"
    sig(args)
    code_name
  when "nothing?"
    sig(args)
    code_nothing?
  when "something?"
    sig(args)
    code_something?
  when /=$/
    sig(args) { Object }

    if code_operator.to_s == "="
      code_context = args.fetch(:context)
      code_context.code_set(self, code_value)
    elsif setter_operator?(code_operator)
      code_dynamic_functions.code_set(code_operator.to_s.chop, code_value)
      return code_value
    else
      code_context = args.fetch(:context).code_lookup!(self)
      code_context.code_set(
        self,
        code_context.code_fetch(self).call(
          **args,
          operator: code_operator.to_s.chop,
          arguments: Object::List.new([code_value])
        )
      )
    end

    code_context.code_fetch(self)
  else
    raise(
      Error,
      "#{code_operator.inspect} not defined on #{code_inspect}:#{code_name}"
    )
  end
end

#code_and(other) ⇒ Object



346
347
348
349
350
# File 'lib/code/concerns/shared.rb', line 346

def code_and(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

#code_as_jsonObject



470
471
472
# File 'lib/code/concerns/shared.rb', line 470

def code_as_json
  as_json.to_code
end

#code_blank?Boolean

Returns:

  • (Boolean)


726
727
728
# File 'lib/code/concerns/shared.rb', line 726

def code_blank?
  Object::Boolean.new(blank?)
end

#code_class_functionsObject



653
654
655
# File 'lib/code/concerns/shared.rb', line 653

def code_class_functions
  dynamic_functions_documentation
end

#code_compare(other) ⇒ Object



364
365
366
367
368
# File 'lib/code/concerns/shared.rb', line 364

def code_compare(other)
  code_other = other.to_code

  Object::Integer.new(self <=> code_other)
end

#code_deep_duplicate(_seen = {}) ⇒ Object



486
487
488
# File 'lib/code/concerns/shared.rb', line 486

def code_deep_duplicate(_seen = {})
  self.class.new(self)
end

#code_different(other) ⇒ Object



352
353
354
355
356
# File 'lib/code/concerns/shared.rb', line 352

def code_different(other)
  code_other = other.to_code

  Object::Boolean.new(self != code_other)
end

#code_documentable_functionsObject



740
741
742
743
744
# File 'lib/code/concerns/shared.rb', line 740

def code_documentable_functions
  Object::Dictionary.new(
    code_dynamic_functions.raw.merge(dictionary_attributes)
  )
end

#code_documentationObject



634
635
636
# File 'lib/code/concerns/shared.rb', line 634

def code_documentation
  Object.documentation_for(self.class)
end

#code_duplicateObject



482
483
484
# File 'lib/code/concerns/shared.rb', line 482

def code_duplicate
  self.class.new(self)
end

#code_dynamic_call(operator, **args) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
# File 'lib/code/concerns/shared.rb', line 518

def code_dynamic_call(operator, **args)
  return nil unless code_dynamic_functions.code_has_key?(operator).truthy?

  stored_value = code_dynamic_functions.code_fetch(operator)
  if stored_value.is_a?(Object::Function)
    return stored_value.call(**args, operator: nil, bound_self: self)
  end

  sig(args)
  stored_value
end

#code_equal(other) ⇒ Object



358
359
360
361
362
# File 'lib/code/concerns/shared.rb', line 358

def code_equal(other)
  code_other = other.to_code

  Object::Boolean.new(self == code_other)
end

#code_exclamation_markObject



394
395
396
# File 'lib/code/concerns/shared.rb', line 394

def code_exclamation_mark
  Object::Boolean.new(falsy?)
end

#code_exclusive_range(value) ⇒ Object



398
399
400
401
402
# File 'lib/code/concerns/shared.rb', line 398

def code_exclusive_range(value)
  code_value = value.to_code

  Object::Range.new(self, code_value, exclude_end: true)
end

#code_false?Boolean

Returns:

  • (Boolean)


574
575
576
# File 'lib/code/concerns/shared.rb', line 574

def code_false?
  Object::Boolean.new(self == Object::Boolean.new(false) || nothing?)
end

#code_falsy?Boolean

Returns:

  • (Boolean)


562
563
564
# File 'lib/code/concerns/shared.rb', line 562

def code_falsy?
  Object::Boolean.new(falsy?)
end

#code_fetch(key) ⇒ Object



502
503
504
# File 'lib/code/concerns/shared.rb', line 502

def code_fetch(key)
  code_dynamic_functions.code_fetch(key)
end

#code_functionsObject



638
639
640
641
642
# File 'lib/code/concerns/shared.rb', line 638

def code_functions
  Object.sorted_dictionary(
    code_instance_functions.code_merge(code_class_functions).raw
  )
end

#code_get(key) ⇒ Object



510
511
512
# File 'lib/code/concerns/shared.rb', line 510

def code_get(key)
  code_dynamic_functions.code_get(key)
end

#code_greater(other) ⇒ Object



370
371
372
373
374
# File 'lib/code/concerns/shared.rb', line 370

def code_greater(other)
  code_other = other.to_code

  Object::Boolean.new((self <=> code_other).positive?)
end

#code_greater_or_equal(other) ⇒ Object



376
377
378
379
380
# File 'lib/code/concerns/shared.rb', line 376

def code_greater_or_equal(other)
  code_other = other.to_code

  Object::Boolean.new((self <=> code_other) >= 0)
end

#code_has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


514
515
516
# File 'lib/code/concerns/shared.rb', line 514

def code_has_key?(key)
  code_dynamic_functions.code_has_key?(key)
end

#code_inclusive_range(value) ⇒ Object



404
405
406
407
408
# File 'lib/code/concerns/shared.rb', line 404

def code_inclusive_range(value)
  code_value = value.to_code

  Object::Range.new(self, code_value, exclude_end: false)
end

#code_inspectObject



626
627
628
# File 'lib/code/concerns/shared.rb', line 626

def code_inspect
  code_to_string
end

#code_instance_functionsObject



644
645
646
647
648
649
650
651
# File 'lib/code/concerns/shared.rb', line 644

def code_instance_functions
  Object.sorted_dictionary(
    Object
      .documented_functions_for(self.class, :instance)
      .code_merge(dynamic_functions_documentation)
      .raw
  )
end

#code_instance_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


669
670
671
672
673
# File 'lib/code/concerns/shared.rb', line 669

def code_instance_of?(klass)
  code_klass = klass.to_code

  Object::Boolean.new(instance_of?(code_klass.raw))
end

#code_is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


663
664
665
666
667
# File 'lib/code/concerns/shared.rb', line 663

def code_is_a?(klass)
  code_klass = klass.to_code

  Object::Boolean.new(is_a?(code_klass.raw))
end

#code_itselfObject



681
682
683
# File 'lib/code/concerns/shared.rb', line 681

def code_itself
  self
end

#code_less(other) ⇒ Object



382
383
384
385
386
# File 'lib/code/concerns/shared.rb', line 382

def code_less(other)
  code_other = other.to_code

  Object::Boolean.new((self <=> code_other).negative?)
end

#code_less_or_equal(other) ⇒ Object



388
389
390
391
392
# File 'lib/code/concerns/shared.rb', line 388

def code_less_or_equal(other)
  code_other = other.to_code

  Object::Boolean.new((self <=> code_other) <= 0)
end

#code_nameObject



630
631
632
# File 'lib/code/concerns/shared.rb', line 630

def code_name
  Object::String.new(name.to_s.split("::")[2..].join("::"))
end

#code_nothing?Boolean

Returns:

  • (Boolean)


546
547
548
# File 'lib/code/concerns/shared.rb', line 546

def code_nothing?
  Object::Boolean.new(nothing?)
end

#code_or(other) ⇒ Object



410
411
412
413
414
# File 'lib/code/concerns/shared.rb', line 410

def code_or(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

#code_presenceObject



730
731
732
# File 'lib/code/concerns/shared.rb', line 730

def code_presence
  present? ? self : Object::Nothing.new
end

#code_presence_in(list = []) ⇒ Object



734
735
736
737
738
# File 'lib/code/concerns/shared.rb', line 734

def code_presence_in(list = [])
  code_list = list.to_code

  code_list.code_include?(self).truthy? ? self : Object::Nothing.new
end

#code_present?Boolean

Returns:

  • (Boolean)


722
723
724
# File 'lib/code/concerns/shared.rb', line 722

def code_present?
  Object::Boolean.new(present?)
end

#code_respond_to?(operator) ⇒ Boolean

Returns:

  • (Boolean)


657
658
659
660
661
# File 'lib/code/concerns/shared.rb', line 657

def code_respond_to?(operator)
  code_operator = operator.to_code

  Object::Boolean.new(code_respond_to_operator?(code_operator))
end

#code_same_object?(other) ⇒ Boolean

Returns:

  • (Boolean)


675
676
677
678
679
# File 'lib/code/concerns/shared.rb', line 675

def code_same_object?(other)
  code_other = other.to_code

  Object::Boolean.new(equal?(code_other))
end

#code_selfObject



416
417
418
# File 'lib/code/concerns/shared.rb', line 416

def code_self
  self
end

#code_send(operator, *arguments, **globals) ⇒ Object



685
686
687
688
689
690
691
692
693
# File 'lib/code/concerns/shared.rb', line 685

def code_send(operator, *arguments, **globals)
  code_operator = operator.to_code

  call(
    **globals,
    arguments: Object::List.new(arguments),
    operator: code_operator
  )
end

#code_set(key, value) ⇒ Object



506
507
508
# File 'lib/code/concerns/shared.rb', line 506

def code_set(key, value)
  code_dynamic_functions.code_set(key, value)
end

#code_something?Boolean

Returns:

  • (Boolean)


550
551
552
# File 'lib/code/concerns/shared.rb', line 550

def code_something?
  Object::Boolean.new(something?)
end

#code_strict_different(other) ⇒ Object



426
427
428
429
430
# File 'lib/code/concerns/shared.rb', line 426

def code_strict_different(other)
  code_other = other.to_code

  Object::Boolean.new(!(self === code_other))
end

#code_strict_equal(other) ⇒ Object



420
421
422
423
424
# File 'lib/code/concerns/shared.rb', line 420

def code_strict_equal(other)
  code_other = other.to_code

  Object::Boolean.new(self === code_other)
end

#code_tap(function, **globals) ⇒ Object



695
696
697
698
699
700
701
702
703
# File 'lib/code/concerns/shared.rb', line 695

def code_tap(function, **globals)
  code_function = function.to_code
  code_function.call(
    **globals,
    arguments: Object::List.new([self]),
    operator: nil
  )
  self
end

#code_then(function, **globals) ⇒ Object



705
706
707
708
709
710
711
712
# File 'lib/code/concerns/shared.rb', line 705

def code_then(function, **globals)
  code_function = function.to_code
  code_function.call(
    **globals,
    arguments: Object::List.new([self]),
    operator: nil
  )
end

#code_to_booleanObject



578
579
580
# File 'lib/code/concerns/shared.rb', line 578

def code_to_boolean
  Object::Boolean.new(self)
end

#code_to_classObject



582
583
584
# File 'lib/code/concerns/shared.rb', line 582

def code_to_class
  Object::Class.new(self)
end

#code_to_dateObject



586
587
588
# File 'lib/code/concerns/shared.rb', line 586

def code_to_date
  Object::Date.new(self)
end

#code_to_decimalObject



590
591
592
# File 'lib/code/concerns/shared.rb', line 590

def code_to_decimal
  Object::Decimal.new(self)
end

#code_to_dictionaryObject



594
595
596
# File 'lib/code/concerns/shared.rb', line 594

def code_to_dictionary
  Object::Dictionary.new(self)
end

#code_to_durationObject



598
599
600
# File 'lib/code/concerns/shared.rb', line 598

def code_to_duration
  Object::Duration.new(self)
end

#code_to_integerObject



602
603
604
# File 'lib/code/concerns/shared.rb', line 602

def code_to_integer
  Object::Integer.new(self)
end

#code_to_json(pretty: nil) ⇒ Object



462
463
464
465
466
467
468
# File 'lib/code/concerns/shared.rb', line 462

def code_to_json(pretty: nil)
  if Object::Boolean.new(pretty).truthy?
    Object::String.new(::JSON.pretty_generate(self))
  else
    Object::String.new(to_json)
  end
end

#code_to_listObject



606
607
608
# File 'lib/code/concerns/shared.rb', line 606

def code_to_list
  Object::List.new(self)
end

#code_to_nothingObject



610
611
612
# File 'lib/code/concerns/shared.rb', line 610

def code_to_nothing
  Object::Nothing.new(self)
end

#code_to_parameterObject



530
531
532
# File 'lib/code/concerns/shared.rb', line 530

def code_to_parameter
  code_to_string.code_parameterize
end

#code_to_rangeObject



614
615
616
# File 'lib/code/concerns/shared.rb', line 614

def code_to_range
  Object::Range.new(self)
end

#code_to_stringObject



618
619
620
# File 'lib/code/concerns/shared.rb', line 618

def code_to_string
  Object::String.new(self)
end

#code_to_timeObject



622
623
624
# File 'lib/code/concerns/shared.rb', line 622

def code_to_time
  Object::Time.new(self)
end

#code_true?Boolean

Returns:

  • (Boolean)


570
571
572
# File 'lib/code/concerns/shared.rb', line 570

def code_true?
  Object::Boolean.new(self == Object::Boolean.new(true))
end

#code_truthy?Boolean

Returns:

  • (Boolean)


566
567
568
# File 'lib/code/concerns/shared.rb', line 566

def code_truthy?
  Object::Boolean.new(truthy?)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


339
340
341
342
343
344
# File 'lib/code/concerns/shared.rb', line 339

def eql?(other)
  code_other = other.to_code
  return false if self.class != code_other.class

  raw.eql?(code_other.raw)
end

#falsy?Boolean

Returns:

  • (Boolean)


432
433
434
# File 'lib/code/concerns/shared.rb', line 432

def falsy?
  !truthy?
end

#hashObject



436
437
438
# File 'lib/code/concerns/shared.rb', line 436

def hash
  [self.class, raw].hash
end

#inspectObject



542
543
544
# File 'lib/code/concerns/shared.rb', line 542

def inspect
  code_inspect.raw
end

#multi_fetch(hash, *keys) ⇒ Object



440
441
442
# File 'lib/code/concerns/shared.rb', line 440

def multi_fetch(hash, *keys)
  keys.to_h { |key| [key, hash.fetch(key)] }
end

#nothing?Boolean

Returns:

  • (Boolean)


554
555
556
# File 'lib/code/concerns/shared.rb', line 554

def nothing?
  false
end

#present?Boolean

Returns:

  • (Boolean)


714
715
716
# File 'lib/code/concerns/shared.rb', line 714

def present?
  true
end

#sig(args, &block) ⇒ Object



444
445
446
447
448
# File 'lib/code/concerns/shared.rb', line 444

def sig(args, &block)
  Type::Sig.sig(args, object: self, &block)

  Object::Nothing.new
end

#something?Boolean

Returns:

  • (Boolean)


558
559
560
# File 'lib/code/concerns/shared.rb', line 558

def something?
  !nothing?
end

#succObject



478
479
480
# File 'lib/code/concerns/shared.rb', line 478

def succ
  self.class.new(raw.succ)
end

#to_codeObject



474
475
476
# File 'lib/code/concerns/shared.rb', line 474

def to_code
  self
end

#to_iObject



538
539
540
# File 'lib/code/concerns/shared.rb', line 538

def to_i
  code_to_integer.raw
end

#to_jsonObject



454
455
456
# File 'lib/code/concerns/shared.rb', line 454

def to_json(...)
  as_json(...).to_json(...)
end

#to_sObject



534
535
536
# File 'lib/code/concerns/shared.rb', line 534

def to_s
  code_to_string.raw
end

#truthy?Boolean

Returns:

  • (Boolean)


450
451
452
# File 'lib/code/concerns/shared.rb', line 450

def truthy?
  true
end