These are chat archives for dry-rb/chat

19th
Aug 2016
Piotr Solnica
@solnic
Aug 19 2016 02:49
What @timriley said :+1:
Nick Sutterer
@apotonick
Aug 19 2016 09:07
@timriley yeah that's funny you said that, because that's what i'm trying to achieve, to have ops "auto_inject-friendly" haha :highfive:
i honestly thought that the container/inject style is what you want to encourage with apps/gems, though. or maybe i was only confused because i read some convos where people used it literally everywhere (which i found a bit ... odd)
Igor
@svilenkov
Aug 19 2016 14:08
Can you have value_to_output => value_to_input mapping when creating an enum
Something like
Types::Strict::Symbol.enum(
     1 => :time_interval,
     2 => :distance_interval,
     4 => :treshold_distance,
     8 => :on_change,
    16 => :total
  )
Sascha Hoellger
@mitnal
Aug 19 2016 15:53

Hi, what’s the best way in dry/transaction to deal with steps that require input from a previous step. Let’s say I have a simplified transaction with multiple steps:

Dry.Transaction(container: some_container) do
  map :build # builds or finds an object and changes some attributes
  try :persist # persists the object AR save!
  tee :send_notification # Send notification email
  tee :track # Log: Object X Created / Updated (if it was a new record or not)
end

I am not sure what’s the best approach here.

  • I could add another transaction but then i would need to know beforehand what I want to track and what the transaction will do in the first step.
  • Another way would be to wrap the object in a struct or something and put the needed information there

What do you guys think is the best way to deal with cases like this?

Piotr Solnica
@solnic
Aug 19 2016 15:56
@mitnal just return what you need and it’ll be passed to the next step
Sascha Hoellger
@mitnal
Aug 19 2016 15:58
@solnic I know. But I always need the object, so I return it. But I can not reconstruct the information from the object alone. I already thought about returning an array: [object, object.new_recrod?]. But I don’t know, it feels a little bit strange.
Ok obviously the example array would be stupid. But I hope you know what I mean :)
Piotr Solnica
@solnic
Aug 19 2016 15:59
why can’t you just do object.new_record? in the step?
Andy Holland
@AMHOL
Aug 19 2016 16:00
@solnic I think that's what he meant by the example array would be stupid
Sascha Hoellger
@mitnal
Aug 19 2016 16:00
@AMHOL yes, thats why the example indeed was really stupid.
Andy Holland
@AMHOL
Aug 19 2016 16:01
What is it exactly you are trying to do?
i.e. what additional info do you need from which step? I assume the persist step
Sascha Hoellger
@mitnal
Aug 19 2016 16:02
Yes, in the example I would need the information if the object was created or just updated
But I only need this information in the last step (track)
I could do something like this in the persist step, but it seems somewhat strange.
is_new_record = object.new_record?
object.save!
[object, is_new_record]
Piotr Solnica
@solnic
Aug 19 2016 16:07
but why not asking that object in the last step - that’s the part I don’t get :)
Andy Holland
@AMHOL
Aug 19 2016 16:07
@solnic new_record will be false after save is called
solnic @solnic shuts up, leaves in shame
Andy Holland
@AMHOL
Aug 19 2016 16:07
lol
Sascha Hoellger
@mitnal
Aug 19 2016 16:07
:)
Andy Holland
@AMHOL
Aug 19 2016 16:08
@mitnal you could decorate it?
Piotr Solnica
@solnic
Aug 19 2016 16:08
I’d pass an array, it’s by design, you pass the data that are needed later
Andy Holland
@AMHOL
Aug 19 2016 16:08
Or use the magical Rails id_was (id_was == id) :trollface:
Piotr Solnica
@solnic
Aug 19 2016 16:08
alternatively you can plug in pub/sub and trigger event
up to you
Andy Holland
@AMHOL
Aug 19 2016 16:08
^^ defo that
Piotr Solnica
@solnic
Aug 19 2016 16:08
I used both approaches, it’s pretty cool
foo_was omg is it a thing?
Andy Holland
@AMHOL
Aug 19 2016 16:09
It actually is
Piotr Solnica
@solnic
Aug 19 2016 16:09
(-‸ლ)
Piotr Solnica
@solnic
Aug 19 2016 16:09
right, now I remember
gah, ok I really gotta focus on dry-v stuff now, ttyl folks :)
Sascha Hoellger
@mitnal
Aug 19 2016 16:10
Ok, I will dig a litte deeper in the pub/sub solution. But I wanted to know if I was missing something and what you guys would recommend in such a case. Thx
Andy Holland
@AMHOL
Aug 19 2016 16:10
:wave:
Piotr Solnica
@solnic
Aug 19 2016 16:10
I recommend either, tbh, you’ll see which one fits better, and passing more than one arg in an array is totally OK, it may feel weird in the beginning but it’s nice, just takes time to get used to it :)
:wave:
Sascha Hoellger
@mitnal
Aug 19 2016 16:11
Ok, thank and :wave:
@AMHOL what exactly did you mean by decorate it. Just wrap it in another object?
Andy Holland
@AMHOL
Aug 19 2016 16:12
If this is the only case, might be overkill to introduce pub/sub, so would go with returning an array/hash/decorated object
Yeah @mitnal, not sure whether there's a nice solution using decorators in this context tho
Sascha Hoellger
@mitnal
Aug 19 2016 16:15
Ok, I think I will start with the array solution and see what happens. The only thing I do not like about this solution is, that I have to pipe the array through some other steps, that do not need this information.
Andy Holland
@AMHOL
Aug 19 2016 16:15
i.e.
class Result < SimpleDelegator
  attr_reader :operation

  def initialize(result, operation)
    super(result)
    @operation = operation
  end
end

Result.new('result', :create)
But yeah, hash/array/dedicated object seems like a better solution here
Sascha Hoellger
@mitnal
Aug 19 2016 16:16
ok, and thanks for your quick help :clap:
Andy Holland
@AMHOL
Aug 19 2016 16:16
I'd probably go with
OpenStruct.new(result: object, operation: :create)
NP
Sascha Hoellger
@mitnal
Aug 19 2016 16:17
I like it :thumbsup:
Piotr Solnica
@solnic
Aug 19 2016 17:38
@fran-worley hola :) I finally started working on the new result AST, you can track progress here: dry-rb/dry-validation#239