This project presents a way for Ruby coders to keep method_missing and respond_to_missing? in sync.
The whole idea of finding a way to automagically keep method_missing and respond_to_missing? in sync, was originally inspired by Avdi Grimm's blog post about the need to sync them. I came up with a quick and dirty hack, and a still-hacky improvement that seems to have been mangled by a blog platform change or some such.
Then at RubyConf 2014, Betsy Haibel gave a talk on metaprogramming, including the need. That inspired me to take another whack at it, this time using the different approach shown in this repo (essentially, a decorator class).
I got some suggestions and other help from Chris Hoffman, mainly in figuring out that I shouldn't do the in-block object access the way I was trying to! :-)
This requires the Ruby version to be at least 1.9. Eventually I might put some work into figuring out why many tests fail with 1.8, but for now, call it a Ruby 1.9+ gem. Check its Travis CI status page to see which versions of Ruby it has been tested against; currently that's 1.9.2, 1.9.3, 2.0.0, 2.1.0, 2.1.1, 2.1.2, JRuby in 1.9 mode, and Rubinius 2.1.1.
Add this line to your application's Gemfile:
gem 'hook_lying_syncer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hook_lying_syncer
Create a HookLyingSyncer by passing it something to wrap, a lambda to turn the method name into the subparts of interest, and a block to execute when there's a match.
-
The "something to wrap" can be any object, even a class. Note however that if you wrap a class, that will not affect its instances! You can affect future instances by using a wrapper to override .new, but if you need to affect extant instances, you have to wrap them yourself.
-
The lambda must return an Array with some truthy content (or at least something that responds positively to #any?) if the method name is one you're interested in, and either an empty Array (or at least something that responds negatively to #any?) or something falsey (i.e., false or nil) otherwise. If you are not comfortable making lambdas, feel free to copy the lambda_maker method in the tests.
-
The block will be called when the lambda indicates that a method of interest has been called. The block will receive three arguments: the original object the HookLyingSyncer wrapped, the matches returned by the lambda, and the list of arguments (if any) passed in the method call.
See the tests for examples.
Version | Change |
---|---|
0.0.1 | Initial release |
0.0.2 | Better compatibility w/ 1.9; declare 1.8 incompatible in README |
0.0.3 | Declare 1.8 incompatible in gemspec (duh!) |
0.0.4 | Fix poorly written dependency versions in gemspec |
- Fork it ( https://github.com/davearonson/hook_lying_syncer/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request