Module: Code::Concerns::Shared

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#methodsObject

Returns the value of attribute methods.



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

def methods
  @methods
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



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

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

.code_getObject



336
337
338
# File 'lib/code/concerns/shared.rb', line 336

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

.code_setObject



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

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

Instance Method Details

#<=>(other) ⇒ Object



163
164
165
166
167
168
# File 'lib/code/concerns/shared.rb', line 163

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

  raw <=> code_other.raw
end

#==(other) ⇒ Object



170
171
172
173
174
175
# File 'lib/code/concerns/shared.rb', line 170

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

  raw == code_other.raw
end

#as_jsonObject



296
297
298
# File 'lib/code/concerns/shared.rb', line 296

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

#call(**args) ⇒ Object



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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/code/concerns/shared.rb', line 8

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

  case code_operator.to_s
  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"
    sig(args) { Object }
    code_equal(code_value)
  when ">", "greater_or_equal"
    sig(args) { Object }
    code_greater(code_value)
  when ">=", "greater"
    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 "||", "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 "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 "methods"
    sig(args)
    code_methods
  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)
    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



184
185
186
187
188
# File 'lib/code/concerns/shared.rb', line 184

def code_and(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

#code_as_jsonObject



308
309
310
# File 'lib/code/concerns/shared.rb', line 308

def code_as_json
  as_json.to_code
end

#code_compare(other) ⇒ Object



202
203
204
205
206
# File 'lib/code/concerns/shared.rb', line 202

def code_compare(other)
  code_other = other.to_code

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

#code_deep_duplicateObject



324
325
326
# File 'lib/code/concerns/shared.rb', line 324

def code_deep_duplicate
  self.class.new(self)
end

#code_different(other) ⇒ Object



190
191
192
193
194
# File 'lib/code/concerns/shared.rb', line 190

def code_different(other)
  code_other = other.to_code

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

#code_duplicateObject



320
321
322
# File 'lib/code/concerns/shared.rb', line 320

def code_duplicate
  self.class.new(self)
end

#code_equal(other) ⇒ Object



196
197
198
199
200
# File 'lib/code/concerns/shared.rb', line 196

def code_equal(other)
  code_other = other.to_code

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

#code_exclamation_markObject



232
233
234
# File 'lib/code/concerns/shared.rb', line 232

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

#code_exclusive_range(value) ⇒ Object



236
237
238
239
240
# File 'lib/code/concerns/shared.rb', line 236

def code_exclusive_range(value)
  code_value = value.to_code

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

#code_falsy?Boolean

Returns:

  • (Boolean)


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

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

#code_fetchObject



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

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

#code_getObject



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

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

#code_greater(other) ⇒ Object



208
209
210
211
212
# File 'lib/code/concerns/shared.rb', line 208

def code_greater(other)
  code_other = other.to_code

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

#code_greater_or_equal(other) ⇒ Object



214
215
216
217
218
# File 'lib/code/concerns/shared.rb', line 214

def code_greater_or_equal(other)
  code_other = other.to_code

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

#code_inclusive_range(value) ⇒ Object



242
243
244
245
246
# File 'lib/code/concerns/shared.rb', line 242

def code_inclusive_range(value)
  code_value = value.to_code

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

#code_inspectObject



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

def code_inspect
  code_to_string
end

#code_less(other) ⇒ Object



220
221
222
223
224
# File 'lib/code/concerns/shared.rb', line 220

def code_less(other)
  code_other = other.to_code

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

#code_less_or_equal(other) ⇒ Object



226
227
228
229
230
# File 'lib/code/concerns/shared.rb', line 226

def code_less_or_equal(other)
  code_other = other.to_code

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

#code_methodsObject



448
449
450
# File 'lib/code/concerns/shared.rb', line 448

def code_methods
  Object::List.new(methods)
end

#code_nameObject



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

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

#code_nothing?Boolean

Returns:

  • (Boolean)


368
369
370
# File 'lib/code/concerns/shared.rb', line 368

def code_nothing?
  Boolean.new(nothing?)
end

#code_or(other) ⇒ Object



248
249
250
251
252
# File 'lib/code/concerns/shared.rb', line 248

def code_or(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

#code_selfObject



254
255
256
# File 'lib/code/concerns/shared.rb', line 254

def code_self
  self
end

#code_setObject



344
345
346
# File 'lib/code/concerns/shared.rb', line 344

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

#code_something?Boolean

Returns:

  • (Boolean)


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

def code_something?
  Boolean.new(something?)
end

#code_strict_different(other) ⇒ Object



264
265
266
267
268
# File 'lib/code/concerns/shared.rb', line 264

def code_strict_different(other)
  code_other = other.to_code

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

#code_strict_equal(other) ⇒ Object



258
259
260
261
262
# File 'lib/code/concerns/shared.rb', line 258

def code_strict_equal(other)
  code_other = other.to_code

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

#code_to_booleanObject



392
393
394
# File 'lib/code/concerns/shared.rb', line 392

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

#code_to_classObject



396
397
398
# File 'lib/code/concerns/shared.rb', line 396

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

#code_to_dateObject



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

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

#code_to_decimalObject



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

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

#code_to_dictionaryObject



408
409
410
# File 'lib/code/concerns/shared.rb', line 408

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

#code_to_durationObject



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

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

#code_to_integerObject



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

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

#code_to_json(pretty: nil) ⇒ Object



300
301
302
303
304
305
306
# File 'lib/code/concerns/shared.rb', line 300

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



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

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

#code_to_nothingObject



424
425
426
# File 'lib/code/concerns/shared.rb', line 424

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

#code_to_parameterObject



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

def code_to_parameter
  code_to_string.code_parameterize
end

#code_to_rangeObject



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

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

#code_to_stringObject



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

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

#code_to_timeObject



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

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

#code_truthy?Boolean

Returns:

  • (Boolean)


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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
180
181
182
# File 'lib/code/concerns/shared.rb', line 177

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)


270
271
272
# File 'lib/code/concerns/shared.rb', line 270

def falsy?
  !truthy?
end

#hashObject



274
275
276
# File 'lib/code/concerns/shared.rb', line 274

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

#inspectObject



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

def inspect
  code_inspect.raw
end

#multi_fetch(hash, *keys) ⇒ Object



278
279
280
# File 'lib/code/concerns/shared.rb', line 278

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

#nothing?Boolean

Returns:

  • (Boolean)


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

def nothing?
  false
end

#sig(args, &block) ⇒ Object



282
283
284
285
286
# File 'lib/code/concerns/shared.rb', line 282

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

  Object::Nothing.new
end

#something?Boolean

Returns:

  • (Boolean)


380
381
382
# File 'lib/code/concerns/shared.rb', line 380

def something?
  !nothing?
end

#succObject



316
317
318
# File 'lib/code/concerns/shared.rb', line 316

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

#to_codeObject



312
313
314
# File 'lib/code/concerns/shared.rb', line 312

def to_code
  self
end

#to_iObject



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

def to_i
  code_to_integer.raw
end

#to_jsonObject



292
293
294
# File 'lib/code/concerns/shared.rb', line 292

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

#to_sObject



356
357
358
# File 'lib/code/concerns/shared.rb', line 356

def to_s
  code_to_string.raw
end

#truthy?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/code/concerns/shared.rb', line 288

def truthy?
  true
end