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/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/argument.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: Argument, Boolean, Class, Code, Context, Date, Decimal, Dictionary, Duration, Function, Global, IdentifierList, Integer, List, Nothing, Parameter, Range, String, Time

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args, **_kargs, &_block) ⇒ Object

Returns a new instance of Object.



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

def initialize(*_args, **_kargs, &_block)
  @raw = nil unless defined?(@raw)
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

.call(**args) ⇒ Object



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

def self.call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  value = arguments.first&.value

  case operator.to_s
  when "new"
    sig(args) { Object.repeat }
    code_new(*arguments.map(&:value))
  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 "to_string"
    sig(args)
    code_to_string
  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 /=$/
    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[..-2],
          arguments: [Argument.new(value)]
        )
      )
    end

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

.code_and_operator(other) ⇒ Object



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

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

.code_different(other) ⇒ Object



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

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

.code_equal_equal(other) ⇒ Object



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

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

.code_equal_equal_equal(other) ⇒ Object



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

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

.code_exclamation_pointObject



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

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

.code_exclusive_range(value) ⇒ Object



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

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

.code_inclusive_range(value) ⇒ Object



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

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

.code_new(*arguments) ⇒ Object



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

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

.code_or_operator(other) ⇒ Object



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

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

.code_selfObject



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

def self.code_self
  self
end

.code_to_stringObject



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

def self.code_to_string
  String.new(to_s)
end

.falsy?Boolean

Returns:



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

def self.falsy?
  !truthy?
end

.inspectObject



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

def self.inspect
  to_s
end

.maybeObject



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

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

.multi_fetch(hash, *keys) ⇒ Object



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

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

.nameObject



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

def self.name
  "Object"
end

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



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

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

.sig(args, &block) ⇒ Object



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

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

.to_sObject



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

def self.to_s
  name
end

.truthy?Boolean

Returns:



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

def self.truthy?
  true
end

.|(other) ⇒ Object



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

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

Instance Method Details

#<=>(other) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/code/object.rb', line 213

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?



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

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

#as_jsonObject



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

def as_json(...)
  raw.as_json
end

#call(**args) ⇒ Object



230
231
232
233
234
235
236
237
238
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
# File 'lib/code/object.rb', line 230

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  value = arguments.first&.value

  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 "to_string"
    sig(args)
    code_to_string
  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 /=$/
    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[..-2],
          arguments: [Argument.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



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

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

#code_different(other) ⇒ Object



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

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

#code_equal_equal(other) ⇒ Object



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

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

#code_equal_equal_equal(other) ⇒ Object



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

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

#code_exclamation_pointObject



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

def code_exclamation_point
  Boolean.new(falsy?)
end

#code_exclusive_range(value) ⇒ Object



351
352
353
354
355
356
357
358
359
# File 'lib/code/object.rb', line 351

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

#code_inclusive_range(value) ⇒ Object



361
362
363
364
365
366
367
368
369
# File 'lib/code/object.rb', line 361

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

#code_or_operator(other) ⇒ Object



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

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

#code_selfObject



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

def code_self
  self
end

#code_to_stringObject



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

def code_to_string
  String.new(to_s)
end

#falsy?Boolean

Returns:



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

def falsy?
  !truthy?
end

#hashObject



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

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

#inspectObject



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

def inspect
  to_s
end

#multi_fetch(hash, *keys) ⇒ Object



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

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

#sig(args, &block) ⇒ Object



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

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

#to_jsonObject



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

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

#to_sObject



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

def to_s
  raw.to_s
end

#truthy?Boolean

Returns:



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

def truthy?
  true
end