Question: I have a Rails application with Resque doing background jobs on multiple servers. The jobs are different in terms of their resource needs, so I would like to limit the amount of jobs of particular type, each server can do. I want to be able to have multiple servers query the same Redis database. 

Here is the scenario:
I have Server-1 and Server-2 connected to the same Redis database.

1) Each server should take 3 "Encoding" jobs and 5 "Parsing" jobs max (8 jobs total). 

2) At a given moment Redis-server will have 10 jobs in "Encoding" queue and 12 jobs in "Parsing" queue available for Resque workers to process. I have 2 servers running, quering the same database. 

QUESTION: How can I prevent Server-1 from taking 8 "Encoding" jobs and Server-2 taking the rest? In other words, how to restrict Server-1 to taking only 3 jobs from "Encoding" queue and 5 jobs from "Parsing" queue and have Server-2 do the same? 

I want to keep the code universal so that I can deploy it to multiple servers without individual tweaking..

Answer: 
```
NSString* val = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    NSCharacterSet *allowedCharacterSet = [NSCharacterSet decimalDigitCharacterSet];
    if ([[string componentsSeparatedByCharactersInSet:[allowedCharacterSet invertedSet]] count] > 1 || [val length] > 5) {
        return NO;
    }

```
If you use my [specification pattern](https://github.com/neoneye/SpecificationPattern) then code looks like this

```
textField.delegate = self

lazy var specification: Specification = {
    return RegularExpressionSpecification(pattern: "^(|0|[1-9]\\d{0,6})$")
}()

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let textFieldString: NSString = textField.text ?? ""
    let s = textFieldString.stringByReplacingCharactersInRange(range, withString:string)
    return specification.isSatisfiedBy(s)
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    let s = textField.text ?? ""
    let isTextValid = specification.isSatisfiedBy(s)
    if isTextValid {
        textField.resignFirstResponder()
    }
    return false
}

```