Sha256: 6bbbbbb45510befd55de2e0b99217a83b79d958d4ae884ed3e36bac8f0b738f3

Contents?: true

Size: 1.38 KB

Versions: 14

Compression:

Stored size: 1.38 KB

Contents

module MongoModel
  class Scope
    module QueryMethods
      def initialize(*)
        SINGLE_VALUE_METHODS.each { |m| instance_variable_set("@#{m}_value", nil) }
        MULTI_VALUE_METHODS.each { |m| instance_variable_set("@#{m}_values", []) }
      end
      
      MULTI_VALUE_METHODS.each do |query_method|
        attr_accessor :"#{query_method}_values"
        
        class_eval <<-CEVAL, __FILE__
          def #{query_method}(*args, &block)
            new_scope = clone
            value = Array.wrap(args.flatten).reject {|x| x.blank? }
            new_scope.#{query_method}_values += value if value.present?
            new_scope
          end
        CEVAL
      end
      
      SINGLE_VALUE_METHODS.each do |query_method|
        attr_accessor :"#{query_method}_value"

        class_eval <<-CEVAL, __FILE__
          def #{query_method}(value, &block)
            new_scope = clone
            new_scope.#{query_method}_value = value
            new_scope
          end
        CEVAL
      end
      
      def from(value, &block)
        new_scope = clone
        new_scope.from_value = value.is_a?(String) ? klass.database.collection(value) : value
        new_scope
      end
      
      def reverse_order
        if order_values.empty?
          order(:id.desc)
        else
          except(:order).order(MongoOrder.parse(order_values).reverse.to_a)
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mongomodel-0.2.20 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.19 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.18 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.17 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.16 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.15 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.14 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.13 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.12 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.11 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.10 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.9 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.8 lib/mongomodel/support/scope/query_methods.rb
mongomodel-0.2.7 lib/mongomodel/support/scope/query_methods.rb