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



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

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

.code_getObject



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

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

.code_setObject



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

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

Instance Method Details

#<=>(other) ⇒ Object



148
149
150
151
152
# File 'lib/code/concerns/shared.rb', line 148

def <=>(other)
  code_other = other.to_code

  [raw, self.class] <=> [code_other.raw, code_other.class]
end

#==(other) ⇒ Object



154
155
156
157
158
# File 'lib/code/concerns/shared.rb', line 154

def ==(other)
  code_other = other.to_code

  [raw, self.class] == [code_other.raw, code_other.class]
end

#as_jsonObject



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

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
# 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 "===", "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



166
167
168
169
170
# File 'lib/code/concerns/shared.rb', line 166

def code_and(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

#code_as_jsonObject



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

def code_as_json
  as_json.to_code
end

#code_deep_duplicateObject



276
277
278
# File 'lib/code/concerns/shared.rb', line 276

def code_deep_duplicate
  self.class.new(self)
end

#code_different(other) ⇒ Object



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

def code_different(other)
  code_other = other.to_code

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

#code_duplicateObject



272
273
274
# File 'lib/code/concerns/shared.rb', line 272

def code_duplicate
  self.class.new(self)
end

#code_equal(other) ⇒ Object



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

def code_equal(other)
  code_other = other.to_code

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

#code_exclamation_markObject



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

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

#code_exclusive_range(value) ⇒ Object



188
189
190
191
192
# File 'lib/code/concerns/shared.rb', line 188

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)


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

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

#code_fetchObject



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

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

#code_getObject



300
301
302
# File 'lib/code/concerns/shared.rb', line 300

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

#code_inclusive_range(value) ⇒ Object



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

def code_inclusive_range(value)
  code_value = value.to_code

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

#code_inspectObject



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

def code_inspect
  code_to_string
end

#code_methodsObject



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

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

#code_nameObject



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

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

#code_nothing?Boolean

Returns:

  • (Boolean)


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

def code_nothing?
  Boolean.new(nothing?)
end

#code_or(other) ⇒ Object



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

def code_or(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

#code_selfObject



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

def code_self
  self
end

#code_setObject



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

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

#code_something?Boolean

Returns:

  • (Boolean)


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

def code_something?
  Boolean.new(something?)
end

#code_strict_different(other) ⇒ Object



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

def code_strict_different(other)
  code_other = other.to_code

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

#code_strict_equal(other) ⇒ Object



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

def code_strict_equal(other)
  code_other = other.to_code

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

#code_to_booleanObject



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

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

#code_to_classObject



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

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

#code_to_dateObject



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

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

#code_to_decimalObject



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

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

#code_to_dictionaryObject



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

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

#code_to_durationObject



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

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

#code_to_integerObject



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

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

#code_to_json(pretty: nil) ⇒ Object



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

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



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

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

#code_to_nothingObject



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

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

#code_to_parameterObject



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

def code_to_parameter
  code_to_string.code_parameterize
end

#code_to_rangeObject



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

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

#code_to_stringObject



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

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

#code_to_timeObject



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

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

#code_truthy?Boolean

Returns:

  • (Boolean)


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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
# File 'lib/code/concerns/shared.rb', line 160

def eql?(other)
  code_other = other.to_code

  [raw, self.class].eql?([code_other.raw, code_other.class])
end

#falsy?Boolean

Returns:

  • (Boolean)


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

def falsy?
  !truthy?
end

#hashObject



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

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

#inspectObject



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

def inspect
  code_inspect.raw
end

#multi_fetch(hash, *keys) ⇒ Object



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

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

#nothing?Boolean

Returns:

  • (Boolean)


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

def nothing?
  false
end

#sig(args, &block) ⇒ Object



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

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

  Object::Nothing.new
end

#something?Boolean

Returns:

  • (Boolean)


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

def something?
  !nothing?
end

#succObject



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

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

#to_codeObject



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

def to_code
  self
end

#to_iObject



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

def to_i
  code_to_integer.raw
end

#to_jsonObject



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

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

#to_sObject



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

def to_s
  code_to_string.raw
end

#truthy?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/code/concerns/shared.rb', line 240

def truthy?
  true
end