sequent icon indicating copy to clipboard operation
sequent copied to clipboard

[feature] define after_initialize callback in sequent command

Open crypto-zhiming opened this issue 4 years ago • 2 comments

Define callback after_initialize in Sequent::Command. We can use this callback to do more things, such as set default value for command attributes.

class AfterInitCommand < Sequent::Command
  attrs flag: Boolean

  after_initialize do
    @flag = true if @flag.nil?
  end
end

crypto-zhiming avatar Feb 11 '22 08:02 crypto-zhiming

I'm not sure if this is the way to go for 'default' values.

We could also introduce the following:

MyCommand < Sequent::Command
  attr flag: Boolean, default: true
end

If you want this with attrs as well, we could try this:

MyCommand < Sequent::Command
  attrs flag: opts(type: Boolean, default: true), other_flag: Boolean
end

bforma avatar Apr 19 '22 07:04 bforma

I'm not sure if this is the way to go for 'default' values.

We could also introduce the following:

MyCommand < Sequent::Command
  attr flag: Boolean, default: true
end

I think after_initialize is just another active model callback we can support (like we already do with validations). Using after_initialize also has access to other attributes in the object.

But we could also implement the default suggestion, that is probably more elegant to set default values.

lvonk avatar Apr 19 '22 08:04 lvonk