Skip to content

Instantly share code, notes, and snippets.

@yosriady
Created August 27, 2014 15:03
Show Gist options
  • Select an option

  • Save yosriady/378ca8baa41917daf5cf to your computer and use it in GitHub Desktop.

Select an option

Save yosriady/378ca8baa41917daf5cf to your computer and use it in GitHub Desktop.
Rails bitmask_attributes attributes to bitmask conversion
class Sheet < ActiveRecord::Base
bitmask :instruments, :as => [:guitar, :piano, :bass, :mandolin, :banjo, :ukulele, :violin, :flute, :harmonica, :trombone, :trumpet, :clarinet, :saxophone, :others], :null => false
def self.instruments_to_bitmask(instruments)
(instruments & Sheet.values_for_instruments).map { |r| 2**Sheet.values_for_instruments.index(r) }.inject(0, :+)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment