Module: BitMagic::Adapters::MongoidAdapter

Extended by:
ActiveSupport::Concern
Defined in:
lib/bit_magic/adapters/mongoid_adapter.rb

Overview

This is the adapter for Mongoid. It’s implemented as a concern to be included inside Mongoid::Document models.

It’s expected that you have an integer column (default name ‘flags’, override using the attribute_name option). It’s suggested, though not required, that you set the default value same as the bit_magic default.

If you have more than one model that you want to use BitMagic in, it’s recommended that you just include this adapter globally:

require 'bit_magic/adapters/mongoid_adapter'
Mongoid::Document.include BitMagic::Adapters::MongoidAdapter

Otherwise, you can include it on a per model basis before calling bit_magic

class Example
  include Mongoid::Document
  # this line below can be excluded if you included the adapter globally
  include BitMagic::Adapters::MongoidAdapter 

  bit_magic :settings, 0 => :is_odd, [1, 2, 3] => :amount, 4 => :is_cool
  field :flags, type: Integer, default: 0
end

After that, you can start using query helpers and instance helpers. Query helpers return a standard Mongoid::Criteria, so you can do everything you normally would in a query (like chaining additional conditions). Instance helpers are wrapped around by a Bits object, in this case, ‘settings’ but also have helper methods added based on the name of the fields.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.1.0".freeze