Class: Code::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/code/object.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/number.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/dictionary.rb,
lib/code/object/identifier_list.rb

Defined Under Namespace

Classes: Argument, Boolean, Class, Context, Date, Decimal, Dictionary, Duration, Function, Global, IdentifierList, Integer, List, Nothing, Number, Range, String, Time

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(**args) ⇒ Object



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

def self.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 /=$/
    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(
      Code::Error::Undefined,
      "#{operator} not defined on #{inspect}:Class"
    )
  end
end

.code_and_operator(other) ⇒ Object



95
96
97
# File 'lib/code/object.rb', line 95

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

.code_different(other) ⇒ Object



99
100
101
# File 'lib/code/object.rb', line 99

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

.code_equal_equal(other) ⇒ Object



103
104
105
# File 'lib/code/object.rb', line 103

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

.code_equal_equal_equal(other) ⇒ Object



127
128
129
# File 'lib/code/object.rb', line 127

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

.code_exclamation_pointObject



107
108
109
# File 'lib/code/object.rb', line 107

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

.code_exclusive_range(value) ⇒ Object



111
112
113
# File 'lib/code/object.rb', line 111

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

.code_inclusive_range(value) ⇒ Object



115
116
117
# File 'lib/code/object.rb', line 115

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

.code_or_operator(other) ⇒ Object



119
120
121
# File 'lib/code/object.rb', line 119

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

.code_selfObject



123
124
125
# File 'lib/code/object.rb', line 123

def self.code_self
  self
end

.code_to_stringObject



131
132
133
# File 'lib/code/object.rb', line 131

def self.code_to_string
  String.new(to_s)
end

.falsy?Boolean

Returns:



135
136
137
# File 'lib/code/object.rb', line 135

def self.falsy?
  !truthy?
end

.inspectObject



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

def self.inspect
  to_s
end

.maybeObject



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

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

.multi_fetch(hash, *keys) ⇒ Object



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

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

.nameObject



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

def self.name
  "Object"
end

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



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

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

.sig(args, &block) ⇒ Object



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

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

.to_sObject



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

def self.to_s
  name
end

.truthy?Boolean

Returns:



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

def self.truthy?
  true
end

.|(other) ⇒ Object



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

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

Instance Method Details

#<=>(other) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/code/object.rb', line 164

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?



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

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

#as_jsonObject

Raises:

  • (NotImplementedError)


327
328
329
# File 'lib/code/object.rb', line 327

def as_json(...)
  raise NotImplementedError, "#{self.class}#as_json"
end

#call(**args) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/code/object.rb', line 181

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 /=$/
    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(
      Code::Error::Undefined,
      "#{operator} not defined on #{inspect}:#{self.class.name}"
    )
  end
end

#code_and_operator(other) ⇒ Object



250
251
252
# File 'lib/code/object.rb', line 250

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

#code_different(other) ⇒ Object



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

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

#code_equal_equal(other) ⇒ Object



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

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

#code_equal_equal_equal(other) ⇒ Object



282
283
284
# File 'lib/code/object.rb', line 282

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

#code_exclamation_pointObject



262
263
264
# File 'lib/code/object.rb', line 262

def code_exclamation_point
  Boolean.new(falsy?)
end

#code_exclusive_range(value) ⇒ Object



266
267
268
# File 'lib/code/object.rb', line 266

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

#code_inclusive_range(value) ⇒ Object



270
271
272
# File 'lib/code/object.rb', line 270

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

#code_or_operator(other) ⇒ Object



274
275
276
# File 'lib/code/object.rb', line 274

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

#code_selfObject



278
279
280
# File 'lib/code/object.rb', line 278

def code_self
  self
end

#code_to_stringObject



286
287
288
# File 'lib/code/object.rb', line 286

def code_to_string
  String.new(to_s)
end

#falsy?Boolean

Returns:



290
291
292
# File 'lib/code/object.rb', line 290

def falsy?
  !truthy?
end

#hashObject



294
295
296
297
298
299
300
# File 'lib/code/object.rb', line 294

def hash
  unless respond_to?(:raw)
    raise NotImplementedError, "#{self.class.name}#hash"
  end

  [self.class, raw].hash
end

#inspectObject



315
316
317
# File 'lib/code/object.rb', line 315

def inspect
  to_s
end

#multi_fetch(hash, *keys) ⇒ Object



302
303
304
# File 'lib/code/object.rb', line 302

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

#sig(args, &block) ⇒ Object



306
307
308
309
# File 'lib/code/object.rb', line 306

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

#to_jsonObject



323
324
325
# File 'lib/code/object.rb', line 323

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

#to_sObject

Raises:

  • (NotImplementedError)


311
312
313
# File 'lib/code/object.rb', line 311

def to_s
  raise NotImplementedError, "#{self.class.name}#to_s"
end

#truthy?Boolean

Returns:



319
320
321
# File 'lib/code/object.rb', line 319

def truthy?
  true
end