Army::Negative – Negative ones for true!

This gem is a simple ActiveRecord MySQL (ARMy) connection adapter monkey-patch. Put it in your Gemfile and all your true are belong to us! Or, rather, they’ll suddenly become very negative, negative one specifically.

Versioning

In general, army-negative versions are pinned to the supported rails version. Specifically, the major and minor version numbers will always match.

So, to use army-negative in a rails app, you’d use a version specifier in its entry in your Gemfile.

Examples:

gem "army-negative", "~> 3.0.0" # for all rails 3.0.x apps
gem "army-negative", "~> 3.1.0" # for all rails 3.1.x apps
gem "army-negative", "~> 4.5.6" # would work in any rails 4.5.x app
# etc.

The exception to this rule is in rails 2.3.x apps (the oldest supported rails version). For these versions, you should use the latest 2.x version of army-negative (see the rails2 branch in the git repo).

Usage

Simply put a line like the following in your Gemfile:

gem "army-negative", "~> 3.1.0"

Then do a bundle install on the command-line and you’re set.

What This Does

This makes ActiveRecord store the value -1 into your TINYINT boolean columns whenever they’re set to true. It also makes ActiveRecord recognize -1 as true when a boolean field is queried.

NOTE: positive one and all other values that were interpreted as true by the MySQL connection adapter will still be recognized as true too.

Why

This was written for a rails application that needed to access a legacy MySQL database. The new application needed to work concurrently with the existing (old) application, following its conventions.

The old application is a Microsoft Access 2003 program that uses the “linked tables” feature to store the data in a MySQL database. Microsoft’s Visual Basic for Applications (VBA) stores true values in memory with all bits turned on. If interpreted as an integer using two’s complement, this is negative one. So the old application stores all our true values as negative one. Because of this, many of the hand-written legacy queries also write and expect to read true values as negative one.

The Name

Though no one besides myself is likely to see or use this gem, I still wanted to push it out to the public for posterity. The ar in ARmy is for ActiveRecord while the my in arMy is for MySQL. I’m sure you can guess what negative is supposed to reference.

Authors and Credits

Authors

Kendall Gifford

License

Licensed using the standard MIT License. See the file LICENSE in the root folder of the project.