File: mongoid.rb

package info (click to toggle)
ruby-enumerize 2.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: ruby: 3,712; makefile: 6
file content (50 lines) | stat: -rw-r--r-- 1,456 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

module Enumerize
  module Scope
    module Mongoid
      def enumerize(name, options={})
        super

        _enumerize_module.dependent_eval do
          if self < ::Mongoid::Document
            if options[:scope]
              _define_mongoid_scope_methods!(name, options)
            end
          end
        end
      end

      private

      def _define_mongoid_scope_methods!(name, options)
        return _define_mongoid_shallow_scopes!(name) if options[:scope] == :shallow
        scope_name = options[:scope] == true ? "with_#{name}" : options[:scope]

        define_singleton_method scope_name do |*values|
          values = enumerized_attributes[name].find_values(*values).map(&:value)
          self.in(name => values)
        end

        if options[:scope] == true
          define_singleton_method "without_#{name}" do |*values|
            values = enumerized_attributes[name].find_values(*values).map(&:value)
            not_in(name => values)
          end
        end
      end

      def _define_mongoid_shallow_scopes!(attribute_name)
        enumerized_attributes[attribute_name].each_value do |value_obj|
          define_singleton_method(value_obj) do
            self.in(attribute_name => value_obj.value)
          end

          define_singleton_method("not_#{value_obj}") do
            self.not_in(attribute_name => value_obj.value)
          end
        end
      end
    end
  end
end