Module: Code::Concerns::Shared
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #as_json ⇒ Object
- #call(**args) ⇒ Object
- #code_and(other) ⇒ Object
- #code_as_json ⇒ Object
- #code_deep_duplicate ⇒ Object
- #code_different(other) ⇒ Object
- #code_duplicate ⇒ Object
- #code_equal_equal(other) ⇒ Object
- #code_equal_equal_equal(other) ⇒ Object
- #code_exclamation_point ⇒ Object
- #code_exclusive_range(value) ⇒ Object
- #code_falsy? ⇒ Boolean
- #code_fetch ⇒ Object
- #code_get ⇒ Object
- #code_inclusive_range(value) ⇒ Object
- #code_inspect ⇒ Object
- #code_name ⇒ Object
- #code_or(other) ⇒ Object
- #code_self ⇒ Object
- #code_set ⇒ Object
- #code_to_boolean ⇒ Object
- #code_to_class ⇒ Object
- #code_to_date ⇒ Object
- #code_to_decimal ⇒ Object
- #code_to_dictionary ⇒ Object
- #code_to_duration ⇒ Object
- #code_to_integer ⇒ Object
- #code_to_json(pretty: nil) ⇒ Object
- #code_to_list ⇒ Object
- #code_to_nothing ⇒ Object
- #code_to_parameter ⇒ Object
- #code_to_range ⇒ Object
- #code_to_string ⇒ Object
- #code_to_time ⇒ Object
- #code_truthy? ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #falsy? ⇒ Boolean
- #hash ⇒ Object
- #inspect ⇒ Object
- #multi_fetch(hash, *keys) ⇒ Object
- #nothing? ⇒ Boolean
- #sig(args) ⇒ Object
- #succ ⇒ Object
- #to_code ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
- #truthy? ⇒ Boolean
Instance Attribute Details
#raw ⇒ Object (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_fetch ⇒ Object
257 258 259 |
# File 'lib/code/concerns/shared.rb', line 257 def self.code_fetch(...) Object::Nothing.new end |
.code_get ⇒ Object
265 266 267 |
# File 'lib/code/concerns/shared.rb', line 265 def self.code_get(...) Object::Nothing.new end |
.code_set ⇒ Object
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_json ⇒ Object
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_json ⇒ Object
237 238 239 |
# File 'lib/code/concerns/shared.rb', line 237 def code_as_json as_json.to_code end |
#code_deep_duplicate ⇒ Object
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_duplicate ⇒ Object
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_point ⇒ Object
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
285 286 287 |
# File 'lib/code/concerns/shared.rb', line 285 def code_falsy? Object::Boolean.new(falsy?) end |
#code_fetch ⇒ Object
269 270 271 |
# File 'lib/code/concerns/shared.rb', line 269 def code_fetch(...) Object::Nothing.new end |
#code_get ⇒ Object
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_inspect ⇒ Object
409 410 411 |
# File 'lib/code/concerns/shared.rb', line 409 def code_inspect code_to_string end |
#code_name ⇒ Object
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_self ⇒ Object
189 190 191 |
# File 'lib/code/concerns/shared.rb', line 189 def code_self self end |
#code_set ⇒ Object
273 274 275 |
# File 'lib/code/concerns/shared.rb', line 273 def code_set(...) Object::Nothing.new end |
#code_to_boolean ⇒ Object
293 294 295 |
# File 'lib/code/concerns/shared.rb', line 293 def code_to_boolean Object::Boolean.new(self) end |
#code_to_class ⇒ Object
297 298 299 |
# File 'lib/code/concerns/shared.rb', line 297 def code_to_class Object::Class.new(self) end |
#code_to_date ⇒ Object
301 302 303 |
# File 'lib/code/concerns/shared.rb', line 301 def code_to_date Object::Date.new(self) end |
#code_to_decimal ⇒ Object
305 306 307 |
# File 'lib/code/concerns/shared.rb', line 305 def code_to_decimal Object::Decimal.new(self) end |
#code_to_dictionary ⇒ Object
309 310 311 |
# File 'lib/code/concerns/shared.rb', line 309 def code_to_dictionary Object::Dictionary.new(self) end |
#code_to_duration ⇒ Object
313 314 315 |
# File 'lib/code/concerns/shared.rb', line 313 def code_to_duration Object::Duration.new(self) end |
#code_to_integer ⇒ Object
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_list ⇒ Object
321 322 323 |
# File 'lib/code/concerns/shared.rb', line 321 def code_to_list Object::List.new(self) end |
#code_to_nothing ⇒ Object
325 326 327 |
# File 'lib/code/concerns/shared.rb', line 325 def code_to_nothing Object::Nothing.new(self) end |
#code_to_parameter ⇒ Object
281 282 283 |
# File 'lib/code/concerns/shared.rb', line 281 def code_to_parameter code_to_string.code_parameterize end |
#code_to_range ⇒ Object
329 330 331 |
# File 'lib/code/concerns/shared.rb', line 329 def code_to_range Object::Range.new(self) end |
#code_to_string ⇒ Object
333 334 335 |
# File 'lib/code/concerns/shared.rb', line 333 def code_to_string Object::String.new(self) end |
#code_to_time ⇒ Object
337 338 339 |
# File 'lib/code/concerns/shared.rb', line 337 def code_to_time Object::Time.new(self) end |
#code_truthy? ⇒ Boolean
289 290 291 |
# File 'lib/code/concerns/shared.rb', line 289 def code_truthy? Object::Boolean.new(truthy?) end |
#eql?(other) ⇒ 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
199 200 201 |
# File 'lib/code/concerns/shared.rb', line 199 def falsy? !truthy? end |
#hash ⇒ Object
203 204 205 |
# File 'lib/code/concerns/shared.rb', line 203 def hash [self.class, raw].hash end |
#inspect ⇒ Object
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
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 |
#succ ⇒ Object
245 246 247 |
# File 'lib/code/concerns/shared.rb', line 245 def succ self.class.new(raw.succ) end |
#to_code ⇒ Object
241 242 243 |
# File 'lib/code/concerns/shared.rb', line 241 def to_code self end |
#to_json ⇒ Object
221 222 223 |
# File 'lib/code/concerns/shared.rb', line 221 def to_json(...) as_json(...).to_json(...) end |
#to_s ⇒ Object
341 342 343 |
# File 'lib/code/concerns/shared.rb', line 341 def to_s code_to_string.raw end |
#truthy? ⇒ Boolean
217 218 219 |
# File 'lib/code/concerns/shared.rb', line 217 def truthy? true end |