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.

See the Usage section below for information on how to use this gem with rails 2.3.x.

Versioning

As of version 2.0, a new versioning system has been employed. All 2.x versions of this gem support rails versions 2.3.x.

Versions 3.x of the gem will support rails versions 3.x.

Usage

In order to use this gem in your rails 2.3.x app, first modify your config/environment.rb and add the following within the configuration block:

Rails::Initializer.run do |config|
  # ...
  config.gem "army-negative", :version => "~> 2.0"
  # ...
end

You can then run rake gems:install in order to install the gem.

Finally, create a new initializer file in config/initializers such as the following:

# config/initializers/army-negative.rb
require 'army-negative'
Army::Negative.activate!

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.