dry

JUnit parallel to Rails Fixtures?

My team has a set of POJO's that we build up to pass into our code and test various functions. For example we have an Address class which contains address information. It doesn't make sense to constantly rebuild this class every time we have to whack an address onto an object to test something. I am thinking that something like Rails'...

Rails: keeping DRY with ActiveRecord models that share similar complex attributes

This seems like it should have a straightforward answer, but after much time on Google and SO I can't find it. It might be a case of missing the right keywords. In my RoR application I have several models that share a specific kind of string attribute that has special validation and other functionality. The closest similar example I can...

How can my-program.hs get its version number from my-program.cabal at build time?

I would like my cabalised program to have a --version switch. I would like it to report the same version as is present in the .cabal file. If I have to update the version number separately in my Haskell source code as well as in the .cabal file, I will eventually get them out of sync. So, how can my program, while being compiled under...

DRYing out implementation of ICloneable in several classes

I have several different classes that I want to be cloneable: GenericRow, GenericRows, ParticularRow, and ParticularRows. There is the following class hierarchy: GenericRow is the parent of ParticularRow, and GenericRows is the parent of ParticularRows. Each class implements ICloneable because I want to be able to create deep copies ...

More elegant way to make a C++ member function change different member variables based on template parameter?

Today, I wrote some code that needed to add elements to different container variables depending on the type of a template parameter. I solved it by writing a friend helper class specialized on its own template parameter which had a member variable of the original class. It saved me a few hundred lines of repeating myself without adding...

Can I make a clojure macro that will allow me to get a list of all functions created by the macro?

I would like to have a macro which I'll call def-foo. Def-foo will create a function, and then will add this function to a set. So I could call (def-foo bar ...) (def-foo baz ...) And then there would be some set, e.g. all-foos, which I could call: all-foos => #{bar, baz} Essentially, I'm just trying to avoid repeating myself. ...

How to refactor this Ruby on Rails code?

I want to fetch posts based on their status, so I have this code inside my PostsController index action. It seems to be cluttering the index action, though, and I'm not sure it belongs here. How could I make it more concise and where would I move it in my application so it doesn't clutter up my index action (if that is the correct thing...

using yield in C# like I would in Ruby

Besides just using yield for iterators in Ruby, I also use it to pass control briefly back to the caller before resuming control in the called method. What I want to do in C# is similar. In a test class, I want to get a connection instance, create another variable instance that uses that connection, then pass the variable to the callin...

C# lambda contents won't happen until it's called, right? Also, code cleanup

I have the following methods: protected static void updateExistingSection(XmlDocument doc, XmlNode rootNode, string sectionTag, CreateSection createSection, Func<XmlNode[]> createChildNodes, Action<XmlNode> firstSection) { IEnumerable<XmlNode> sections = getChildNodesByName(rootNode, sectionTag); if (sections.Cou...

Can I avoid repeating myself in this situation (Java)

if (openFile == null) { new AppFileDialog().chooseFile("Save", appFrame); } if (openFile == null) { return; } Here I need to check to see if the user has already chosen a file. If not, they are given a prompt to. If the file is still null, the function returns without saving. The problem is the two identical if sta...

How do I initialize attributes when I instantiate objects in Rails?

Clients have many Invoices. Invoices have a number attribute that I want to initialize by incrementing the client's previous invoice number. For example: @client = Client.find(1) @client.last_invoice_number > 14 @invoice = @client.invoices.build @invoice.number > 15 I want to get this functionality into my Invoice model, but I'm not ...

Automark model names/attributes for translation

Is there any way one could automatically mark all model names and attributes for translation, without specifying verbose_name/_plural on each one of them? Doesn't feel very DRY to do this every time: class Profile(models.Model): length = models.IntegerField(_('length')) weight = models.IntegerField(_('weight')) favorite_mov...

DRYing ASP.NET MVC controllers and views

How do I share code among controllers of different models. In a 'super' controller action, I want to be able to figure out which controller (route) was called, and using that route name load the corresponding model, and use that model to query the database to pass to the view. I can easily do this in Ruby on Rails, does MVC allow such ...

Ruby: Multiply many variables with the same name

Hi. I'm new to ruby. Is there any way how can I shorten this code? Thanks plans.each do |plan| total = plan.landline.to_f * @landline.to_f total += plan.vpn.to_f * @vpn.to_f total += plan.other_networks.to_f * @other_networks.to_f total += plan.gprs.to_f * @gprs.to_f total += plan.sms.to_f * @sms.to_f total += plan.mms.to_...

How can you be DRY with a programming language that doesn't have Reflection?

Any programming language that does not have a suitable reflection mechanism I find seriously debilitating for rapidly changing problems. It seems with certain languages its incredible hard or not possible to do: Convention over Configuration Automatic Databinding AOP / Meta programming with out reflection. Some example languages th...

How to layout all children of a wxPanel?

I have a wxPython application which allows the users to select items from menus that then change what is visible on the screen. This often requires a recalculation of the layout of panels. I'd like to be able to call the layout of all the children of a panel (and the children of those children) in reverse order. That is, the items wit...

Can this query be any more DRY?

I've just checked out the ON DUPLICATE KEY UPDATE for MySQL. Here is an example query. $query = 'INSERT INTO `activities` (`id`, `hole_id`, `name_id`, `start_depth`, `end_depth`, `start_time`, `end_time` ...

[Eclipse] Creating code template that evaluates newly created var

I am writing a lot of unit tests these days. And I want to minimize the amount of typing I have to do. So I have created several Eclipse coding templates. Most of them work fine. But, recently I want to do a bit more advanced stuff. I use EasyMock and when writing expectations , I find myself writing stuff over and over again. I want...

How to set JQuery .show / .hide without repeating Div selectors.

I want to pass this function a True or a False and have the elements listed show (true) or hide (false) on this input. I am currently using this function... function SetElementVisibility(visible) { if (visible) { $("#Div1").show("slow"); $("#Div2").show("slow"); $("#Div3").show("slow"...

DRY programming with jquery

Posting as a jquery newbie. I am sure there must be a way to condense the code below in a DRY fashion. Essentially this is just a show/hide that is applied to multiple elements on a page, all using the same template and naming conventions: $("#homelink1").hover( function() { $("#poptext1").show(); }, function() { $("#poptext1")....