RubyGems Navigation menu

codeodor-with 0.0.2

I sometimes get a little descriptive with my variable names, so when you're doing a lot of work specifically with one object, it gets especially ugly and repetetive, making the code harder to read than it needs to be: @contract_participants_on_drugs.contract_id = params[:contract_id] @contract_participants_on_drugs.participant_name = params[:participant_name] @contract_participants_on_drugs.drug_conviction = DrugConvictions.find(:wtf => 'this is getting ridiculous') ... And so on. It gets ridiculous. Utility Belt implements a with(object) method via a change to Object: class Object #utility belt implementation def with(object, &block) object.instance_eval &block end end Unfortunately, that just executes the block in the context of the object, so there isn't any crossover, nor can you perform assignments with attr_accessors (that I was able to do, anyway). So, here's With.object() to fill the void. With.object(@foo) do a = "wtf" b = "this is not as bad" end In the above example, @foo.a and @foo.b are the variables getting set. If you prefer, you can require 'with_on_object' instead and use the notation with(object) do ... end. The tests in the /test directory offer more examples of what's been implemented and tested so far (except where noted - namely performing assignment to a variable that was declared outside the block, and is not on @foo). Not everything is working yet, but it works for the simplest, most common cases I've run up against. More complex tests are on the way, along with code to make them pass. Special thanks to Reg Braithwaite, for help and ideas along the way.

Gemfile:
= Copy to clipboard Copied!

install:
=

Versions:

  1. 0.0.2 - August 11, 2014 (8.5 KB)
  2. 0.0.1 - August 11, 2014 (9 KB)

Runtime Dependencies (2):

hoe >= 1.8.0
newgem >= 1.2.3

Authors:

  • Sammy Larbi

SHA 256 checksum:

e12edf13facbb8c54cf3720c50b9262864a11b0446584291b8561027f46395ca

Total downloads 6,228

For this version 3,561

Licenses:

N/A

Required Ruby Version: >= 0

Links: