ruby on rails - Changing an instance variable within dynamic method (dynamic resource using ActiveResource) -


i using dynamic dispatch define several class methods in class inherits activeresource.

class invoice < activeresource::base   self.site = 'http://localhost:8080/'    def self.define(method)     define_singleton_method(method) |params = {}|       site = self.site       self.site = "#{self.site}/invoice/#{method.to_s.camelize(:lower)}"        puts "self.site -> #{self.site}"         results = invoice.all(params: params)        self.site = site       puts "changing self.site #{site}"             return results     end   end    define :get_details   define :put_approval   define :get_attachment   define :get_pending_notifications end 

this works great first call, whatever (invoice.get_details, invoice.get_pending_notifications...), fails on second call.

i understand why happening , can fixed.

most site not getting reset properly. try this, works under ruby console:

class invoice < activeresource::base   self.site = 'http://localhost:8080/'    def self.define(method)     define_singleton_method(method) |params = {}|       site = self.site       begin         self.site = "#{self.site}/invoice/#{method.to_s.camelize(:lower)}"         invoice.all(params: params)       ensure         # code run, if there exception above         # , won't affect original value returned above         self.site = site       end     end   end    define :get_details   define :put_approval   define :get_attachment   define :get_pending_notifications end 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -