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



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

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

.code_getObject



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

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

.code_setObject



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

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

Instance Method Details

#<=>(other) ⇒ Object



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

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

  raw <=> code_other.raw
end

#==(other) ⇒ Object



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

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

  raw == code_other.raw
end

#as_jsonObject



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

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

#blank?Boolean

Returns:

  • (Boolean)


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

def blank?
  !present?
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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 "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 "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 "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 "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



199
200
201
202
203
# File 'lib/code/concerns/shared.rb', line 199

def code_and(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

#code_as_jsonObject



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

def code_as_json
  as_json.to_code
end

#code_blank?Boolean

Returns:

  • (Boolean)


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

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

#code_compare(other) ⇒ Object



217
218
219
220
221
# File 'lib/code/concerns/shared.rb', line 217

def code_compare(other)
  code_other = other.to_code

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

#code_deep_duplicateObject



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

def code_deep_duplicate
  self.class.new(self)
end

#code_different(other) ⇒ Object



205
206
207
208
209
# File 'lib/code/concerns/shared.rb', line 205

def code_different(other)
  code_other = other.to_code

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

#code_duplicateObject



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

def code_duplicate
  self.class.new(self)
end

#code_equal(other) ⇒ Object



211
212
213
214
215
# File 'lib/code/concerns/shared.rb', line 211

def code_equal(other)
  code_other = other.to_code

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

#code_exclamation_markObject



247
248
249
# File 'lib/code/concerns/shared.rb', line 247

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

#code_exclusive_range(value) ⇒ Object



251
252
253
254
255
# File 'lib/code/concerns/shared.rb', line 251

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)


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

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

#code_fetchObject



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

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

#code_getObject



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

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

#code_greater(other) ⇒ Object



223
224
225
226
227
# File 'lib/code/concerns/shared.rb', line 223

def code_greater(other)
  code_other = other.to_code

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

#code_greater_or_equal(other) ⇒ Object



229
230
231
232
233
# File 'lib/code/concerns/shared.rb', line 229

def code_greater_or_equal(other)
  code_other = other.to_code

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

#code_inclusive_range(value) ⇒ Object



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

def code_inclusive_range(value)
  code_value = value.to_code

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

#code_inspectObject



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

def code_inspect
  code_to_string
end

#code_less(other) ⇒ Object



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

def code_less(other)
  code_other = other.to_code

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

#code_less_or_equal(other) ⇒ Object



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

def code_less_or_equal(other)
  code_other = other.to_code

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

#code_methodsObject



463
464
465
# File 'lib/code/concerns/shared.rb', line 463

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

#code_nameObject



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

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

#code_nothing?Boolean

Returns:

  • (Boolean)


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

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

#code_or(other) ⇒ Object



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

def code_or(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

#code_presenceObject



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

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

#code_presence_in(list = []) ⇒ Object



487
488
489
490
491
# File 'lib/code/concerns/shared.rb', line 487

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)


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

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

#code_selfObject



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

def code_self
  self
end

#code_setObject



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

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

#code_something?Boolean

Returns:

  • (Boolean)


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

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

#code_strict_different(other) ⇒ Object



279
280
281
282
283
# File 'lib/code/concerns/shared.rb', line 279

def code_strict_different(other)
  code_other = other.to_code

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

#code_strict_equal(other) ⇒ Object



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

def code_strict_equal(other)
  code_other = other.to_code

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

#code_to_booleanObject



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

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

#code_to_classObject



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

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

#code_to_dateObject



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

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

#code_to_decimalObject



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

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

#code_to_dictionaryObject



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

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

#code_to_durationObject



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

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

#code_to_integerObject



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

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

#code_to_json(pretty: nil) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/code/concerns/shared.rb', line 315

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



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

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

#code_to_nothingObject



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

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

#code_to_parameterObject



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

def code_to_parameter
  code_to_string.code_parameterize
end

#code_to_rangeObject



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

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

#code_to_stringObject



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

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

#code_to_timeObject



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

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

#code_truthy?Boolean

Returns:

  • (Boolean)


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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
# File 'lib/code/concerns/shared.rb', line 192

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)


285
286
287
# File 'lib/code/concerns/shared.rb', line 285

def falsy?
  !truthy?
end

#hashObject



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

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

#inspectObject



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

def inspect
  code_inspect.raw
end

#multi_fetch(hash, *keys) ⇒ Object



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

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

#nothing?Boolean

Returns:

  • (Boolean)


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

def nothing?
  false
end

#present?Boolean

Returns:

  • (Boolean)


467
468
469
# File 'lib/code/concerns/shared.rb', line 467

def present?
  true
end

#sig(args, &block) ⇒ Object



297
298
299
300
301
# File 'lib/code/concerns/shared.rb', line 297

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

  Object::Nothing.new
end

#something?Boolean

Returns:

  • (Boolean)


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

def something?
  !nothing?
end

#succObject



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

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

#to_codeObject



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

def to_code
  self
end

#to_iObject



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

def to_i
  code_to_integer.raw
end

#to_jsonObject



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

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

#to_sObject



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

def to_s
  code_to_string.raw
end

#truthy?Boolean

Returns:

  • (Boolean)


303
304
305
# File 'lib/code/concerns/shared.rb', line 303

def truthy?
  true
end