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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/kumi/core/ruby_parser/sugar.rb', line 190

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