monkeypatching

MonkeyPatching ActiveJobs

泄露秘密 提交于 2019-12-09 18:58:19
问题 I am having an issue monkey-patching part of ActiveJobs. I have the following code in config/initializers/extensions/arguements.rb module ActiveJob module Arguments TYPE_WHITELIST = [ Date, DateTime, Time, NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ] end end Basically, I am trying to add basic support for Date/Time objects for use in the ActiveJob created by ActionMailer#deliver_later Upon loading the rails app I can see my whitelist is loaded, however when I call the

Is there any way to monkey-patch or swizzle an NSArray (or other Class Cluster)?

佐手、 提交于 2019-12-09 07:12:59
问题 Today I was working on a project in which I wanted to "alias" an alternative method for all instances of NSArray , and didn't think it would be too difficult with some good old-fashioned method swizzling. I broke out JRSwizzle and… [NSArray jr_swizzleMethod:@selector(objectAtIndex:) withMethod:@selector(objectAtIndex_accordingToMe:) error:nil]; To be clear, I paired this with the appropriate category on NSArray , an instance method called objectAtIndex_accordingToMe: . However, I was just

Looking for a concrete example of what's wrong with monkey patching?

南楼画角 提交于 2019-12-08 11:18:27
I've heard a lot of oohs and aahs about how monkey patching is so cool or monkey patching is the worst thing on earth. I have the idea that it's not good if you write some code and then someone else changes its functionality to do something else. But I'm looking for a concrete example of a situation where this could really hurt you. I mean, I write code all the time and then my coworkers make changes to it. So, how is monkey patching any different from that? What's the worst that could happen ? Programming has had a slow but steady movement away from coding practices that require understanding

How can I monkey-patch a decorator in Django's models while testing?

可紊 提交于 2019-12-08 07:04:56
问题 I have a @memoize decorator in my models, which caches some details on the model itself, to avoid multiple database calls when called many times (especially in templates). However, since I store the objects and refer to them in tests, this breaks things. For example, if I do mygroup.subscribers , add a subscriber and try it again, it will return an incorrect number of subscribers, since it's been memoized. How can I monkey-patch that decorator to do nothing from my tests.py? I haven't found a

Monkey patching ActiveResource::Errors

时光怂恿深爱的人放手 提交于 2019-12-08 06:37:30
I've come across an issue with ActiveResource that has been resolved and was trying to monkey patch it into my application without much luck. I've added a file in config/initializers/ containing the following: class ActiveResource::Errors < ActiveModel::Errors # https://github.com/rails/rails/commit/b09b2a8401c18d1efff21b3919ac280470a6eb8b def from_hash(messages, save_cache = false) clear unless save_cache messages.each do |(key,errors)| errors.each do |error| if @base.attributes.keys.include?(key) add key, error elsif key == 'base' self[:base] << error else # reporting an error on an

Looking for a concrete example of what's wrong with monkey patching?

女生的网名这么多〃 提交于 2019-12-08 04:18:07
问题 I've heard a lot of oohs and aahs about how monkey patching is so cool or monkey patching is the worst thing on earth. I have the idea that it's not good if you write some code and then someone else changes its functionality to do something else. But I'm looking for a concrete example of a situation where this could really hurt you. I mean, I write code all the time and then my coworkers make changes to it. So, how is monkey patching any different from that? What's the worst that could happen

Use zone.js to detect current execution context from anywhere?

你。 提交于 2019-12-08 04:03:53
问题 With zone.js is it possible to determine the current execution context from anywhere ? Ie., if a zone-bound function calls another function which calls setTimeout(myFn) can I determine the current execution context from within myFn() ? If so, please provide a simple example of how to do so. 回答1: Every time you fork() a zone , accessing the zone object within that zone 's context will always return the forked zone . The following experiment demonstrates this behavior: var b = function() {

How can I monkey-patch a decorator in Django's models while testing?

霸气de小男生 提交于 2019-12-07 15:25:26
I have a @memoize decorator in my models, which caches some details on the model itself, to avoid multiple database calls when called many times (especially in templates). However, since I store the objects and refer to them in tests, this breaks things. For example, if I do mygroup.subscribers , add a subscriber and try it again, it will return an incorrect number of subscribers, since it's been memoized. How can I monkey-patch that decorator to do nothing from my tests.py? I haven't found a way to do it cleanly, since models get loaded first. Thierry Lam At the beginning of memoize

How to monkeypatch a static method? [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-07 01:31:02
问题 This question already has answers here : Pointers to static methods in Python (3 answers) Closed 6 years ago . While it's fairly simple to monkeypatch instance methods to classes, e.g. class A(object): pass def a(self): print "a" A.a = a doing this with another class's @staticmethod à la class B(object): @staticmethod def b(): print "static b" A.b = B.b results in A.b() yielding a TypeError : unbound method b() must be called with A instance as first argument (got nothing instead) 回答1: Make A

PowerShell, Extension Methods, and Monkey Patching

泪湿孤枕 提交于 2019-12-06 22:20:12
问题 Is it possible to write extension method in PowerShell? or to bolt a new method on top of an existing type like [string] live at runtime? 回答1: I don't know of a way to patch a type with an extension method. But it's certainly possible to patch an object via the add-member cmdlet PS> $a = "foo" PS> $a = add-member -in $a -memberType ScriptMethod -name Bar -value { $this + "bar" } -passthru PS> $a.Foo() foobar EDIT Explain the completely and totally readable PowerShell Syntax :) I love