• Protocol-based dependency injection in Swift

    I’ve been using protocols for dependency injection in Swift for a while, but recently I came up with a nifty little function that helps with the task using generics. Using protocols for dependency injection is a great way to abstract your code and make it more flexible. Say you get an unnamed view controller as a segue.destinationViewController property. In Swift you don’t need to cast it to specific class instance. Instead you can check for protocol conformance. “Is this X” vs “is this something that wants Y” – identity versus function. If later you add a segue to another conforming controller you won’t have to add a line of code. Read more...

  • Swift wrapper for Grand Central Dispatch

    I’ve written another Swift wrapper for GCD. Original C API does not fit Swift at all. It’s tedious and not very readable. Until recently I’ve been using this great gist by Tom Adriaenssen, but now I needed to use other QoS queues beside main and the background queue. Other default global queues available in GCD are: “Interactive”, “User Initiated” and “Utility”.

    Problem with Tom’s implementation was that it declared three class-level methods for every queue it supported. This approach is untenable for more than two queues. Luckily with Swift we can extend types like dispatch_queue_t or dispatch_group_t and define methods that will reference self. This way we can implement sync, async, after methods that will be available on every queue including custom queues created by user. Read more...

  • Open text editor on offenses found by RuboCop

    I’ve recently added RuboCop to one of my existing, large Ruby projects. A lot of the style adjustments RuboCop was able to apply automatically, but a significant number of files was still left for me to fix.

    Opening each file manually seemed like a hassle so I’ve prepared this script that opens $EDITOR on each offense, waits for user to modify the file and goes to the next offense. It’s tuned for MacVim/Vim, but any editor that doesn’t detach from shell should work. This saved me tremendous amounts of time. Read more...

  • Converting or fixing MySql table character set

    Recently I had to migrate some very old website to a new server. When that website was first deployed the MySql server was at version 4.0 and had no internal support for character sets. When MySql 4.1 first appeared and assigned each table and column their own character set it was very common for applications to store data with different actual encoding than the meta data specified in the database schema.

    This led to problems during data migrations and also when browsing database with clients that specify their connection encoding. Whenever a connection encoding different than default (latin1) was specified MySql would try to convert the table contents to the requested coding and fail miserably if the data’s real encoding was different than what was stored in table definition. In my case I was storing utf8 data while MySql was convinced it was latin1. When in a situation like this you have two options. Read more...

  • Trigger AdWords conversion on JavaScript event

    When creating ChoiceVoice we wanted to greatly simplify the order process. Wojtek decided to fit it all on single page powered by Ajax. This worked out great, but at the end of the implementation we faced a minor problem. Google’s Adwords don’t support programmatic conversion tracking. With the snippet provided by Google the user triggers conversion by navigating to a site - like a “Thank you for your order” page. This wasn’t good for us, because our clients are taken to PayPal immediately after filling out the form. Read more...

  • Soft hyphen and in-page search in different browsers

    When creating my main page I wanted to hyphenate words in some places. This can be especially useful when putting content in narrow, justified columns. Unfortunately this rather rudimentary technique in paper media is rarely seen on the web. Currently the best way to break words on your web page is to literally add the soft-hyphen characters to your text. Soft hyphen in html is encoded with a ­ entity.

    Fortunately, unlike how some posts dated a few years back describe it, this entity now displays properly in all modern browsers. Properly means that ­ displays a hyphen when there is a need to break a word, but doesn’t display anything when the word fits the line. There are other issues though. Read more...

subscribe via RSS