Class: Bitmasker::BitmaskScope

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods
Defined in:
lib/bitmasker/bitmask_scope.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make(model_class, field_name, mask_name, bitmask_attributes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bitmasker/bitmask_scope.rb', line 14

def self.make(model_class, field_name, mask_name, bitmask_attributes)
  klass = Class.new(self) do
    define_attribute_methods [mask_name]

    def self.to_s
      "#{superclass}(#{model_class}##{field_name})"
    end
  end

  klass.model_class = model_class
  klass.table_name = model_class.table_name
  klass.field_name = field_name
  klass.mask_name = mask_name
  klass.bitmask_attributes = bitmask_attributes.stringify_keys

  klass
end

Instance Method Details

#bitmaskObject



32
33
34
# File 'lib/bitmasker/bitmask_scope.rb', line 32

def bitmask
  Bitmask.new(bitmask_attributes, 0)
end

#with_any_attribute(_, *attributes) ⇒ Object



42
43
44
45
# File 'lib/bitmasker/bitmask_scope.rb', line 42

def with_any_attribute(_, *attributes)
  # TODO: Test lots of databases
  bitmask_query attributes, "#{table_name}.#{field_name} & :mask <> 0"
end

#with_attribute(_, *attributes) ⇒ Object

REVIEW: This (the unused _ attribute) tells me I have the design wrong



37
38
39
40
# File 'lib/bitmasker/bitmask_scope.rb', line 37

def with_attribute(_, *attributes)
  # TODO: Test lots of databases
  bitmask_query attributes, "#{table_name}.#{field_name} & :mask = :mask"
end

#without_attribute(_, *attributes) ⇒ Object



47
48
49
50
# File 'lib/bitmasker/bitmask_scope.rb', line 47

def without_attribute(_, *attributes)
  # TODO: Test lots of databases
  bitmask_query attributes, "#{table_name}.#{field_name} & :mask = 0 OR #{table_name}.#{field_name} IS NULL"
end