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:
= In die Zwischenablage kopieren Kopiert!

installieren:
=

Versionen:

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

Runtime Abhängigkeiten (2):

hoe >= 1.8.0
newgem >= 1.2.3

Autoren:

  • Sammy Larbi

SHA 256-Prüfsumme:

e12edf13facbb8c54cf3720c50b9262864a11b0446584291b8561027f46395ca

Downloads insgesamt 6.226

Für diese Version 3.560

Lizenzen:

k.A.

Erforderliche Ruby-Version: >= 0

Links: