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

#methodsObject

Returns the value of attribute methods.



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

def methods
  @methods
end

#rawObject

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



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

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

.code_getObject



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

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

.code_setObject



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

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

Instance Method Details

#<=>(other) ⇒ Object



139
140
141
142
143
# File 'lib/code/concerns/shared.rb', line 139

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

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

#==(other) ⇒ Object



145
146
147
148
149
# File 'lib/code/concerns/shared.rb', line 145

def ==(other)
  code_other = other.to_code

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

#as_jsonObject



233
234
235
# File 'lib/code/concerns/shared.rb', line 233

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
135
136
137
# 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 "methods"
    sig(args)
    code_methods
  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



157
158
159
160
161
# File 'lib/code/concerns/shared.rb', line 157

def code_and(other)
  code_other = other.to_code

  truthy? ? code_other : self
end

#code_as_jsonObject



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

def code_as_json
  as_json.to_code
end

#code_deep_duplicateObject



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

def code_deep_duplicate
  self.class.new(self)
end

#code_different(other) ⇒ Object



163
164
165
166
167
# File 'lib/code/concerns/shared.rb', line 163

def code_different(other)
  code_other = other.to_code

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

#code_duplicateObject



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

def code_duplicate
  self.class.new(self)
end

#code_equal_equal(other) ⇒ Object



169
170
171
172
173
# File 'lib/code/concerns/shared.rb', line 169

def code_equal_equal(other)
  code_other = other.to_code

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

#code_equal_equal_equal(other) ⇒ Object



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

def code_equal_equal_equal(other)
  code_other = other.to_code

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

#code_exclamation_pointObject



175
176
177
# File 'lib/code/concerns/shared.rb', line 175

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

#code_exclusive_range(value) ⇒ Object



179
180
181
182
183
# File 'lib/code/concerns/shared.rb', line 179

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)


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

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

#code_fetchObject



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

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

#code_getObject



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

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

#code_inclusive_range(value) ⇒ Object



185
186
187
188
189
# File 'lib/code/concerns/shared.rb', line 185

def code_inclusive_range(value)
  code_value = value.to_code

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

#code_inspectObject



361
362
363
# File 'lib/code/concerns/shared.rb', line 361

def code_inspect
  code_to_string
end

#code_methodsObject



369
370
371
# File 'lib/code/concerns/shared.rb', line 369

def code_methods
  Object::List.new(methods)
end

#code_nameObject



365
366
367
# File 'lib/code/concerns/shared.rb', line 365

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

#code_or(other) ⇒ Object



191
192
193
194
195
# File 'lib/code/concerns/shared.rb', line 191

def code_or(other)
  code_other = other.to_code

  truthy? ? self : code_other
end

#code_selfObject



197
198
199
# File 'lib/code/concerns/shared.rb', line 197

def code_self
  self
end

#code_setObject



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

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

#code_to_booleanObject



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

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

#code_to_classObject



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

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

#code_to_dateObject



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

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

#code_to_decimalObject



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

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

#code_to_dictionaryObject



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

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

#code_to_durationObject



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

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

#code_to_integerObject



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

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

#code_to_json(pretty: nil) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/code/concerns/shared.rb', line 237

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



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

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

#code_to_nothingObject



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

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

#code_to_parameterObject



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

def code_to_parameter
  code_to_string.code_parameterize
end

#code_to_rangeObject



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

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

#code_to_stringObject



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

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

#code_to_timeObject



357
358
359
# File 'lib/code/concerns/shared.rb', line 357

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

#code_truthy?Boolean

Returns:

  • (Boolean)


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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/code/concerns/shared.rb', line 151

def eql?(other)
  code_other = other.to_code

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

#falsy?Boolean

Returns:

  • (Boolean)


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

def falsy?
  !truthy?
end

#hashObject



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

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

#inspectObject



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

def inspect
  code_inspect.raw
end

#multi_fetch(hash, *keys) ⇒ Object



215
216
217
# File 'lib/code/concerns/shared.rb', line 215

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

#nothing?Boolean

Returns:

  • (Boolean)


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

def nothing?
  false
end

#sig(args, &block) ⇒ Object



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

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

  Object::Nothing.new
end

#succObject



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

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

#to_codeObject



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

def to_code
  self
end

#to_jsonObject



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

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

#to_sObject



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

def to_s
  code_to_string.raw
end

#truthy?Boolean

Returns:

  • (Boolean)


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

def truthy?
  true
end