github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

xing / flag_shih_tzu

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 137
    • 11
  • Source
  • Commits
  • Network (11)
  • Issues (0)
  • Wiki (2)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (2)
    • flag_query_mode
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A rails plugin to store a collection of boolean attributes in a single ActiveRecord column as a bit field. — Read more

  cancel

http://blog.xing.com/2009/08/a-rails-plugin-to-store-a-collection-of-boolean-values-as-a-single-bit-field/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

README cleanup 
boosty (author)
Mon Feb 08 08:18:26 -0800 2010
commit  9b229aeb758ea5a36bcb09429508bca6506d590d
tree    0a6ddc77c4ead96a8e7ac39b99bee0cfaba68e2d
parent  0be7a18b1fe9586ae273001f05a93256515237c3
flag_shih_tzu /
name age
history
message
file .gitignore Tue Oct 20 16:28:42 -0700 2009 merge d--j/master [pboling]
file README.rdoc Mon Feb 08 08:18:26 -0800 2010 README cleanup [boosty]
file Rakefile Tue Oct 06 01:18:33 -0700 2009 Add RCov for helping us to see coverage. [martincik]
file init.rb Tue Aug 25 05:43:04 -0700 2009 Test flag_shih_tzu using a real ActiveRecord cl... [boosty]
directory lib/ Mon Feb 08 05:50:32 -0800 2010 Introduced :flag_query_mode option to change SQ... [boosty]
directory test/ Mon Feb 08 05:50:32 -0800 2010 Introduced :flag_query_mode option to change SQ... [boosty]
README.rdoc

FlagShihTzu

A rails plugin to store a collection of boolean attributes in a single ActiveRecord column as a bit field.

github.com/xing/flag_shih_tzu

This plugin lets you use a single integer column in an ActiveRecord model to store a collection of boolean attributes (flags). Each flag can be used almost in the same way you would use any boolean attribute on an ActiveRecord object.

The benefits:

  • No migrations needed for new boolean attributes. This helps a lot if you have very large db-tables where you want to avoid ALTER TABLE whenever possible.
  • Only the one integer column needs to be indexed.

Using FlagShihTzu, you can add new boolean attributes whenever you want, without needing any migration. Just add a new flag to the has_flags call.

And just in case you are wondering what "Shih Tzu" means: en.wikipedia.org/wiki/Shih_Tzu

Prerequisites

FlagShihTzu assumes that your ActiveRecord model already has an integer field to store the flags, which should be defined to not allow NULL values and should have a default value of 0 (which means all flags are initially set to false).

The plugin has been tested with Rails versions from 2.1 to 2.3 and MySQL, PostgreSQL and SQLite3 databases.

Installation

  cd path/to/your/rails-project
  ./script/plugin install git://github.com/xing/flag_shih_tzu.git

Usage

Defining the flags

  class Spaceship < ActiveRecord::Base
    include FlagShihTzu

    has_flags 1 => :warpdrive,
              2 => :shields,
              3 => :electrolytes

  end

has_flags takes a hash. The keys must be positive integers and represent the position of the bit being used to enable or disable the flag. The keys must not be changed once in use, or you will get wrong results. That is why the plugin forces you to set them explicitly. The values are symbols for the flags being created.

Generated instance methods

Calling has_flags as shown above creates the following instance methods on Spaceship:

  Spaceship#warpdrive
  Spaceship#warpdrive?
  Spaceship#warpdrive=
  Spaceship#shields
  Spaceship#shields?
  Spaceship#shields=
  Spaceship#electrolytes
  Spaceship#electrolytes?
  Spaceship#electrolytes=

Generated named scopes

The following named scopes become available:

  Spaceship.warpdrive         # :conditions => "(spaceships.flags in (1,3,5,7))"
  Spaceship.not_warpdrive     # :conditions => "(spaceships.flags not in (1,3,5,7))"
  Spaceship.shields           # :conditions => "(spaceships.flags in (2,3,6,7))"
  Spaceship.not_shields       # :conditions => "(spaceships.flags not in (2,3,6,7))"
  Spaceship.electrolytes      # :conditions => "(spaceships.flags in (4,5,6,7))"
  Spaceship.not_electrolytes  # :conditions => "(spaceships.flags not in (4,5,6,7))"

If you do not want the named scopes to be defined, set the :named_scopes option to false when calling has_flags:

  has_flags 1 => :warpdrive, 2 => :shields, 3 => :electrolytes, :named_scopes => false

Examples for using the generated methods

  enterprise = Spaceship.new
  enterprise.warpdrive = true
  enterprise.shields = true
  enterprise.electrolytes = false
  enterprise.save

  if enterprise.shields?
    ...
  end

  Spaceship.warpdrive.find(:all)
  Spaceship.not_electrolytes.count
  ...

How it stores the values

As said, FlagShihTzu uses a single integer column to store the values for all the defined flags as a bit field.

The bit position of a flag corresponds to the given key.

This way, we can use bit operators on the stored integer value to set, unset and check individual flags.

                +---+---+---+                +---+---+---+
                |   |   |   |                |   |   |   |
  Bit position  | 3 | 2 | 1 |                | 3 | 2 | 1 |
  (flag key)    |   |   |   |                |   |   |   |
                +---+---+---+                +---+---+---+
                |   |   |   |                |   |   |   |
  Bit value     | 4 | 2 | 1 |                | 4 | 2 | 1 |
                |   |   |   |                |   |   |   |
                +---+---+---+                +---+---+---+
                | e | s | w |                | e | s | w |
                | l | h | a |                | l | h | a |
                | e | i | r |                | e | i | r |
                | c | e | p |                | c | e | p |
                | t | l | d |                | t | l | d |
                | r | d | r |                | r | d | r |
                | o | s | i |                | o | s | i |
                | l |   | v |                | l |   | v |
                | y |   | e |                | y |   | e |
                | t |   |   |                | t |   |   |
                | e |   |   |                | e |   |   |
                | s |   |   |                | s |   |   |
                +---+---+---+                +---+---+---+
                | 1 | 1 | 0 | = 4 + 2 = 6    | 1 | 0 | 1 | = 4 + 1 = 5
                +---+---+---+                +---+---+---+

Read more about bit fields here: en.wikipedia.org/wiki/Bit_field

Using a custom column name

The default column name to store the flags is ‘flags’, but you can provide a custom column name using the :column option:

  has_flags 1 => :warpdrive, :column => 'bits'

Support for manually building conditions

The following class methods may support you when manually building ActiveRecord conditions:

  Spaceship.warpdrive_condition         # "(spaceships.flags in (1,3,5,7))"
  Spaceship.not_warpdrive_condition     # "(spaceships.flags not in (1,3,5,7))"
  Spaceship.shields_condition           # "(spaceships.flags in (2,3,6,7))"
  Spaceship.not_shields_condition       # "(spaceships.flags not in (2,3,6,7))"
  Spaceship.electrolytes_condition      # "(spaceships.flags in (4,5,6,7))"
  Spaceship.not_electrolytes_condition  # "(spaceships.flags not in (4,5,6,7))"

Choosing a query mode

While the default way of building the SQL conditions uses an IN() list (as shown above), this approach will not work well for a high number of flags, as the value list for IN() grows.

For MySQL, depending on your MySQL settings, this can even hit the ‘max_allowed_packet’ limit with the generated query.

In this case, consider changing the flag query mode to :bit_operator instead of :in_list, like so:

  has_flags 1 => :warpdrive, 2 => :shields, :flag_query_mode => :bit_operator

This will modify the generated condition and named_scope methods to use bit operators in the SQL instead of an IN() list:

  Spaceship.warpdrive_condition         # "(spaceships.flags & 1 = 1)",
  Spaceship.not_warpdrive_condition     # "(spaceships.flags & 1 = 0)",
  Spaceship.shields_condition           # "(spaceships.flags & 2 = 2)",
  Spaceship.not_shields_condition       # "(spaceships.flags & 2 = 0)",

  Spaceship.warpdrive         # :conditions => "(spaceships.flags & 1 = 1)"
  Spaceship.not_warpdrive     # :conditions => "(spaceships.flags & 1 = 0)"
  Spaceship.shields           # :conditions => "(spaceships.flags & 2 = 2)"
  Spaceship.not_shields       # :conditions => "(spaceships.flags & 2 = 0)"

The drawback is that due to the bit operator, this query can not use an index on the flags column.

Running the plugin tests

  1. Modify test/database.yml to fit your test environment.
  2. If needed, create the test database you configured in test/database.yml.

Then you can run

  DB=mysql|postgres|sqlite3 rake test:plugins PLUGIN=flag_shih_tzu

from your Rails project root or

  DB=mysql|postgres|sqlite3 rake

from vendor/plugins/flag_shih_tzu.

Authors

Patryk Peszko, Sebastian Roebke, David Anderson and Tim Payton

Please find out more about our work in our tech blog.

Contributors

TobiTobes, Martin Stannard, Ladislav Martincik, Peter Boling, Daniel Jagszent, Thorsten Boettger, Darren Torpey and Joost Baaij

License

The MIT License

Copyright © 2009 XING AG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server