mardi 26 décembre 2017

Chain of methods in rails ( call methods by step)

I try to find how to best way implement chain of methods in Ruby( Rails application) like this

class Foo
  attr_accessor :errors
  attr_accessor :shared_params

  def initialize(..)
    ..
  end

  def call 
    check_params
    calc_smthing
    write_in_db
  end

  prviate

  def check_params
    ..
  end

  def calc_smthing
    ..
  end

  def write_in_db
    ..
  end
end

main ideas:

  • if something fail( or not fail but return false) in some step others steps doesnt call, of course I dont want to add multiple ifs to check state and think about how implement it in one place

  • I need to save errors

  • It would be great to find way to share params between methods.

I can write in simple( ugly) way with ifs but I try to find more elegant way or pattern because problem not so specific I think.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire