class Mongoid::DynamicMatchers::Method

Attributes

matchers[R]
attribute_names[R]
model[R]
name[R]

Public Class Methods

match(model, name) click to toggle source
# File lib/mongoid/dynamic_matchers.rb, line 31
def match(model, name)
  klass = matchers.find { |k| name =~ k.pattern }
  klass.new(model, name) if klass
end
new(model, name) click to toggle source
# File lib/mongoid/dynamic_matchers.rb, line 51
def initialize(model, name)
  @model = model
  @name = name.to_s
  @attribute_names = @name.match(self.class.pattern)[1].split('_and_')
  @attribute_names.map! { |n| @model.aliased_fields[n] || n }
end
pattern() click to toggle source
# File lib/mongoid/dynamic_matchers.rb, line 36
def pattern
  /^#{prefix}_([_a-zA-Z]\w*)#{suffix}$/
end
prefix() click to toggle source
# File lib/mongoid/dynamic_matchers.rb, line 40
def prefix
  raise NotImplementedError
end
suffix() click to toggle source
# File lib/mongoid/dynamic_matchers.rb, line 44
def suffix
  ''
end

Public Instance Methods

body() click to toggle source
# File lib/mongoid/dynamic_matchers.rb, line 70
def body
  raise NotImplementedError
end
define() click to toggle source
# File lib/mongoid/dynamic_matchers.rb, line 62
      def define
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
          def self.#{name}(#{signature})
            #{body}
          end
        CODE
      end
valid?() click to toggle source
# File lib/mongoid/dynamic_matchers.rb, line 58
def valid?
  attribute_names.all? { |name| model.fields[name] }
end