1

I'm using a gem called active-interaction.

I've got an interaction that inherits ActiveInteraction with filters:

module Api
 module V2
  class MyInteraction < ActiveInteraction::Base
   
   string :sort_string
   array :ids, default: []

   # Here I want to have a third parameter, and for it to be either 
   # An integer, a string or a float.

I'm trying to find a way to have a parameter that can accept 2 or more types / a dynamic "any" type.

Any suggestions?

Thanks.

Aviv Day
  • 428
  • 4
  • 13
Amit
  • 155
  • 1
  • 1
  • 15

1 Answers1

1

You're looking for a concept called: "overloading", I'm not so familiar with the active-interaction gem, but in statically typed languages such as C#, Java, etc... You can use overloading.

How can it help? For example, if you have a route called "send-details" that can accept a string/int. You can do something like (C# for the example):

[HttpPost]
public void doSomething(String param)
{
    BL.doSomething()
}

[HttpPost]
public void doSomething(int param)
{
   BL.doSomething()
}

Now, we overloaded the doSomething function and the route will match the correct one, keeping it DRY and OOP friendly.

From the docs, it looks very weird the way active-interaction behaves, because ruby is a dynamically-typed language, and active-interaction uses a filter to make it statically typed. I believe your best option will be:

Move all BL to a separate Service, and create 2 interactions with different params (the signature) => in both just call the newly created service.

Yes, it's a lot of code, but I believe it will be your best resolution for the situation.

On a personal note, I would not use active interaction for production products due to lack of community and the fact it goes against ruby standards.

you can read more about overloading here: https://en.wikipedia.org/wiki/Function_overloading

Aviv Day
  • 428
  • 4
  • 13
  • I know overloading... But how do you implement it in active interaction? – Amit Jul 17 '22 at 10:07
  • Active interaction, break standards as I said, but a good option would be, to change the key and value to a hash, and then it will act like ruby dynamically typed. This is obviously a monkey patch, so the main option is just not using active-interaction / creating multiple classes for "overloading". This is a concept in static vs dynamically typed languages, I suggest reading: https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages – Aviv Day Jul 17 '22 at 10:08