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.



4
5
6
# File 'lib/code/concerns/shared.rb', line 4

def raw
  @raw
end

Class Method Details

.code_fetchObject



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

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

.code_getObject



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

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

.code_setObject



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

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

Instance Method Details

#<=>(other) ⇒ Object



131
132
133
134
135
# File 'lib/code/concerns/shared.rb', line 131

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

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

#==(other) ⇒ Object



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

def ==(other)
  code_other = other.to_code

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

#as_jsonObject



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

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

#call(**args) ⇒ Object



6
7
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
# File 'lib/code/concerns/shared.rb', line 6

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



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

def code_and(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

#code_as_jsonObject



237
238
239
# File 'lib/code/concerns/shared.rb', line 237

def code_as_json
  as_json.to_code
end

#code_deep_duplicateObject



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

def code_deep_duplicate
  self.class.new(self)
end

#code_different(other) ⇒ Object



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

def code_different(other)
  code_other = other.to_code

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

#code_duplicateObject



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

def code_duplicate
  self.class.new(self)
end

#code_equal_equal(other) ⇒ Object



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

def code_equal_equal(other)
  code_other = other.to_code

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

#code_equal_equal_equal(other) ⇒ Object



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

def code_equal_equal_equal(other)
  code_other = other.to_code

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

#code_exclamation_pointObject



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

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

#code_exclusive_range(value) ⇒ Object



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

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)


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

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

#code_fetchObject



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

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

#code_getObject



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

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

#code_inclusive_range(value) ⇒ Object



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

def code_inclusive_range(value)
  code_value = value.to_code

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

#code_inspectObject



409
410
411
# File 'lib/code/concerns/shared.rb', line 409

def code_inspect
  code_to_string
end

#code_nameObject



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

def code_name
  Object::String.new(name)
end

#code_or(other) ⇒ Object



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

def code_or(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

#code_selfObject



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

def code_self
  self
end

#code_setObject



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

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

#code_to_booleanObject



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

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

#code_to_classObject



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

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

#code_to_dateObject



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

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

#code_to_decimalObject



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

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

#code_to_dictionaryObject



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

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

#code_to_durationObject



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

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

#code_to_integerObject



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

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

#code_to_json(pretty: nil) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/code/concerns/shared.rb', line 229

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



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

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

#code_to_nothingObject



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

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

#code_to_parameterObject



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

def code_to_parameter
  code_to_string.code_parameterize
end

#code_to_rangeObject



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

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

#code_to_stringObject



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

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

#code_to_timeObject



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

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

#code_truthy?Boolean

Returns:

  • (Boolean)


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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  code_other = other.to_code

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

#falsy?Boolean

Returns:

  • (Boolean)


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

def falsy?
  !truthy?
end

#hashObject



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

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

#inspectObject



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

def inspect
  code_inspect.raw
end

#multi_fetch(hash, *keys) ⇒ Object



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

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

#nothing?Boolean

Returns:

  • (Boolean)


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

def nothing?
  false
end

#sig(args) ⇒ Object



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

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

  Object::Nothing.new
end

#succObject



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

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

#to_codeObject



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

def to_code
  self
end

#to_jsonObject



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

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

#to_sObject



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

def to_s
  code_to_string.raw
end

#truthy?Boolean

Returns:

  • (Boolean)


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

def truthy?
  true
end