dry

How can I convert this code to meta-programming, so I can stop duplicating it?

I've got a small but growing framework for building .net systems with ruby / rake , that I've been working on for a while now. In this code base, I have the following: require 'rake/tasklib' def assemblyinfo(name=:assemblyinfo, *args, Albacore::AlbacoreTask def execute(name) asm = AssemblyInfo.new asm.load_config_by_ta...

CakePHP: Execute code before some model methods

Right now I have several methods in my model which all fetch the same object at their beginning (the model's parent class). I would like to do this automatically and execute some code beforehand. I would like to say "execute fetchParent() before you call the methods getParentId(), getParentTable() and mayChange()". It it not sufficient...

Polymorphic has_many through Controllers: Antipattern?

I'm tempted to say yes. A contrived example, using has_many :through and polymorphs: class Person < ActiveRecord::Base has_many :clubs, :through => :memberships has_many :gyms, :through => :memberships end class Membership < ActiveRecord::Base belongs_to :member, :polymorphic => true end class Club < ActiveRecord::Base has_ma...

Is using DTO's and Entities breach of DRY principle ?

I was looking at a library called Automapper. I am having a few concerns with this: We dont want to expose our data model (GOOD). Why should the datamodel closely resemble your DB? using lightweight DTOs instead of your entities. (GOOD) Now I need to map my entities to these DTOs. Am i respecting the DRY principle?? ...

PHP: Call a method on a returned class

I have a method which returns a class and want to call a method on it. Instead of $theClass = $this->getClass(); $theClass->foo(); I would like to write $this->getClass()->foo(); Is there a syntax for this as of PHP4? This works: $this->{$this->getClassName()}->foo(); But I would like to manipulate the class beforehand (I do th...

DRYing c++ structure

I have a simple c++ struct that is extensively used in a program. Now I wish to persist the structure in a sqlite database as individual fields (iow not as a blob). What good ways are there to map the attributes of the struct to database columns? ...

How much duplicated code do you tolerate?

In a recent code review I spotted a few lines of duplicated logic in a class (less than 15 lines). When I suggested that the author refactor the code, he argued that the code is simpler to understand that way. After reading the code again, I have to agree extracting the duplicated logic would hurt readability a little. I know DRY is gu...

Keeping SQL Dry

Here is a MySQL query I'm running: -- get the sid of every supplier who does not supply both a red and green part SELECT Suppliers.sid, Parts.color FROM Suppliers JOIN Catalog ON Catalog.sid = Suppliers.sid JOIN Parts ON Parts.pid = Catalog.pid WHERE Suppliers.sid NOT IN ( SELECT Suppliers.sid FROM Suppliers JOIN Catalog ON ...

Eliminate repetition in C++ code?

Given the following: StreamLogger& operator<<(const char* s) { elements.push_back(String(s)); return *this; } StreamLogger& operator<<(int val) { elements.push_back(String(asString<int>(val))); return *this; } StreamLogger& operator<<(unsigned val) { elements.push_back(String(asString<unsigned>(val))); return *this; } Str...

MySQL: How can I condense this verbose query?

Here is my schema: Suppliers(sid: integer, sname: string, address string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) Primary keys in bold. Here is the MySQL query I'm working with: -- Find the sids of suppliers who supply every red part or supply every green part. -- this isn't...

How can I use the same tokenizer across models with validates_length_of?

I have the following validation in a model: validates_length_of :description, :minimum => 2, :on => :save, :message => "must be at least 2 words", :tokenizer => lambda {|text| text.scan(/\w+/)} And this works fine. When I add a second field to the model that needs to be validated by number of words, I declare tokenize_by_words ...

C++: Avoiding dual maintenance in inheritance hierarchies

When creating a C++ inheritance structure, you have to define member functions exactly the same in multiple places: If B is an abstract base class, and D, E, and F all inherit from B, you might have this: class B { virtual func A( ... params ) = 0; }; class D : public B { func A( ... params ); }; /* ... etc... similar implement...

Google App Engine: How can I DRY this basic request handling?

I'm just getting started with Google App Engine. Currently, my app has two pages: one lists all the inventory currently in stock, and the other is a detail page for a given item. I feel that my coding could be much more DRY. (I'm making all the calls to print headers and footers twice, for instance.) Here is the code. How can I factor o...

Where to put partials shared by the whole application in Rails?

Where would I go about placing partial files shared by more than one model? I have a page called crop.html.erb that is used for one model - Photo. Now I would like to use it for another model called User as well. I could copy and paste the code but that's not very DRY, so I figured I would move it into a partial. Since it's shared betw...

How to implement or emulate an "abstract" OCUnit test class?

I have a number of Objective-C classes organized in an inheritance hierarchy. They all share a common parent which implements all the behaviors shared among the children. Each child class defines a few methods that make it work, and the parent class raises an exception for the methods designed to be implemented/overridden by its children...

ASP.NET: Reusing the same Repeater ItemTemplate

I'm currently using a certain ItemTemplate for three repeaters that are all on the same page, but they are all bound to different data sources. Is there any way to refactor my web form without using a user control to easily refer to this template so I will only have to define it once? ...

Ruby Actions: How to avoid a bunch of returns to halt execution?

How can I DRY the code below? Do I have to setup a bunch of ELSEs ? I usually find the "if this is met, stop", "if this is met, stop", rather than a bunch of nested ifs. I discovered that redirect_to and render don't stop the action execution... def payment_confirmed confirm_payment do |confirmation| @purchase = Purchase....

Ruby Design Problem for SQL Bulk Inserter

This is a Ruby design problem. How can I make a reusable flat file parser that can perform different data scrubbing operations per call, return the emitted results from each scrubbing operation to the caller and perform bulk SQL insertions? Now, before anyone gets narky/concerned, I have written this code already in a very unDRY fashion...

What's the best platform for a static-website?

Hello, I am building a static-website (as in, to change a page, we change the HTML and there is no DB or anything). Well, it will have a number of pages and I don't want to copy and paste the HTML navigation and layout code around everywhere. So what would be the best platform to use in this situation so I can have all my layout and "co...

rails + rspec : Staying DRY when testing validations

Ok say I have the following model: class Country < ActiveRecord::Base validates_presence_of :name validates_presence_of :code end I'm doing the rspec unit tests for those validations. They look like this: it "should be invalid without a name" do country = Country.new(@valid_attributes.except(:name)) country.should_not b...