dry

Using parameter to control Rails validations

I have an Event model with a finish_time field and a form checkbox called whenever. When whenever is checked I want to set finish_time to nil regardless of its value in parameters, when whenever is not checked I want to make sure that finish_time is a valid date before I save it in the database. Currently I am achieving this using a w...

Looping javascript get requests

I've got a javascript 101 question. I'm using a nice graphing library (flot), and plotting multiple graphs on a single page. Each graph is a div, with a attribute set that tells the graph where to grab it's data from. For example, the HTML might be: <div class="line-chart" data-src='/revenue.js'></div> <div class="scatter-chart" data...

Improving MVC usage

I'm busy building an MVC application in PHP using Kohana MVC framework, and it works very well. But there are some small annoyances which I'd like to address. A lot of logic is repeated across actions in controllers and controllers themselves. I've been thinking about it, and I thought it would be smart to define an object which contain...

Is Form validation and Business validation too much?

I've got this question about form validation and business validation. I see a lot of frameworks that use some sort of form validation library. You submit some values and the library validates the values from the form. If not ok it will show some errors on you screen. If all goes to plan the values will be set into domain objects. Here th...

Use the same parameters many times in a find conditions: hash

Hello, I have a model who holds 2 properties: valid_from and valid_to. I need to select all instances that are currently valid, i.e. valid_from <= today and valid_to >= today. i have the following find : Mymodel.find(:all, :conditions => ["valid_from <= ? and valid_to >= ?", Date.today, Date.today]) I already thought about storing D...

ASP.NET MVC - How to achieve reusable user controls and maintain DRY?

First post so please be gentle :) When creating user controls in ASP.NET MVC, what is the best way to structure the code so that the controllers that invoke views that use the user controls do not all have to know so much about the controls? I would like to know a good way to maintain DRY while using user controls in ASP.NET MVC. Pleas...

CakePHP: AppController to access model of derivated Controller

I want to implement shared "add" actions in the AppController. For this, I need to access the appropriate model of the derivated controller. How do I do this? ...

i violated D.R.Y. help me please?

I'm making a blackjack sim and I want to deal the cards how it would be in a casino, i.e. all players get dealt a card, dealer gets one face down, players get another card, dealer gets one face up BUT LOOK I VIOLATED DRY :( How to redo?? void BlackJack::newHand() { resetHands(); for (unsigned int i = 0; i < players.size(); i...

Making a reusable predicate for EntitySet<T>, IQueryable<T> and IEnumerable<T>

In my LINQ to SQL setup I have various tables which are mapped to classes which basically support the same interface to support versioning, i.e. public interface IValid { int? validTo { get; } int validFrom { get; } } The LINQ to SQL classes derive from this interface like this: public partial class representationRevision : I...

Getting DRY with Rhino Mocks

I am looking for ways of making the following more concise. public class MyTests { IPresenter presenter; [SetUp] public void SetUp() { presenter = MockRepository.GenerateStub<IPresenter>(); } ... } In particular specifying the type again when creating the mock seems redundant. For example I can write...

django reusable template code

So as I am architecting my project, I'm thinking I must be doing something wrong. Some pieces of template code are reusable that makes me want to extract the code out of the template, but I can't find a good way. For example, some buttons are the same design all throughout the website. What's the best way to extract it out of the page...

rails - DRY respond_to with repeated actions

In one of my rails controller, I must respond to several types of formats, so I use the typical respond_to chain: respond_to do |format| format.html { ... } format.mobile { ... } format.jpg { ... } format.xml { ... } format.js { ... } end Usually that the { ... } part is repeated on several formats. What is the best wa...

Composing functions in Java?

I'm writing demo code for an API we've created and I keep running into the same problem where I'm repeating myself, over and over ad nauseum. I am painfully aware that Java is scheduled to have closures added but I don't have access to them now. Here is what is repeated all over the place that I'd like to just box into it's own little ...

help me "dry" out this .net XML serialization code

I have a base collection class and a child collection class, each of which are serializable. In a test, I discovered that simply having the child class's ReadXml method call base.ReadXml resulted in an InvalidCastException later on. First, here's the class structure: Base Class // Collection of Row objects [Serializable] [XmlRoot("Ro...

Integrating REST Web Services Provision with Main Application

I have a Rails web application that also exposes a few RESTful web services. The main application is secured and obviously uses its own security mechanism whereas the web services would be best authenticated with HTTP authentication. To be very DRY, the controllers behind the web services are shared by the controllers in the main applic...

C# XNA: How to remove repetition from this simple function?

I have a game consisting of ships that fly around on a 2d grid. I am writing a function that takes a location, and figures out if the (predefined) target can be hit from there. This simply requires checking all the grid square's in the potential aggressor's line of fire. In most cases, this is a cross, formed like so: (currX +/- SHOT_R...

How do I write a dry update_output() function for these stages in jQuery?

So I was offered help with this on another question, but I'm struggling to implement it when my app progresses in complexity... var gender; var age; $("#gender :radio").click(function() { gender = $(this).val(); update(); }); $("#age li").click(function() { age = $(this).text(); $('#children').show(); update(); }); function ...

How to "DRY up" C# attributes in Models and ViewModels?

This question was inspired by my struggles with ASP.NET MVC, but I think it applies to other situations as well. Let's say I have an ORM-generated Model and two ViewModels (one for a "details" view and one for an "edit" view): Model public class FooModel // ORM generated { public int Id { get; set; } public string FirstName { ...

Best way to handle view and helper-only constants in Rails

I have a constant that is only used in views, but it's used in different ways in different places. It's an array of option names, and is used for select boxes, but I also use this in other views to see if strings are found in this array, and respond accordingly. What's the best way to handle this to keep DRY? I initially created a con...

Reducing repeated code, jQuery

Ok, so this works perfectly well now however i'm reckoning it could be more optomised as there is a lot of repeated code. Anyone care to have a go? var myObj = { gender:'', age:'', children:'', income:'', stage2select:'', day:'', spend:'', categories:'', product:'', price:'', quantity:'', ...