Module: BitMagic::Adapters::MongoidAdapter::ClassMethods
- Defined in:
- lib/bit_magic/adapters/mongoid_adapter.rb
Constant Summary collapse
- BIT_MAGIC_BOOLEAN_CASTER =
Cast the given value into a Boolean, follows Mongoid::Boolean rules
lambda do |val| !!Mongoid::Boolean.mongoize(val) end
- BIT_MAGIC_ADAPTER_DEFAULTS =
Adapter options specific to this adapter
:named_scopes Enables (true) or disables (false) individual scopes to query fields
:query_by_value whether to use bitwise operations or IN (?) when querying by default will use IN (?) if the total bits defined by bit_magic is less than or equal to 8. true to always query by value, false to always query using bitwise operations
{ :bool_caster => BIT_MAGIC_BOOLEAN_CASTER, :named_scopes => true, :query_by_value => 8 }.freeze
Instance Method Summary collapse
-
#bit_magic_adapter(name) ⇒ Object
This method is called by Base#bit_magic after setting up the magician Here, we inject query helpers, scopes, and other useful methods.
-
#bit_magic_adapter_defaults(options) ⇒ Object
Method used to set adapter defaults as options to Magician, Used by the bit_magic definition to add custom options to the magician.
Instance Method Details
#bit_magic_adapter(name) ⇒ Object
This method is called by Base#bit_magic after setting up the magician Here, we inject query helpers, scopes, and other useful methods
Query helpers: (NAMESPACE is the name given to bit_magic) All the methods that generate where queries take an optional options hash as the last value. Can be used to alter options given to bit_magic. eg: passing ‘false’ as the last argument will force the query to generate bitwise operations instead of ‘$in => []’ queries
NAMESPACE_query_helper(field_names = nil)
an internal method used by other query helpers
NAMESPACE_where_in(array, column_name = nil)
generates a 'column_name => {:$in => [...]}' query for the array numbers
column_name defaults to attribute_name in the options
NAMESPACE_with_all(*field_names, options = {})
takes one or more field names, and queries for values where ALL of
them are enabled. For fields with multiple bits, they must be max value
This is the equivalent of: field[0] and field[1] and field[2] ...
NAMESPACE_with_any(*field_names, options = {})
takes one or more field names, and queries for values where any of
them are enabled
This is the equivalent of: field[0] or field[1] or field[2] ...
NAMESPACE_without_any(*field_names, options = {})
takes one or more field names, and queries for values where at least
one of them is disabled. For fields with multiple bits, any value
other than maximum number
This is the equivalent of "!field[0] or !field[1] or !field[2] ..."
NAMESPACE_without_all(*field_names, options = {})
takes one or more field names and queries for values where none of
them are enabled (all disabled). For fields with multiple bits,
value must be zero.
This is the equivalent of: !field[0] and !field[1] and !field[2] ...
NAMESPACE_equals(field_value_list, options = {})
* this will truncate values to match the number of bits available
field_value_list is a Hash with field_name => value key-pairs.
generates a query that matches the bits to the value, exactly
This is the equivalent of: field[0] = val and field[1] = value ...
Additional named scopes These can be disabled by passing ‘named_scopes: false’ as an option FIELD is the field name for the bit/bit range
NAMESPACE_FIELD
queries for values where FIELD has been enabled
NAMESPACE_not_FIELD
queries for values where FIELD has been disabled (not enabled)
NAMESPACE_FIELD_equals(value)
* only exists for fields with more than one bit
queries for values where FIELD is exactly equal to value
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/bit_magic/adapters/mongoid_adapter.rb', line 126 def bit_magic_adapter(name) query_prep = :"#{name}_query_helper" query_in = :"#{name}_where_in" self.class_eval do define_singleton_method(query_prep) do |field_names = nil| magician = @bit_magic_fields[name] bit_gen = magician.bits_generator = (field_names.is_a?(Array) and field_names.last.is_a?(Hash)) ? field_names.pop : {} by_value = .key?(:query_by_value) ? [:query_by_value] : magician.[:query_by_value] by_value = (magician.bits_length <= by_value) if by_value.is_a?(Integer) column_name = [:column_name] || magician.[:column_name] || magician.[:attribute_name] [magician, bit_gen, by_value, column_name] end define_singleton_method(query_in) do |column_name, arr| where(column_name => {:$in => arr}) end define_singleton_method(:"#{name}_with_all") do |*field_names| magician, bit_gen, by_value, column_name = self.send(query_prep, field_names) if by_value === true self.send(query_in, column_name, bit_gen.all_of(*field_names)) else where(column_name => {:$bitsAllSet => bit_gen.all_of_number(*field_names)}) end end define_singleton_method(:"#{name}_without_any") do |*field_names| magician, bit_gen, by_value, column_name = self.send(query_prep, field_names) if by_value === true self.send(query_in, column_name, bit_gen.instead_of(*field_names)) else where(column_name => {:$bitsAnyClear => bit_gen.any_of_number(*field_names)}) end end define_singleton_method(:"#{name}_without_all") do |*field_names| magician, bit_gen, by_value, column_name = self.send(query_prep, field_names) if by_value === true self.send(query_in, column_name, bit_gen.none_of(*field_names)) else where(column_name => {:$bitsAllClear => bit_gen.any_of_number(*field_names)}) end end # Query for if any of these bits are set. define_singleton_method(:"#{name}_with_any") do |*field_names| magician, bit_gen, by_value, column_name = self.send(query_prep, field_names) if by_value === true self.send(query_in, column_name, bit_gen.any_of(*field_names)) else where(column_name => {:$bitsAnySet => bit_gen.any_of_number(*field_names)}) end end define_singleton_method(:"#{name}_equals") do |field_value, = {}| magician, bit_gen, by_value, column_name = self.send(query_prep, []) if by_value === true self.send(query_in, column_name, bit_gen.equal_to(field_value)) else all_num, none_num = bit_gen.equal_to_numbers(field_value) where(column_name => {:$bitsAllSet => all_num, :$bitsAllClear => none_num}) end end end if @bit_magic_fields and @bit_magic_fields[name] and @bit_magic_fields[name].[:named_scopes] fields = @bit_magic_fields[name].field_list self.class_eval do fields.each_pair do |field, value| define_singleton_method(:"#{name}_#{field}") do self.send(:"#{name}_with_all", field) end define_singleton_method(:"#{name}_not_#{field}") do self.send(:"#{name}_without_all", field) end if value.is_a?(Array) and value.length > 1 define_singleton_method(:"#{name}_#{field}_equals") do |val| self.send(:"#{name}_equals", field => val) end end end end end end |
#bit_magic_adapter_defaults(options) ⇒ Object
Method used to set adapter defaults as options to Magician, Used by the bit_magic definition to add custom options to the magician
69 70 71 |
# File 'lib/bit_magic/adapters/mongoid_adapter.rb', line 69 def bit_magic_adapter_defaults() BIT_MAGIC_ADAPTER_DEFAULTS.merge() end |