Module: Kumi::Core::RubyParser::Sugar::ProxyRefinement

Included in:
DeclarationReferenceProxy, InputFieldProxy
Defined in:
lib/kumi/core/ruby_parser/sugar.rb

Overview

Shared refinement for proxy objects that need to handle operators Both DeclarationReferenceProxy and InputFieldProxy can use this

Class Method Summary collapse

Class Method Details

.extended(proxy_class) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/kumi/core/ruby_parser/sugar.rb', line 276

def self.extended(proxy_class)
  # Add operator methods directly to the proxy class
  proxy_class.class_eval do
    # Arithmetic operations
    ARITHMETIC_OPS.each do |op, op_name|
      define_method(op) do |other|
        ast_node = to_ast_node
        other_node = Sugar.ensure_literal(other)
        Sugar.create_call_expression(op_name, [ast_node, other_node])
      end
    end

    # Comparison operations (including == and != that don't work with refinements)
    COMPARISON_OPS.each do |op|
      define_method(op) do |other|
        ast_node = to_ast_node
        other_node = Sugar.ensure_literal(other)
        Sugar.create_call_expression(op, [ast_node, other_node])
      end
    end

    # Logical operations
    define_method(:&) do |other|
      ast_node = to_ast_node
      other_node = Sugar.ensure_literal(other)
      Sugar.create_call_expression(:and, [ast_node, other_node])
    end

    define_method(:|) do |other|
      ast_node = to_ast_node
      other_node = Sugar.ensure_literal(other)
      Sugar.create_call_expression(:or, [ast_node, other_node])
    end

    # Array access
    define_method(:[]) do |index|
      ast_node = to_ast_node
      Sugar.create_call_expression(:at, [ast_node, Sugar.ensure_literal(index)])
    end

    # Unary minus
    define_method(:-@) do
      ast_node = to_ast_node
      Sugar.create_call_expression(:subtract, [Sugar.ensure_literal(0), ast_node])
    end

    # Override Ruby's built-in nil? method to transform into == nil
    define_method(:nil?) do
      ast_node = to_ast_node
      nil_literal = Kumi::Syntax::Literal.new(nil)
      Sugar.create_call_expression(:==, [ast_node, nil_literal])
    end
  end
end