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/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, IdentifierList, Integer, Json, List, Nothing, Parameter, Range, String, Time

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Returns a new instance of Object.



7
8
# File 'lib/code/object.rb', line 7

def initialize(...)
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



5
6
7
# File 'lib/code/object.rb', line 5

def raw
  @raw
end

Class Method Details

.as_jsonObject



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

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

.call(**args) ⇒ Object



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

def self.call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, List.new)
  value = arguments.code_first

  case operator.to_s
  when "new"
    sig(args) { Object.repeat }
    code_new(*arguments.raw)
  when "!", "not"
    sig(args)
    code_exclamation_point
  when "!=", "different"
    sig(args) { Object }
    code_different(value)
  when "&&", "and"
    sig(args) { Object }
    code_and_operator(value)
  when "+", "self"
    sig(args)
    code_self
  when "..", "inclusive_range"
    sig(args) { Object }
    code_inclusive_range(value)
  when "...", "exclusive_range"
    sig(args) { Object }
    code_exclusive_range(value)
  when "==", "equal"
    sig(args) { Object }
    code_equal_equal(value)
  when "===", "strict_equal"
    sig(args) { Object }
    code_equal_equal_equal(value)
  when "falsy?"
    sig(args)
    Boolean.new(falsy?)
  when "truthy?"
    sig(args)
    Boolean.new(truthy?)
  when "||", "or"
    sig(args) { Object }
    code_or_operator(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 arguments.any?
      code_to_json(pretty: value.code_get(String.new(:pretty)))
    else
      code_to_json
    end
  when /=$/
    sig(args) { Object }

    if operator == "="
      context = args[:context]
      context.code_set(self, value)
    else
      context = args[:context].lookup!(self)
      context.code_set(
        self,
        context.code_fetch(self).call(
          **args,
          operator: operator.chop,
          arguments: List.new([value])
        )
      )
    end

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

.code_and_operator(other) ⇒ Object



141
142
143
# File 'lib/code/object.rb', line 141

def self.code_and_operator(other)
  truthy? ? other : self
end

.code_as_jsonObject



218
219
220
# File 'lib/code/object.rb', line 218

def self.code_as_json
  Json.to_code(as_json)
end

.code_different(other) ⇒ Object



145
146
147
# File 'lib/code/object.rb', line 145

def self.code_different(other)
  Boolean.new(self != other)
end

.code_equal_equal(other) ⇒ Object



149
150
151
# File 'lib/code/object.rb', line 149

def self.code_equal_equal(other)
  Boolean.new(self == other)
end

.code_equal_equal_equal(other) ⇒ Object



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

def self.code_equal_equal_equal(other)
  Boolean.new(self === other)
end

.code_exclamation_pointObject



153
154
155
# File 'lib/code/object.rb', line 153

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

.code_exclusive_range(value) ⇒ Object



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

def self.code_exclusive_range(value)
  Range.new(self, value, exclude_end: true)
end

.code_inclusive_range(value) ⇒ Object



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

def self.code_inclusive_range(value)
  Range.new(self, value, exclude_end: false)
end

.code_newObject



137
138
139
# File 'lib/code/object.rb', line 137

def self.code_new(*)
  new(*)
end

.code_or_operator(other) ⇒ Object



165
166
167
# File 'lib/code/object.rb', line 165

def self.code_or_operator(other)
  truthy? ? self : other
end

.code_selfObject



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

def self.code_self
  self
end

.code_to_json(pretty: nil) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/code/object.rb', line 210

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:



177
178
179
# File 'lib/code/object.rb', line 177

def self.falsy?
  !truthy?
end

.inspectObject



194
195
196
# File 'lib/code/object.rb', line 194

def self.inspect
  to_s
end

.maybeObject



10
11
12
# File 'lib/code/object.rb', line 10

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

.multi_fetch(hash, *keys) ⇒ Object



181
182
183
# File 'lib/code/object.rb', line 181

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

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



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

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

.sig(args) ⇒ Object



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

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

.to_jsonObject



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

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

.to_sObject



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

def self.to_s
  name
end

.truthy?Boolean

Returns:



198
199
200
# File 'lib/code/object.rb', line 198

def self.truthy?
  true
end

.|(other) ⇒ Object



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

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

Instance Method Details

#<=>(other) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/code/object.rb', line 222

def <=>(other)
  if respond_to?(:raw)
    raw <=> (other.respond_to?(:raw) ? other.raw : other)
  else
    other <=> self
  end
end

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



230
231
232
233
234
235
236
# File 'lib/code/object.rb', line 230

def ==(other)
  if respond_to?(:raw)
    raw == (other.respond_to?(:raw) ? other.raw : other)
  else
    other == self
  end
end

#as_jsonObject



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

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

#call(**args) ⇒ Object



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

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, List.new)
  value = arguments.code_first

  case operator.to_s
  when "!", "not"
    sig(args)
    code_exclamation_point
  when "!=", "different"
    sig(args) { Object }
    code_different(value)
  when "&&", "and"
    sig(args) { Object }
    code_and_operator(value)
  when "+", "self"
    sig(args)
    code_self
  when "..", "inclusive_range"
    sig(args) { Object }
    code_inclusive_range(value)
  when "...", "exclusive_range"
    sig(args) { Object }
    code_exclusive_range(value)
  when "==", "equal"
    sig(args) { Object }
    code_equal_equal(value)
  when "===", "strict_equal"
    sig(args) { Object }
    code_equal_equal_equal(value)
  when "falsy?"
    sig(args)
    Boolean.new(falsy?)
  when "truthy?"
    sig(args)
    Boolean.new(truthy?)
  when "||", "or"
    sig(args) { Object }
    code_or_operator(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 arguments.any?
      code_to_json(pretty: value.code_get(String.new(:pretty)))
    else
      code_to_json
    end
  when /=$/
    sig(args) { Object }

    if operator == "="
      context = args[:context]
      context.code_set(self, value)
    else
      context = args[:context].lookup!(self)
      context.code_set(
        self,
        context.code_fetch(self).call(
          **args,
          operator: operator.chop,
          arguments: List.new([value])
        )
      )
    end

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

#code_and_operator(other) ⇒ Object



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

def code_and_operator(other)
  truthy? ? other : self
end

#code_as_jsonObject



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

def code_as_json
  Json.to_code(as_json)
end

#code_different(other) ⇒ Object



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

def code_different(other)
  Boolean.new(self != other)
end

#code_equal_equal(other) ⇒ Object



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

def code_equal_equal(other)
  Boolean.new(self == other)
end

#code_equal_equal_equal(other) ⇒ Object



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

def code_equal_equal_equal(other)
  Boolean.new(self === other)
end

#code_exclamation_pointObject



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

def code_exclamation_point
  Boolean.new(falsy?)
end

#code_exclusive_range(value) ⇒ Object



367
368
369
370
371
372
373
# File 'lib/code/object.rb', line 367

def code_exclusive_range(value)
  Range.new(
    self,
    value,
    Dictionary.new({ String.new(:exclude_end) => Boolean.new(true) })
  )
end

#code_inclusive_range(value) ⇒ Object



375
376
377
378
379
380
381
# File 'lib/code/object.rb', line 375

def code_inclusive_range(value)
  Range.new(
    self,
    value,
    Dictionary.new({ String.new(:exclude_end) => Boolean.new(false) })
  )
end

#code_or_operator(other) ⇒ Object



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

def code_or_operator(other)
  truthy? ? self : other
end

#code_selfObject



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

def code_self
  self
end

#code_to_json(pretty: nil) ⇒ Object



432
433
434
435
436
437
438
# File 'lib/code/object.rb', line 432

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:



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

def falsy?
  !truthy?
end

#hashObject



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

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

#inspectObject



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

def inspect
  to_s
end

#multi_fetch(hash, *keys) ⇒ Object



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

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

#sig(args) ⇒ Object



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

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

#succObject



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

def succ
  raw.respond_to?(:succ) ? self.class.new(raw.succ) : self.class.new(self)
end

#to_jsonObject



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

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

#to_sObject



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

def to_s
  raw.to_s
end

#truthy?Boolean

Returns:



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

def truthy?
  true
end