Class: Code::Object

Inherits:
Object show all
Defined in:
lib/code/object.rb,
lib/code/object/code.rb,
lib/code/object/date.rb,
lib/code/object/html.rb,
lib/code/object/http.rb,
lib/code/object/json.rb,
lib/code/object/list.rb,
lib/code/object/time.rb,
lib/code/object/class.rb,
lib/code/object/range.rb,
lib/code/object/global.rb,
lib/code/object/string.rb,
lib/code/object/boolean.rb,
lib/code/object/context.rb,
lib/code/object/decimal.rb,
lib/code/object/integer.rb,
lib/code/object/nothing.rb,
lib/code/object/duration.rb,
lib/code/object/function.rb,
lib/code/object/parameter.rb,
lib/code/object/dictionary.rb,
lib/code/object/identifier_list.rb

Defined Under Namespace

Classes: Boolean, Class, Code, Context, Date, Decimal, Dictionary, Duration, Function, Global, Html, Http, IdentifierList, Integer, Json, List, Nothing, Parameter, Range, String, Time

Constant Summary collapse

NUMBER_CLASSES =
[
  Integer,
  Decimal,
  String,
  ::Integer,
  ::Float,
  ::String,
  ::BigDecimal
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Returns a new instance of Object.



19
20
# File 'lib/code/object.rb', line 19

def initialize(...)
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



15
16
17
# File 'lib/code/object.rb', line 15

def raw
  @raw
end

Class Method Details

.as_jsonObject



241
242
243
# File 'lib/code/object.rb', line 241

def self.as_json(...)
  name.as_json(...)
end

.call(**args) ⇒ Object



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

def self.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_point
  when "!=", "different"
    sig(args) { Object }
    code_different(code_value)
  when "&&", "and"
    sig(args) { Object }
    code_and_operator(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_equal(code_value)
  when "===", "strict_equal"
    sig(args) { Object }
    code_equal_equal_equal(code_value)
  when "falsy?"
    sig(args)
    Boolean.new(falsy?)
  when "truthy?"
    sig(args)
    Boolean.new(truthy?)
  when "||", "or"
    sig(args) { Object }
    code_or_operator(code_value)
  when "to_boolean"
    sig(args)
    Boolean.new(self)
  when "to_class"
    sig(args)
    Class.new(self)
  when "to_date"
    sig(args)
    Date.new(self)
  when "to_decimal"
    sig(args)
    Decimal.new(self)
  when "to_dictionary"
    sig(args)
    Dictionary.new(self)
  when "to_duration"
    sig(args)
    Duration.new(self)
  when "to_integer"
    sig(args)
    Integer.new(self)
  when "to_list"
    sig(args)
    List.new(self)
  when "to_nothing"
    sig(args)
    Nothing.new(self)
  when "to_range"
    sig(args)
    Range.new(self)
  when "to_string"
    sig(args)
    String.new(self)
  when "to_time"
    sig(args)
    Time.new(self)
  when "as_json"
    sig(args)
    code_as_json
  when "to_json"
    sig(args) { { pretty: Boolean.maybe } }

    if code_arguments.any?
      code_to_json(pretty: code_value.code_get(:pretty))
    else
      code_to_json
    end
  when /=$/
    sig(args) { Object }

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

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

.code_and_operator(other) ⇒ Object



161
162
163
164
165
# File 'lib/code/object.rb', line 161

def self.code_and_operator(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

.code_as_jsonObject



253
254
255
# File 'lib/code/object.rb', line 253

def self.code_as_json
  to_ruby.as_json.to_code
end

.code_different(other) ⇒ Object



167
168
169
170
171
# File 'lib/code/object.rb', line 167

def self.code_different(other)
  code_other = other.to_code

  Boolean.new(self != code_other)
end

.code_equal_equal(other) ⇒ Object



173
174
175
176
177
# File 'lib/code/object.rb', line 173

def self.code_equal_equal(other)
  code_other = other.to_code

  Boolean.new(self == code_other)
end

.code_equal_equal_equal(other) ⇒ Object



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

def self.code_equal_equal_equal(other)
  code_other = other.to_code

  Boolean.new(self === code_other)
end

.code_exclamation_pointObject



179
180
181
# File 'lib/code/object.rb', line 179

def self.code_exclamation_point
  Boolean.new(falsy?)
end

.code_exclusive_range(value) ⇒ Object



183
184
185
186
187
# File 'lib/code/object.rb', line 183

def self.code_exclusive_range(value)
  code_value = value.to_code

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

.code_inclusive_range(value) ⇒ Object



189
190
191
192
193
# File 'lib/code/object.rb', line 189

def self.code_inclusive_range(value)
  code_value = value.to_code

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

.code_new(*arguments) ⇒ Object



155
156
157
158
159
# File 'lib/code/object.rb', line 155

def self.code_new(*arguments)
  code_arguments = arguments.to_code

  new(*code_arguments.raw)
end

.code_or_operator(other) ⇒ Object



195
196
197
198
199
# File 'lib/code/object.rb', line 195

def self.code_or_operator(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

.code_selfObject



201
202
203
# File 'lib/code/object.rb', line 201

def self.code_self
  self
end

.code_to_json(pretty: nil) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/code/object.rb', line 245

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

.falsy?Boolean

Returns:



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

def self.falsy?
  !truthy?
end

.inspectObject



229
230
231
# File 'lib/code/object.rb', line 229

def self.inspect
  name
end

.maybeObject



30
31
32
# File 'lib/code/object.rb', line 30

def self.maybe
  Type::Maybe.new(self)
end

.multi_fetch(hash, *keys) ⇒ Object



215
216
217
# File 'lib/code/object.rb', line 215

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

.nothing?Boolean

Returns:



22
23
24
# File 'lib/code/object.rb', line 22

def self.nothing?
  false
end

.repeat(minimum = 0, maximum = nil) ⇒ Object



34
35
36
# File 'lib/code/object.rb', line 34

def self.repeat(minimum = 0, maximum = nil)
  Type::Repeat.new(self, minimum:, maximum:)
end

.sig(args) ⇒ Object



219
220
221
222
223
# File 'lib/code/object.rb', line 219

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

  Nothing.new
end

.to_jsonObject



237
238
239
# File 'lib/code/object.rb', line 237

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

.to_sObject



225
226
227
# File 'lib/code/object.rb', line 225

def self.to_s
  name
end

.truthy?Boolean

Returns:



233
234
235
# File 'lib/code/object.rb', line 233

def self.truthy?
  true
end

.|(other) ⇒ Object



38
39
40
# File 'lib/code/object.rb', line 38

def self.|(other)
  Type::Or.new(self, other)
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

  raw <=> code_other.raw
end

#==(other) ⇒ Object Also known as: eql?



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

def ==(other)
  code_other = other.to_code

  raw == code_other.raw
end

#as_jsonObject



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

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

#call(**args) ⇒ Object



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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
# File 'lib/code/object.rb', line 270

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 "!", "not"
    sig(args)
    code_exclamation_point
  when "!=", "different"
    sig(args) { Object }
    code_different(code_value)
  when "&&", "and"
    sig(args) { Object }
    code_and_operator(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_equal(code_value)
  when "===", "strict_equal"
    sig(args) { Object }
    code_equal_equal_equal(code_value)
  when "falsy?"
    sig(args)
    Boolean.new(falsy?)
  when "truthy?"
    sig(args)
    Boolean.new(truthy?)
  when "||", "or"
    sig(args) { Object }
    code_or_operator(code_value)
  when "to_boolean"
    sig(args)
    Boolean.new(self)
  when "to_class"
    sig(args)
    Class.new(self)
  when "to_date"
    sig(args)
    Date.new(self)
  when "to_decimal"
    sig(args)
    Decimal.new(self)
  when "to_duration"
    sig(args)
    Duration.new(self)
  when "to_dictionary"
    sig(args)
    Dictionary.new(self)
  when "to_integer"
    sig(args)
    Integer.new(self)
  when "to_list"
    sig(args)
    List.new(self)
  when "to_nothing"
    sig(args)
    Nothing.new(self)
  when "to_range"
    sig(args)
    Range.new(self)
  when "to_string"
    sig(args)
    String.new(self)
  when "to_time"
    sig(args)
    Time.new(self)
  when "as_json"
    sig(args)
    code_as_json
  when "to_json"
    sig(args) { { pretty: Boolean.maybe } }

    if code_arguments.any?
      code_to_json(pretty: code_value.code_get(:pretty))
    else
      code_to_json
    end
  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: List.new([code_value])
        )
      )
    end

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

#code_and_operator(other) ⇒ Object



383
384
385
386
387
# File 'lib/code/object.rb', line 383

def code_and_operator(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

#code_as_jsonObject



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

def code_as_json
  as_json.to_code
end

#code_different(other) ⇒ Object



389
390
391
392
393
# File 'lib/code/object.rb', line 389

def code_different(other)
  code_other = other.to_code

  Boolean.new(self != code_other)
end

#code_equal_equal(other) ⇒ Object



395
396
397
398
399
# File 'lib/code/object.rb', line 395

def code_equal_equal(other)
  code_other = other.to_code

  Boolean.new(self == code_other)
end

#code_equal_equal_equal(other) ⇒ Object



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

def code_equal_equal_equal(other)
  code_other = other.to_code

  Boolean.new(self === code_other)
end

#code_exclamation_pointObject



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

def code_exclamation_point
  Boolean.new(falsy?)
end

#code_exclusive_range(value) ⇒ Object



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

def code_exclusive_range(value)
  code_value = value.to_code

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

#code_inclusive_range(value) ⇒ Object



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

def code_inclusive_range(value)
  code_value = value.to_code

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

#code_or_operator(other) ⇒ Object



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

def code_or_operator(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

#code_selfObject



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

def code_self
  self
end

#code_to_json(pretty: nil) ⇒ Object



463
464
465
466
467
468
469
# File 'lib/code/object.rb', line 463

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

#falsy?Boolean

Returns:



433
434
435
# File 'lib/code/object.rb', line 433

def falsy?
  !truthy?
end

#hashObject



437
438
439
# File 'lib/code/object.rb', line 437

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

#multi_fetch(hash, *keys) ⇒ Object



441
442
443
# File 'lib/code/object.rb', line 441

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

#nothing?Boolean

Returns:



26
27
28
# File 'lib/code/object.rb', line 26

def nothing?
  false
end

#sig(args) ⇒ Object



445
446
447
448
449
# File 'lib/code/object.rb', line 445

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

  Nothing.new
end

#succObject



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

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

#to_codeObject



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

def to_code
  self
end

#to_jsonObject



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

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

#truthy?Boolean

Returns:



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

def truthy?
  true
end