Sha256: 82abc26e3b34607a254d6684edc7f6856ff5ca4bd988e9b21cce2cde6f3eb736

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

module Casino
  class Intersection
    module Match
      class Base
        attr_accessor :document, :field, :key, :selector, :value

        def initialize(document, key, selector, value = nil)
          self.document = document
          self.field    = selector[key]
          self.key      = key
          self.selector = selector
          self.value    = document_value || value
        end

        def evaluate
          matcher.evaluate
        end

        def match_arguments
          [ key, value, document, field ]
        end

        def matchers
          matcher_classes.map { |class_name| matcher_for class_name }
        end

        def matcher
          matchers.find { |matcher| matcher.eligible? }
        end

        def matcher_for(class_name)
          class_name = String(class_name).humanize
          Match.const_get(class_name).new(*match_arguments)
        end

        private

        def matcher_classes
          %w(all include recurse greater lesser expression equivalence)
        end

        def document_value
          document.send(key) if document.respond_to?(key)
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-casino-0.0.4 lib/casino/intersection/match/base.rb
mongoid-casino-0.0.3 lib/casino/intersection/match/base.rb
mongoid-casino-0.0.2 lib/casino/intersection/match/base.rb
mongoid-casino-0.0.1 lib/casino/intersection/match/base.rb