AaronLasseigne / active_interaction

:briefcase: Manage application specific business logic.
MIT License
2.07k stars 137 forks source link

Add composable method for using compose like dsl #552

Closed ExplosiveGM closed 1 year ago

ExplosiveGM commented 1 year ago

This code add method composable. It must be rewrited. It created for showing main idea. Sometimes when you have many composable interactors inside main interactor you want push args by name

class SomeInteractor
   integer :a
   integer :b
   string :c
   string :d

  composable(ChildInteractor1, %i[a b])
  composable(ChildInteractor2, %[b c], method: 'custom_child_interactor')
  composable(SomeNamespace::ChildInteractor3, %[c d])
  composable(ChildInteractor4, %i[a b c d current_user])  

  def execute
     child_interactor1
     custom_child_interactor
     some_namespace_child_interactor3
  end

  def current_user
    #SomeCode
  end

It makes execute method clear

AaronLasseigne commented 1 year ago

I'm not clear on how this is helping. What's the advantage over using compose inside of execute?

ExplosiveGM commented 1 year ago

When you write code this way, you declare all interactions that you will use inside this parent interaction. Moreover you dont repeat code in parameters and make execute clear.

class ParentInteraction
  string :arg1
  string :arg2
  string :arg3
  string :arg4
  string :arg5 
  string :arg6
  string :arg7
  string :arg8
  string: arg9  

  composable(ApplyArg123 %i[arg1 arg2 arg3])
  composable(ApplyArg456, %i[arg4 arg5 arg6])
  composable(ApplyArg789, %i[arg7 arg8 arg9]) 

  def execute
    apply_arg123
    apply_arg456
    apply_arg789

    true
  end
end

Now you can compare this code:

class ParentInteraction
  string :arg1
  string :arg2
  string :arg3
  string :arg4
  string :arg5 
  string :arg6
  string :arg7
  string :arg8
  string: arg9 

  def execute
    compose(ApplyArg123, arg1: arg1, arg2: arg2, arg3: arg3])
    compose(ApplyArg456, arg4: arg4, arg5: arg5, arg6: arg6])
    compose(ApplyArg789, arg7: arg7, arg8: arg8, arg9: arg9])

    true
  end
end

If you dont like composable method. You can create something like this

class ParentInteraction
  string :arg1
  string :arg2
  string :arg3
  string :arg4
  string :arg5 
  string :arg6
  string :arg7
  string :arg8
  string: arg9 

  use_parent_inputs_for(ApplyArg123, %i[arg1 arg2 arg3])  
  use_parent_inputs_for(ApplyArg456, %i[arg4 arg5 arg6]) 
  use_parent_inputs_for(ApplyArg789, %i[arg7 arg8 arg9])

  def execute
    #must take params from parent interaction
    compose(ApplyArg123)
    compose(ApplyArg456)
    compose(ApplyArg789)

    true
  end
end

Or you can do something like this

class ParentInteraction
  string :arg1
  string :arg2
  string :arg3
  string :arg4
  string :arg5 
  string :arg6
  string :arg7
  string :arg8
  string: arg9 

  def execute
    #must take params from parent interaction
    compose(ApplyArg123, parent_inputs: true)
    compose(ApplyArg456, parent_inputs: true)
    compose(ApplyArg789, parent_inputs: true)

    true
  end
end

This will compare ParentInteraction and ChildInteraction and pass attributes with same name

I hope you understand the main idea.

ExplosiveGM commented 1 year ago

Okay maybe it is not useful probably I can do something like this

class ParentInteraction
  string :arg1
  string :arg2
  string :arg3
  string :arg4
  string :arg5 
  string :arg6
  string :arg7
  string :arg8
  string: arg9 

  def execute
    #must take params from parent interaction
    compose(ApplyArg123, inputs)
    compose(ApplyArg456, inputs)
    compose(ApplyArg789, inputs)

    true
  end
end