ruby - Internal rails notifications -
i'm creating rails app, imports stuff external service. @ end of import, user should info how many new items has been imported (it's periodical check, adds new items local database. not of them each time.). whole process , method-chain quite complex i'm looking best-practice on how pass such information nested method. schema looks more or less that:
some_controller.rb -> model.method() -> lib1.method1() -> lib2.method2() -> lib3.method3() -> lib4.method4() -> lib5.method5() -> items_import_method()
and need somehow pass info how many new items has been imported items_imported_method()
some_controller.rb
(or other place import fired). obvious way of doing passing new_items_count
var way though whole method chain seems bit wrong me. isn't there better way?
i thinking kind of internal events/messages system let me subscribe custom channel, activeresource events maybe there well-known , suggested approach such situation?
thanks in advance.
one way tackle create kind of context container object has properties of steps can manipulate, pass in each method on way down.
for example:
class contextexample attr_accessor :example_field end
in practice:
@context = contextexample.new lib1.method1(@context) @context.example_field # => "test!"
where you're doing is:
module lib1 def self.method1(context) lib2.method2(context) end end module lib2 def self.method2(context) context.example_field = "test!" end end
these modules or classes can save context
if required, , pass on when necessary.
if have well-defined properties need set , manipulate, class attr_accessor
works pretty well. if it's more arbitrary thing , you're not sure might shake out in end, either use hash or openstruct object capture whatever might come up.
Comments
Post a Comment