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/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, 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



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

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

.call(**args) ⇒ Object



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

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.zone.local(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



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

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

.code_as_jsonObject



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

def self.code_as_json
  Json.to_code(as_json)
end

.code_different(other) ⇒ Object



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

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

.code_equal_equal(other) ⇒ Object



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

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

.code_equal_equal_equal(other) ⇒ Object



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

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

.code_exclamation_pointObject



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

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

.code_exclusive_range(value) ⇒ Object



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

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

.code_inclusive_range(value) ⇒ Object



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

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

.code_newObject



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

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

.code_or_operator(other) ⇒ Object



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

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

.code_selfObject



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

def self.code_self
  self
end

.code_to_json(pretty: nil) ⇒ Object



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

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:



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

def self.falsy?
  !truthy?
end

.inspectObject



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

def self.inspect
  name
end

.maybeObject



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

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

.multi_fetch(hash, *keys) ⇒ Object



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

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

.nothing?Boolean

Returns:



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

def self.nothing?
  false
end

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



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

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

.sig(args) ⇒ Object



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

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

.to_jsonObject



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

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

.to_sObject



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

def self.to_s
  name
end

.truthy?Boolean

Returns:



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

def self.truthy?
  true
end

.|(other) ⇒ Object



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

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

Instance Method Details

#<=>(other) ⇒ Object



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

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



238
239
240
241
242
243
244
# File 'lib/code/object.rb', line 238

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

#as_jsonObject



430
431
432
# File 'lib/code/object.rb', line 430

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

#call(**args) ⇒ Object



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
350
351
352
353
354
355
356
357
# File 'lib/code/object.rb', line 247

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.zone.local(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



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

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

#code_as_jsonObject



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

def code_as_json
  Json.to_code(as_json)
end

#code_different(other) ⇒ Object



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

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

#code_equal_equal(other) ⇒ Object



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

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

#code_equal_equal_equal(other) ⇒ Object



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

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

#code_exclamation_pointObject



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

def code_exclamation_point
  Boolean.new(falsy?)
end

#code_exclusive_range(value) ⇒ Object



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

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

#code_inclusive_range(value) ⇒ Object



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

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

#code_or_operator(other) ⇒ Object



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

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

#code_selfObject



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

def code_self
  self
end

#code_to_json(pretty: nil) ⇒ Object



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

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:



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

def falsy?
  !truthy?
end

#hashObject



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

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

#multi_fetch(hash, *keys) ⇒ Object



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

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

#nothing?Boolean

Returns:



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

def nothing?
  false
end

#sig(args) ⇒ Object



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

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

#succObject



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

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

#to_jsonObject



426
427
428
# File 'lib/code/object.rb', line 426

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

#truthy?Boolean

Returns:



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

def truthy?
  true
end