Module: Code::Concerns::Shared

Included in:
Object, Object
Defined in:
lib/code/concerns/shared.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/code/concerns/shared.rb', line 6

def raw
  @raw
end

Class Method Details

.code_fetchObject



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

def self.code_fetch(...)
  Object::Nothing.new
end

.code_getObject



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

def self.code_get(...)
  Object::Nothing.new
end

.code_setObject



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

def self.code_set(...)
  Object::Nothing.new
end

Instance Method Details

#<=>(other) ⇒ Object



136
137
138
139
140
# File 'lib/code/concerns/shared.rb', line 136

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

  [raw, self.class] <=> [code_other.raw, code_other.class]
end

#==(other) ⇒ Object



142
143
144
145
146
# File 'lib/code/concerns/shared.rb', line 142

def ==(other)
  code_other = other.to_code

  [raw, self.class] == [code_other.raw, code_other.class]
end

#as_jsonObject



230
231
232
# File 'lib/code/concerns/shared.rb', line 230

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

#call(**args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/code/concerns/shared.rb', line 8

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 "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(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)
    code_falsy?
  when "truthy?"
    sig(args)
    code_truthy?
  when "||", "or"
    sig(args) { Object }
    code_or(code_value)
  when "to_boolean"
    sig(args)
    code_to_boolean
  when "to_class"
    sig(args)
    code_to_class
  when "to_date"
    sig(args)
    code_to_date
  when "to_decimal"
    sig(args)
    code_to_decimal
  when "to_dictionary"
    sig(args)
    code_to_dictionary
  when "to_duration"
    sig(args)
    code_to_duration
  when "to_integer"
    sig(args)
    code_to_integer
  when "to_list"
    sig(args)
    code_to_list
  when "to_nothing"
    sig(args)
    code_to_nothing
  when "to_range"
    sig(args)
    code_to_range
  when "to_string"
    sig(args)
    code_to_string
  when "to_time"
    sig(args)
    code_to_time
  when "as_json"
    sig(args)
    code_as_json
  when "duplicate"
    sig(args)
    code_duplicate
  when "deep_duplicate"
    sig(args)
    code_deep_duplicate
  when "to_parameter"
    sig(args)
    code_to_parameter
  when "to_json"
    sig(args) { { pretty: Object::Boolean.maybe } }

    if code_arguments.any?
      code_to_json(pretty: code_value.code_get(:pretty))
    else
      code_to_json
    end
  when "name"
    sig(args)
    code_name
  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: Object::List.new([code_value])
        )
      )
    end

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

#code_and(other) ⇒ Object



154
155
156
157
158
# File 'lib/code/concerns/shared.rb', line 154

def code_and(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

#code_as_jsonObject



242
243
244
# File 'lib/code/concerns/shared.rb', line 242

def code_as_json
  as_json.to_code
end

#code_deep_duplicateObject



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

def code_deep_duplicate
  self.class.new(self)
end

#code_different(other) ⇒ Object



160
161
162
163
164
# File 'lib/code/concerns/shared.rb', line 160

def code_different(other)
  code_other = other.to_code

  Object::Boolean.new(self != code_other)
end

#code_duplicateObject



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

def code_duplicate
  self.class.new(self)
end

#code_equal_equal(other) ⇒ Object



166
167
168
169
170
# File 'lib/code/concerns/shared.rb', line 166

def code_equal_equal(other)
  code_other = other.to_code

  Object::Boolean.new(self == code_other)
end

#code_equal_equal_equal(other) ⇒ Object



198
199
200
201
202
# File 'lib/code/concerns/shared.rb', line 198

def code_equal_equal_equal(other)
  code_other = other.to_code

  Object::Boolean.new(self === code_other)
end

#code_exclamation_pointObject



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

def code_exclamation_point
  Object::Boolean.new(falsy?)
end

#code_exclusive_range(value) ⇒ Object



176
177
178
179
180
# File 'lib/code/concerns/shared.rb', line 176

def code_exclusive_range(value)
  code_value = value.to_code

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

#code_falsy?Boolean

Returns:

  • (Boolean)


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

def code_falsy?
  Object::Boolean.new(falsy?)
end

#code_fetchObject



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

def code_fetch(...)
  Object::Nothing.new
end

#code_getObject



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

def code_get(...)
  Object::Nothing.new
end

#code_inclusive_range(value) ⇒ Object



182
183
184
185
186
# File 'lib/code/concerns/shared.rb', line 182

def code_inclusive_range(value)
  code_value = value.to_code

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

#code_inspectObject



414
415
416
# File 'lib/code/concerns/shared.rb', line 414

def code_inspect
  code_to_string
end

#code_nameObject



418
419
420
# File 'lib/code/concerns/shared.rb', line 418

def code_name
  Object::String.new(name.to_s.split("::")[2..].join("::"))
end

#code_or(other) ⇒ Object



188
189
190
191
192
# File 'lib/code/concerns/shared.rb', line 188

def code_or(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

#code_selfObject



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

def code_self
  self
end

#code_setObject



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

def code_set(...)
  Object::Nothing.new
end

#code_to_booleanObject



298
299
300
# File 'lib/code/concerns/shared.rb', line 298

def code_to_boolean
  Object::Boolean.new(self)
end

#code_to_classObject



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

def code_to_class
  Object::Class.new(self)
end

#code_to_dateObject



306
307
308
# File 'lib/code/concerns/shared.rb', line 306

def code_to_date
  Object::Date.new(self)
end

#code_to_decimalObject



310
311
312
# File 'lib/code/concerns/shared.rb', line 310

def code_to_decimal
  Object::Decimal.new(self)
end

#code_to_dictionaryObject



314
315
316
# File 'lib/code/concerns/shared.rb', line 314

def code_to_dictionary
  Object::Dictionary.new(self)
end

#code_to_durationObject



318
319
320
# File 'lib/code/concerns/shared.rb', line 318

def code_to_duration
  Object::Duration.new(self)
end

#code_to_integerObject



322
323
324
# File 'lib/code/concerns/shared.rb', line 322

def code_to_integer
  Object::Integer.new(self)
end

#code_to_json(pretty: nil) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/code/concerns/shared.rb', line 234

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

#code_to_listObject



326
327
328
# File 'lib/code/concerns/shared.rb', line 326

def code_to_list
  Object::List.new(self)
end

#code_to_nothingObject



330
331
332
# File 'lib/code/concerns/shared.rb', line 330

def code_to_nothing
  Object::Nothing.new(self)
end

#code_to_parameterObject



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

def code_to_parameter
  code_to_string.code_parameterize
end

#code_to_rangeObject



334
335
336
# File 'lib/code/concerns/shared.rb', line 334

def code_to_range
  Object::Range.new(self)
end

#code_to_stringObject



338
339
340
# File 'lib/code/concerns/shared.rb', line 338

def code_to_string
  Object::String.new(self)
end

#code_to_timeObject



342
343
344
# File 'lib/code/concerns/shared.rb', line 342

def code_to_time
  Object::Time.new(self)
end

#code_truthy?Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/code/concerns/shared.rb', line 294

def code_truthy?
  Object::Boolean.new(truthy?)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
# File 'lib/code/concerns/shared.rb', line 148

def eql?(other)
  code_other = other.to_code

  [raw, self.class].eql?([code_other.raw, code_other.class])
end

#falsy?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/code/concerns/shared.rb', line 204

def falsy?
  !truthy?
end

#hashObject



208
209
210
# File 'lib/code/concerns/shared.rb', line 208

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

#inspectObject



350
351
352
# File 'lib/code/concerns/shared.rb', line 350

def inspect
  code_inspect.raw
end

#multi_fetch(hash, *keys) ⇒ Object



212
213
214
# File 'lib/code/concerns/shared.rb', line 212

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

#nothing?Boolean

Returns:

  • (Boolean)


354
355
356
# File 'lib/code/concerns/shared.rb', line 354

def nothing?
  false
end

#sig(args) ⇒ Object



216
217
218
219
220
# File 'lib/code/concerns/shared.rb', line 216

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

  Object::Nothing.new
end

#succObject



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

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

#to_codeObject



246
247
248
# File 'lib/code/concerns/shared.rb', line 246

def to_code
  self
end

#to_jsonObject



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

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

#to_sObject



346
347
348
# File 'lib/code/concerns/shared.rb', line 346

def to_s
  code_to_string.raw
end

#truthy?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/code/concerns/shared.rb', line 222

def truthy?
  true
end