monkeypatching - Ruby using prepend to patch a class doesn't work -
i'm trying patch verb methods in sinatra add function call before it. having problems in using alias chain keep original methods, discovered prepend allow me want without using such hacky method.
however prepended functions not being called , being ignored. what's going on?
here's patch:
if defined? sinatra::base module restman module patches module sinatra_base_patch [:get, :post, :put, :delete, :head, :options, :patch, :link, :unlink].each |func_name| define_method func_name |*args,&block| if args.first.class == symbol super(restman::routes.get(args.first)[:uri],*block) else super(*args,*block) end end end end end end ::sinatra::base.prepend restman::patches::sinatra_base_patch end
edit:(explanation)
the patch pretty simple, overrides sinatra's normal http verb methods , checks if symbol passed or not, if 1 passes symbol method returns mapping , takes url out of mapping , passes sinatra's normal http verb methods.
this can have:
restman::routes.define map :root, to: '/' end
and do
get :root 'hello world!' end
i thinking of trying refinements, might work better.. maybe?
the get
, post
, put
etc. methods in sinatra class methods, creating instance methods names. in order intercept methods need prepend sinatra::base
singleton class.
try this:
::sinatra::base.singleton_class.prepend restman::patches::sinatra_base_patch
Comments
Post a Comment