Sha256: 23a11899eff9f2f26ac564561c986d7faffa505b4318332505a4c4fa5bebd9ae

Contents?: true

Size: 1.36 KB

Versions: 77

Compression:

Stored size: 1.36 KB

Contents

$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require 'mongo_mapper'
require 'pp'

MongoMapper.database = 'testing'

class User
  include MongoMapper::Document

  # plain old vanilla scopes with fancy queries
  scope :johns,   where(:name => 'John')

  # plain old vanilla scopes with hashes
  scope :bills, :name => 'Bill'

  # dynamic scopes with parameters
  scope :by_name,  lambda { |name| where(:name => name) }
  scope :by_ages,  lambda { |low, high| where(:age.gte => low, :age.lte => high) }

  # Yep, even plain old methods work as long as they return a query
  def self.by_tag(tag)
    where(:tags => tag)
  end

  # You can even make a method that returns a scope
  def self.twenties; by_ages(20, 29) end

  key :name, String
  key :tags, Array
end
User.collection.remove # empties collection

User.create(:name => 'John',  :tags => %w[ruby mongo], :age => 28)
User.create(:name => 'Bill',  :tags => %w[ruby mongo], :age => 30)
User.create(:name => 'Frank', :tags => %w[mongo],      :age => 35)
User.create(:name => 'Steve', :tags => %w[html5 css3], :age => 27)

# simple scopes
pp User.johns.first
pp User.bills.first

# scope with arg
pp User.by_name('Frank').first

# scope with two args
pp User.by_ages(20, 29).all

# chaining class methods on scopes
pp User.by_ages(20, 40).by_tag('ruby').all

# scope made using method that returns scope
pp User.twenties.all

Version data entries

77 entries across 77 versions & 9 rubygems

Version Path
mongo_mapper-0.14.0 examples/scopes.rb
mongo_mapper-0.14.0.rc1 examples/scopes.rb
mongo_mapper-0.13.1 examples/scopes.rb
mongo_mapper-0.13.0 examples/scopes.rb
mongo_mapper-0.13.0.beta2 examples/scopes.rb
mongo_mapper-0.13.0.beta1 examples/scopes.rb
mongo_mapper-0.12.0 examples/scopes.rb
lookout-mongo_mapper-0.11.3 examples/scopes.rb
mongo_mapper-0.11.2 examples/scopes.rb
jamieorc-mongo_mapper-0.11.1.1 examples/scopes.rb
mongo_mapper-0.11.1 examples/scopes.rb
mongo_mapper-0.11.0 examples/scopes.rb
mongo_mapper-0.10.1 examples/scopes.rb
mongo_mapper-0.10.0 examples/scopes.rb
mongo_mapper-0.9.2 examples/scopes.rb
jonbell-mongo_mapper-0.8.6 examples/scopes.rb
mongo_mapper-0.9.1 examples/scopes.rb
mongo_mapper-0.9.0 examples/scopes.rb
ign-mongo_mapper-0.8.6.2 examples/scopes.rb
ign-mongo_mapper-0.8.6.1 examples/scopes.rb